Documentation ¶
Overview ¶
Package gohttp is sample http server framework This package is 1. used like std net/http app.Init()
app.RouteFunc("/foo", func(resp http.ResponseWriter, req http.Request) { resp.Write([]byte("foo ok") })
2. used gohttp.Context app.Init()
app.RouteFunCtx("/bar", func(ctx *gohttp.Context) { ctx.Output("bar ok") })
3. used like tornado
type baz struct { gohttp.Context }
func (ctx *baz) GET() { ctx.Output("baz ok") }
app.Init() app.Route("/baz", &baz{})
Index ¶
- Constants
- Variables
- func InitLog() *log.Log
- func RegistErrCode(code int, msg string) error
- func WalkDir(path string, matchs ...func(path string) bool) ([]string, error)
- type APIHandler
- func (resp APIHandler) CloseNotify() <-chan bool
- func (ctx *APIHandler) Finish()
- func (resp APIHandler) Finished() bool
- func (resp APIHandler) Flush()
- func (resp APIHandler) Header() http.Header
- func (resp APIHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (resp APIHandler) Size() int
- func (resp APIHandler) Status() int
- func (resp APIHandler) Write(b []byte) (int, error)
- func (resp APIHandler) WriteHeader(code int)
- type APIOutput
- type Application
- type BaseHTTPHandler
- func (resp BaseHTTPHandler) CloseNotify() <-chan bool
- func (ctx *BaseHTTPHandler) Finish()
- func (resp BaseHTTPHandler) Finished() bool
- func (resp BaseHTTPHandler) Flush()
- func (ctx *BaseHTTPHandler) HEAD()
- func (resp BaseHTTPHandler) Header() http.Header
- func (resp BaseHTTPHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (ctx *BaseHTTPHandler) Initialize()
- func (ctx *BaseHTTPHandler) Prepare()
- func (ctx *BaseHTTPHandler) SetCookie(name, value string)
- func (resp BaseHTTPHandler) Size() int
- func (resp BaseHTTPHandler) Status() int
- func (resp BaseHTTPHandler) Write(b []byte) (int, error)
- func (resp BaseHTTPHandler) WriteHeader(code int)
- type Config
- func (cfg *Config) SetAddress(addr string)
- func (cfg *Config) SetDebug(debug bool)
- func (cfg *Config) SetLogFormat(format string)
- func (cfg *Config) SetMaxHeaderBytes(max int)
- func (cfg *Config) SetStaticPath(static ...string)
- func (cfg *Config) SetTLS(cert, key string)
- func (cfg *Config) SetTimeout(timeout int)
- func (cfg *Config) SetWebPath(web string)
- type Context
- func (resp Context) CloseNotify() <-chan bool
- func (ctx *Context) Download(file string)
- func (resp Context) Finished() bool
- func (resp Context) Flush()
- func (ctx *Context) GetBody(name string) interface{}
- func (ctx *Context) GetBodyArgs() []byte
- func (ctx *Context) GetCookie(name string) string
- func (ctx *Context) GetCookies() []*http.Cookie
- func (ctx *Context) GetForm() (map[string]string, map[string]*multipart.FileHeader, error)
- func (ctx *Context) GetMatch(name string, null string) string
- func (ctx *Context) GetMatchArgs() map[string]string
- func (ctx *Context) GetQuery(name string, null string) string
- func (ctx *Context) GetQueryArgs() map[string][]string
- func (ctx *Context) HTML(v interface{}, code ...int)
- func (ctx *Context) HTTPError(msg string, code int)
- func (resp Context) Header() http.Header
- func (resp Context) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (ctx *Context) JSON(v interface{}, code ...int)
- func (ctx *Context) Output(v interface{}, code ...int)
- func (ctx *Context) RecvFile(name string, path string) (string, error)
- func (ctx *Context) Redirect(url string, code int)
- func (ctx *Context) RemoteAddr() string
- func (ctx *Context) Render(tpl string, data interface{})
- func (ctx *Context) SaveFile(fh *multipart.FileHeader, path string, name ...string) (string, error)
- func (resp Context) Size() int
- func (resp Context) Status() int
- func (resp Context) Write(b []byte) (int, error)
- func (resp Context) WriteHeader(code int)
- func (ctx *Context) XML(v interface{}, code ...int)
- type Entry
- type HandleFunCtx
- type HandleFunc
- type HandleHTTP
- type Handler
- type InfoHandler
- func (resp InfoHandler) CloseNotify() <-chan bool
- func (resp InfoHandler) Finished() bool
- func (resp InfoHandler) Flush()
- func (ctx *InfoHandler) GET()
- func (resp InfoHandler) Header() http.Header
- func (resp InfoHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (resp InfoHandler) Size() int
- func (resp InfoHandler) Status() int
- func (resp InfoHandler) Write(b []byte) (int, error)
- func (resp InfoHandler) WriteHeader(code int)
- type Logger
- type ResponseWriter
- type RouteHandler
- func (resp RouteHandler) CloseNotify() <-chan bool
- func (resp RouteHandler) Finished() bool
- func (resp RouteHandler) Flush()
- func (ctx *RouteHandler) GET()
- func (resp RouteHandler) Header() http.Header
- func (resp RouteHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (resp RouteHandler) Size() int
- func (resp RouteHandler) Status() int
- func (resp RouteHandler) Write(b []byte) (int, error)
- func (resp RouteHandler) WriteHeader(code int)
- type RouterList
- func (r *RouterList) Remove(path string)
- func (r *RouterList) Route(path string, handler Handler)
- func (r *RouterList) RouteCtxMethod(method, path string, f HandleFunCtx)
- func (r *RouterList) RouteFunCtx(path string, f HandleFunCtx)
- func (r *RouterList) RouteFunc(path string, f HandleFunc)
- func (r *RouterList) RouteMethod(method, path string, f HandleFunc)
- func (r *RouterList) RouteStdHandler(path string, h http.Handler)
- func (r *RouterList) String() string
- type Template
Constants ¶
const (
// VERSION version
VERSION = "0.2.7.041819_beta"
)
Variables ¶
var ( // StartTrace 运行trace StartTrace = func(ctx *Context) { f, err := os.Create("trace.out") if err != nil { panic(err) } err = trace.Start(f) if err != nil { panic(err) } } // StopTrace 停止trace StopTrace = func(ctx *Context) { trace.Stop() } // StartGC 手动触发GC StartGC = func(ctx *Context) { runtime.GC() } // StopGC stop gc StopGC = func(ctx *Context) { debug.SetGCPercent(-1) } // Index wapper pprof.Index, default path is /debug/pprof/ Index = pprof.Index // Cmdline wapper pprof.Cmdline, default path is /debug/pprof/cmdline Cmdline = pprof.Cmdline // Profile wapper pprof.Profile, default path is /debug/pprof/profile Profile = pprof.Profile // Symbol wapper pprof.Symbol, default path is /debug/pprof/symbol Symbol = pprof.Symbol // Trace wapper pprof.Trace, default path is /debug/pprof/trace Trace = pprof.Trace )
var ErrMap = map[int]string{
0: "success",
1000: "success",
1001: "fail",
1002: "",
}
ErrMap xx
Functions ¶
func RegistErrCode ¶
RegistErrCode regist api error code and msg.
Types ¶
type APIHandler ¶
type APIHandler struct { APIOutput BaseHTTPHandler }
APIHandler designed for http api. It can used easily.
func (APIHandler) CloseNotify ¶
func (resp APIHandler) CloseNotify() <-chan bool
Implements the http.CloseNotify interface
func (APIHandler) Hijack ¶
func (resp APIHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
Implements the http.Hijacker interface
func (APIHandler) WriteHeader ¶
func (resp APIHandler) WriteHeader(code int)
rewrite http.ResponseWriter interface method
type APIOutput ¶
type APIOutput struct { Code int `json:"code"` Msg string `json:"msg"` Err error `json:"err"` Data interface{} `json:"data"` }
APIOutput is sturct data need responsed.
func (APIOutput) MarshalJSON ¶
MarshalJSON rewrite format to json, implement json.Marshaler interface.
type Application ¶
Application is a httpserver instance.
func (*Application) Run ¶
func (app *Application) Run(addr ...string)
Run starts the server by listen address. HTTP/2.0 is only supported in https, If server is http mode, then HTTP/1.x will be used.
func (*Application) ServeHTTP ¶
func (app *Application) ServeHTTP(responsewriter http.ResponseWriter, request *http.Request)
ServeHTTP is HTTP server implement method. It makes App compatible to native http handler.
func (*Application) Stop ¶
func (app *Application) Stop() error
Stop gracefully shuts down the server without interrupting any active connections.
type BaseHTTPHandler ¶
type BaseHTTPHandler struct {
*Context
}
BaseHTTPHandler http handler
func (BaseHTTPHandler) CloseNotify ¶
func (resp BaseHTTPHandler) CloseNotify() <-chan bool
Implements the http.CloseNotify interface
func (BaseHTTPHandler) Flush ¶
func (resp BaseHTTPHandler) Flush()
Implements the http.Flush interface
func (BaseHTTPHandler) Hijack ¶
func (resp BaseHTTPHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
Implements the http.Hijacker interface
func (*BaseHTTPHandler) SetCookie ¶
func (ctx *BaseHTTPHandler) SetCookie(name, value string)
SetCookie set cookie for response
func (BaseHTTPHandler) WriteHeader ¶
func (resp BaseHTTPHandler) WriteHeader(code int)
rewrite http.ResponseWriter interface method
type Config ¶
type Config struct { Debug bool `json:"debug" yaml:"debug"` Addr string `json:"addr" yaml:"addr"` ReadTimeout int `json:"readtimeout" yaml:"readtimeout"` ReadHeaderTimeout int `json:"readheadertime" yaml:"readheadertime"` WriteTimeout int `json:"writetime" yaml:"writetime"` MaxHeaderBytes int `json:"maxheaderbytes" yaml:"maxheaderbytes"` CertFile string `json:"cert" yaml:"cert"` KeyFile string `json:"key" yaml:"key"` WebPath string `json:"web_path" yaml:"web_path"` StaticPath []string `json:"static_path" yaml:"static_path"` LogFormat string `json:"log_format" yaml:"log_format"` }
Config config
func (*Config) SetLogFormat ¶
SetLogFormat set log format
func (*Config) SetMaxHeaderBytes ¶
SetMaxHeaderBytes set max header bytes
func (*Config) SetStaticPath ¶
SetStaticPath set static path
type Context ¶
type Context struct { *http.Request *Application // contains filtered or unexported fields }
Context gohttp context
func (Context) CloseNotify ¶
func (resp Context) CloseNotify() <-chan bool
Implements the http.CloseNotify interface
func (*Context) GetBodyArgs ¶
GetBodyArgs fetch body arguments
func (*Context) GetForm ¶
GetForm formdata, Content-Type must be multipart/form-data. TODO: RemoveAll removes any temporary files associated with a Form.
func (*Context) GetMatch ¶
GetMatch fetch match argument named by <name>, null is default value defined by user
func (*Context) GetMatchArgs ¶
GetMatchArgs get match args
func (*Context) GetQuery ¶
GetQuery fetch query argument named by <name>, null is default value defined by user
func (*Context) GetQueryArgs ¶
GetQueryArgs get query args
func (Context) Hijack ¶
func (resp Context) Hijack() (net.Conn, *bufio.ReadWriter, error)
Implements the http.Hijacker interface
func (*Context) RemoteAddr ¶
RemoteAddr remote addr for request, copied from net/url.stripPort
func (Context) WriteHeader ¶
func (resp Context) WriteHeader(code int)
rewrite http.ResponseWriter interface method
type HandleFunCtx ¶
type HandleFunCtx func(*Context)
HandleFunCtx handle fun ctx
func (HandleFunCtx) Exec ¶
func (f HandleFunCtx) Exec(ctx *Context)
Exec implements HandleHTTP interface
type HandleFunc ¶
type HandleFunc func(http.ResponseWriter, *http.Request)
HandleFunc handle func
func (HandleFunc) Exec ¶
func (f HandleFunc) Exec(ctx *Context)
Exec implements HandleHTTP interface
type Handler ¶
type Handler interface { // Handler implements ServeHTTP(http.ResponseWriter, *http.Request) func. http.Handler // Prepare invoked before Init. Prepare() // Initialize invoked before httpMethod func. Initialize() // Finish invoked after httpMethod func. Finish() WriteHeader(code int) // contains filtered or unexported methods }
Handler implements http handler interface. Initialize -> Prepare -> GET/POST... -> Finish
type InfoHandler ¶
type InfoHandler struct {
BaseHTTPHandler
}
InfoHandler info handler
func (InfoHandler) CloseNotify ¶
func (resp InfoHandler) CloseNotify() <-chan bool
Implements the http.CloseNotify interface
func (InfoHandler) Hijack ¶
func (resp InfoHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
Implements the http.Hijacker interface
func (InfoHandler) WriteHeader ¶
func (resp InfoHandler) WriteHeader(code int)
rewrite http.ResponseWriter interface method
type Logger ¶
type Logger interface { Debugf(string, ...interface{}) Infof(string, ...interface{}) Warnf(string, ...interface{}) Errorf(string, ...interface{}) Fatalf(string, ...interface{}) }
Logger interface
type ResponseWriter ¶
type ResponseWriter interface { // ResponseWriter have three method: // Header() Header <1> // Write([]byte) (int, error) <2> // WriteHeader(int) <3> http.ResponseWriter http.Flusher http.Hijacker // Status returns the status code of the response or 0 if the response has not been written. Status() int // Finished returns whether or not the ResponseWriter has been finished. Finished() bool // Size returns the size of the response body. Size() int }
ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.
type RouteHandler ¶
type RouteHandler struct {
BaseHTTPHandler
}
RouteHandler routehandler
func (RouteHandler) CloseNotify ¶
func (resp RouteHandler) CloseNotify() <-chan bool
Implements the http.CloseNotify interface
func (RouteHandler) Hijack ¶
func (resp RouteHandler) Hijack() (net.Conn, *bufio.ReadWriter, error)
Implements the http.Hijacker interface
func (RouteHandler) WriteHeader ¶
func (resp RouteHandler) WriteHeader(code int)
rewrite http.ResponseWriter interface method
type RouterList ¶
type RouterList []*route
RouterList router List
func (*RouterList) RouteCtxMethod ¶
func (r *RouterList) RouteCtxMethod(method, path string, f HandleFunCtx)
RouteCtxMethod route by method
func (*RouterList) RouteFunCtx ¶
func (r *RouterList) RouteFunCtx(path string, f HandleFunCtx)
RouteFunCtx route handle func
func (*RouterList) RouteFunc ¶
func (r *RouterList) RouteFunc(path string, f HandleFunc)
RouteFunc route handle func
func (*RouterList) RouteMethod ¶
func (r *RouterList) RouteMethod(method, path string, f HandleFunc)
RouteMethod route by method
func (*RouterList) RouteStdHandler ¶
func (r *RouterList) RouteStdHandler(path string, h http.Handler)
RouteStdHandler route wapper by net/http.Handler
func (*RouterList) String ¶
func (r *RouterList) String() string