Documentation ¶
Index ¶
- func GetTransporterName() (tName string)deprecated
- func SetTransporter(transporter func(options *config.Options) network.Transporter)
- type CtxCallback
- type CtxErrCallback
- type Engine
- func (engine *Engine) AddProtocol(protocol string, factory interface{})
- func (engine *Engine) Close() error
- func (engine *Engine) Delims(left, right string) *Engine
- func (engine *Engine) GetCtxPool() *sync.Pool
- func (engine *Engine) GetOptions() *config.Options
- func (engine *Engine) GetServerName() []byte
- func (engine *Engine) GetTracer() tracer.Controller
- func (engine *Engine) GetTransporterName() (tName string)
- func (engine *Engine) HasServer(name string) bool
- func (engine *Engine) HijackConnHandle(c network.Conn, h app.HijackHandler)
- func (engine *Engine) Init() error
- func (engine *Engine) IsRunning() bool
- func (engine *Engine) IsStreamRequestBody() bool
- func (engine *Engine) IsTraceEnable() bool
- func (engine *Engine) LoadHTMLFiles(files ...string)
- func (engine *Engine) LoadHTMLGlob(pattern string)
- func (engine *Engine) MarkAsRunning() (err error)
- func (engine *Engine) NewContext() *app.RequestContext
- func (engine *Engine) NoMethod(handlers ...app.HandlerFunc)
- func (engine *Engine) NoRoute(handlers ...app.HandlerFunc)
- func (engine *Engine) PrintRoute(method string)
- func (engine *Engine) Routes() (routes RoutesInfo)
- func (engine *Engine) Run() (err error)
- func (engine *Engine) Serve(c context.Context, conn network.Conn) (err error)
- func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext)
- func (engine *Engine) ServeStream(ctx context.Context, conn network.StreamConn) error
- func (engine *Engine) SetAltHeader(targetProtocol, altHeaderValue string)
- func (engine *Engine) SetAutoReloadHTMLTemplate(tmpl *template.Template, files []string)
- func (engine *Engine) SetClientIPFunc(f app.ClientIP)
- func (engine *Engine) SetFormValueFunc(f app.FormValueFunc)
- func (engine *Engine) SetFuncMap(funcMap template.FuncMap)
- func (engine *Engine) SetHTMLTemplate(tmpl *template.Template)
- func (engine *Engine) Shutdown(ctx context.Context) (err error)
- func (engine *Engine) Use(middleware ...app.HandlerFunc) IRoutes
- type IRouter
- type IRoutes
- type MethodTrees
- type RouteInfo
- type RouterGroup
- func (group *RouterGroup) Any(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) AnyEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
- func (group *RouterGroup) BasePath() string
- func (group *RouterGroup) DELETE(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) DELETEEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
- func (group *RouterGroup) GET(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) GETEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
- func (group *RouterGroup) Group(relativePath string, handlers ...app.HandlerFunc) *RouterGroup
- func (group *RouterGroup) HEAD(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) HEADEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
- func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) HandleEX(httpMethod, relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
- func (group *RouterGroup) OPTIONS(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) PATCH(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) POST(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) POSTEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
- func (group *RouterGroup) PUT(relativePath string, handlers ...app.HandlerFunc) IRoutes
- func (group *RouterGroup) PUTEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
- func (group *RouterGroup) Static(relativePath, root string) IRoutes
- func (group *RouterGroup) StaticFS(relativePath string, fs *app.FS) IRoutes
- func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes
- func (group *RouterGroup) Use(middleware ...app.HandlerFunc) IRoutes
- type RoutesInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetTransporterName
deprecated
func GetTransporterName() (tName string)
Deprecated: This only get the global default transporter - may not be the real one used by the engine. Use engine.GetTransporterName for the real transporter used.
func SetTransporter ¶
func SetTransporter(transporter func(options *config.Options) network.Transporter)
SetTransporter only sets the global default value for the transporter. Use WithTransporter during engine creation to set the transporter for the engine.
Types ¶
type CtxCallback ¶
type CtxErrCallback ¶
type Engine ¶
type Engine struct { // engine name Name string // route RouterGroup // NoHijackConnPool will control whether invite pool to acquire/release the hijackConn or not. // If it is difficult to guarantee that hijackConn will not be closed repeatedly, set it to true. NoHijackConnPool bool // KeepHijackedConns is an opt-in disable of connection // close by hertz after connections' HijackHandler returns. // This allows to save goroutines, e.g. when hertz used to upgrade // http connections to WS and connection goes to another handler, // which will close it when needed. KeepHijackedConns bool // Function to handle panics recovered from http handlers. // It should be used to generate an error page and return the http error code // 500 (Internal Server Error). // The handler can be used to keep your server from crashing because of // unrecovered panics. PanicHandler app.HandlerFunc // ContinueHandler is called after receiving the Expect 100 Continue Header // // https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.2.3 // https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.1.1 // Using ContinueHandler a server can make decisioning on whether or not // to read a potentially large request body based on the headers // // The default is to automatically read request bodies of Expect 100 Continue requests // like they are normal requests ContinueHandler func(header *protocol.RequestHeader) bool // Hook functions get triggered sequentially when engine start OnRun []CtxErrCallback // Hook functions get triggered simultaneously when engine shutdown OnShutdown []CtxCallback // contains filtered or unexported fields }
func (*Engine) AddProtocol ¶
func (*Engine) GetCtxPool ¶
func (*Engine) GetOptions ¶
func (*Engine) GetServerName ¶
func (*Engine) GetTracer ¶
func (engine *Engine) GetTracer() tracer.Controller
func (*Engine) GetTransporterName ¶
func (*Engine) HijackConnHandle ¶
func (engine *Engine) HijackConnHandle(c network.Conn, h app.HijackHandler)
func (*Engine) IsStreamRequestBody ¶
func (*Engine) IsTraceEnable ¶
func (*Engine) LoadHTMLFiles ¶
LoadHTMLFiles loads a slice of HTML files and associates the result with HTML renderer.
func (*Engine) LoadHTMLGlob ¶
LoadHTMLGlob loads HTML files identified by glob pattern and associates the result with HTML renderer.
func (*Engine) MarkAsRunning ¶
MarkAsRunning will mark the status of the hertz engine as "running". Warning: do not call this method by yourself, unless you know what you are doing.
func (*Engine) NewContext ¶
func (engine *Engine) NewContext() *app.RequestContext
NewContext make a pure RequestContext without any http request/response information
Set the Request filed before use it for handlers
func (*Engine) NoMethod ¶
func (engine *Engine) NoMethod(handlers ...app.HandlerFunc)
NoMethod sets the handlers called when the HTTP method does not match.
func (*Engine) NoRoute ¶
func (engine *Engine) NoRoute(handlers ...app.HandlerFunc)
NoRoute adds handlers for NoRoute. It returns a 404 code by default.
func (*Engine) PrintRoute ¶
func (*Engine) Routes ¶
func (engine *Engine) Routes() (routes RoutesInfo)
Routes returns a slice of registered routes, including some useful information, such as: the http method, path and the handler name.
func (*Engine) ServeHTTP ¶
func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext)
ServeHTTP makes the router implement the Handler interface.
func (*Engine) ServeStream ¶
func (*Engine) SetAltHeader ¶
SetAltHeader sets the value of "Alt-Svc" header for protocols other than targetProtocol.
func (*Engine) SetAutoReloadHTMLTemplate ¶
SetAutoReloadHTMLTemplate associate a template with HTML renderer.
func (*Engine) SetClientIPFunc ¶
func (*Engine) SetFormValueFunc ¶
func (engine *Engine) SetFormValueFunc(f app.FormValueFunc)
func (*Engine) SetFuncMap ¶
SetFuncMap sets the funcMap used for template.funcMap.
func (*Engine) SetHTMLTemplate ¶
SetHTMLTemplate associate a template with HTML renderer.
func (*Engine) Shutdown ¶
Shutdown starts the server's graceful exit by next steps:
- Trigger OnShutdown hooks concurrently and wait them until wait timeout or finish
- Close the net listener, which means new connection won't be accepted
- Wait all connections get closed: One connection gets closed after reaching out the shorter time of processing one request (in hand or next incoming), idleTimeout or ExitWaitTime
- Exit
func (*Engine) Use ¶
func (engine *Engine) Use(middleware ...app.HandlerFunc) IRoutes
Use attaches a global middleware to the router. ie. the middleware attached though Use() will be included in the handlers chain for every single request. Even 404, 405, static files...
For example, this is the right place for a logger or error management middleware.
type IRouter ¶
type IRouter interface { IRoutes Group(string, ...app.HandlerFunc) *RouterGroup }
IRouter defines all router handle interface includes single and group router.
type IRoutes ¶
type IRoutes interface { Use(...app.HandlerFunc) IRoutes Handle(string, string, ...app.HandlerFunc) IRoutes Any(string, ...app.HandlerFunc) IRoutes GET(string, ...app.HandlerFunc) IRoutes POST(string, ...app.HandlerFunc) IRoutes DELETE(string, ...app.HandlerFunc) IRoutes PATCH(string, ...app.HandlerFunc) IRoutes PUT(string, ...app.HandlerFunc) IRoutes OPTIONS(string, ...app.HandlerFunc) IRoutes HEAD(string, ...app.HandlerFunc) IRoutes StaticFile(string, string) IRoutes Static(string, string) IRoutes StaticFS(string, *app.FS) IRoutes }
IRoutes defines all router handle interface.
type MethodTrees ¶
type MethodTrees []*router
type RouteInfo ¶
type RouteInfo struct { Method string Path string Handler string HandlerFunc app.HandlerFunc }
RouteInfo represents a request route's specification which contains method and path and its handler.
type RouterGroup ¶
type RouterGroup struct { Handlers app.HandlersChain // contains filtered or unexported fields }
RouterGroup is used internally to configure router, a RouterGroup is associated with a prefix and an array of handlers (middleware).
func (*RouterGroup) Any ¶
func (group *RouterGroup) Any(relativePath string, handlers ...app.HandlerFunc) IRoutes
Any registers a route that matches all the HTTP methods. GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (*RouterGroup) AnyEX ¶
func (group *RouterGroup) AnyEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
AnyEX adds a handlerName param. When handler is decorated or handler is an anonymous function, Hertz cannot get handler name directly. In this case, pass handlerName explicitly.
func (*RouterGroup) BasePath ¶
func (group *RouterGroup) BasePath() string
BasePath returns the base path of router group. For example, if v := router.Group("/rest/n/v1/api"), v.BasePath() is "/rest/n/v1/api".
func (*RouterGroup) DELETE ¶
func (group *RouterGroup) DELETE(relativePath string, handlers ...app.HandlerFunc) IRoutes
DELETE is a shortcut for router.Handle("DELETE", path, handle).
func (*RouterGroup) DELETEEX ¶
func (group *RouterGroup) DELETEEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
DELETEEX adds a handlerName param. When handler is decorated or handler is an anonymous function, Hertz cannot get handler name directly. In this case, pass handlerName explicitly.
func (*RouterGroup) GET ¶
func (group *RouterGroup) GET(relativePath string, handlers ...app.HandlerFunc) IRoutes
GET is a shortcut for router.Handle("GET", path, handle).
func (*RouterGroup) GETEX ¶
func (group *RouterGroup) GETEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
GETEX adds a handlerName param. When handler is decorated or handler is an anonymous function, Hertz cannot get handler name directly. In this case, pass handlerName explicitly.
func (*RouterGroup) Group ¶
func (group *RouterGroup) Group(relativePath string, handlers ...app.HandlerFunc) *RouterGroup
Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix. For example, all the routes that use a common middleware for authorization could be grouped.
func (*RouterGroup) HEAD ¶
func (group *RouterGroup) HEAD(relativePath string, handlers ...app.HandlerFunc) IRoutes
HEAD is a shortcut for router.Handle("HEAD", path, handle).
func (*RouterGroup) HEADEX ¶
func (group *RouterGroup) HEADEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
HEADEX adds a handlerName param. When handler is decorated or handler is an anonymous function, Hertz cannot get handler name directly. In this case, pass handlerName explicitly.
func (*RouterGroup) Handle ¶
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...app.HandlerFunc) IRoutes
Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. See the example code in GitHub.
For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).
func (*RouterGroup) HandleEX ¶
func (group *RouterGroup) HandleEX(httpMethod, relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
HandleEX adds a handlerName param. When handler is decorated or handler is an anonymous function, Hertz cannot get handler name directly. In this case, pass handlerName explicitly.
func (*RouterGroup) OPTIONS ¶
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...app.HandlerFunc) IRoutes
OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle).
func (*RouterGroup) PATCH ¶
func (group *RouterGroup) PATCH(relativePath string, handlers ...app.HandlerFunc) IRoutes
PATCH is a shortcut for router.Handle("PATCH", path, handle).
func (*RouterGroup) POST ¶
func (group *RouterGroup) POST(relativePath string, handlers ...app.HandlerFunc) IRoutes
POST is a shortcut for router.Handle("POST", path, handle).
func (*RouterGroup) POSTEX ¶
func (group *RouterGroup) POSTEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
POSTEX adds a handlerName param. When handler is decorated or handler is an anonymous function, Hertz cannot get handler name directly. In this case, pass handlerName explicitly.
func (*RouterGroup) PUT ¶
func (group *RouterGroup) PUT(relativePath string, handlers ...app.HandlerFunc) IRoutes
PUT is a shortcut for router.Handle("PUT", path, handle).
func (*RouterGroup) PUTEX ¶
func (group *RouterGroup) PUTEX(relativePath string, handler app.HandlerFunc, handlerName string) IRoutes
PUTEX adds a handlerName param. When handler is decorated or handler is an anonymous function, Hertz cannot get handler name directly. In this case, pass handlerName explicitly.
func (*RouterGroup) Static ¶
func (group *RouterGroup) Static(relativePath, root string) IRoutes
Static serves files from the given file system root. To use the operating system's file system implementation, use :
router.Static("/static", "/var/www")
func (*RouterGroup) StaticFS ¶
func (group *RouterGroup) StaticFS(relativePath string, fs *app.FS) IRoutes
StaticFS works just like `Static()` but a custom `FS` can be used instead.
func (*RouterGroup) StaticFile ¶
func (group *RouterGroup) StaticFile(relativePath, filepath string) IRoutes
StaticFile registers a single route in order to Serve a single file of the local filesystem. router.StaticFile("favicon.ico", "./resources/favicon.ico")
func (*RouterGroup) Use ¶
func (group *RouterGroup) Use(middleware ...app.HandlerFunc) IRoutes
Use adds middleware to the group, see example code in GitHub.