route

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2023 License: Apache-2.0 Imports: 41 Imported by: 0

Documentation

Index

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 CtxCallback func(ctx context.Context)

type CtxErrCallback

type CtxErrCallback func(ctx context.Context) error

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 NewEngine

func NewEngine(opt *config.Options) *Engine

func (*Engine) AddProtocol

func (engine *Engine) AddProtocol(protocol string, factory interface{})

func (*Engine) Close

func (engine *Engine) Close() error

func (*Engine) Delims

func (engine *Engine) Delims(left, right string) *Engine

Delims sets template left and right delims and returns an Engine instance.

func (*Engine) GetCtxPool

func (engine *Engine) GetCtxPool() *sync.Pool

func (*Engine) GetOptions

func (engine *Engine) GetOptions() *config.Options

func (*Engine) GetServerName

func (engine *Engine) GetServerName() []byte

func (*Engine) GetTracer

func (engine *Engine) GetTracer() tracer.Controller

func (*Engine) GetTransporterName

func (engine *Engine) GetTransporterName() (tName string)

func (*Engine) HasServer

func (engine *Engine) HasServer(name string) bool

func (*Engine) HijackConnHandle

func (engine *Engine) HijackConnHandle(c network.Conn, h app.HijackHandler)

func (*Engine) Init

func (engine *Engine) Init() error

func (*Engine) IsRunning

func (engine *Engine) IsRunning() bool

func (*Engine) IsStreamRequestBody

func (engine *Engine) IsStreamRequestBody() bool

func (*Engine) IsTraceEnable

func (engine *Engine) IsTraceEnable() bool

func (*Engine) LoadHTMLFiles

func (engine *Engine) LoadHTMLFiles(files ...string)

LoadHTMLFiles loads a slice of HTML files and associates the result with HTML renderer.

func (*Engine) LoadHTMLGlob

func (engine *Engine) LoadHTMLGlob(pattern string)

LoadHTMLGlob loads HTML files identified by glob pattern and associates the result with HTML renderer.

func (*Engine) MarkAsRunning

func (engine *Engine) MarkAsRunning() (err error)

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 *Engine) PrintRoute(method string)

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) Run

func (engine *Engine) Run() (err error)

func (*Engine) Serve

func (engine *Engine) Serve(c context.Context, conn network.Conn) (err error)

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 *Engine) ServeStream(ctx context.Context, conn network.StreamConn) error

func (*Engine) SetAltHeader

func (engine *Engine) SetAltHeader(targetProtocol, altHeaderValue string)

SetAltHeader sets the value of "Alt-Svc" header for protocols other than targetProtocol.

func (*Engine) SetAutoReloadHTMLTemplate

func (engine *Engine) SetAutoReloadHTMLTemplate(tmpl *template.Template, files []string)

SetAutoReloadHTMLTemplate associate a template with HTML renderer.

func (*Engine) SetClientIPFunc

func (engine *Engine) SetClientIPFunc(f app.ClientIP)

func (*Engine) SetFormValueFunc

func (engine *Engine) SetFormValueFunc(f app.FormValueFunc)

func (*Engine) SetFuncMap

func (engine *Engine) SetFuncMap(funcMap template.FuncMap)

SetFuncMap sets the funcMap used for template.funcMap.

func (*Engine) SetHTMLTemplate

func (engine *Engine) SetHTMLTemplate(tmpl *template.Template)

SetHTMLTemplate associate a template with HTML renderer.

func (*Engine) Shutdown

func (engine *Engine) Shutdown(ctx context.Context) (err error)

Shutdown starts the server's graceful exit by next steps:

  1. Trigger OnShutdown hooks concurrently and wait them until wait timeout or finish
  2. Close the net listener, which means new connection won't be accepted
  3. 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
  4. 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

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.

type RoutesInfo

type RoutesInfo []RouteInfo

RoutesInfo defines a RouteInfo array.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL