Documentation ¶
Index ¶
- Variables
- func CleanPath(p string) string
- type AfterFunction
- type BeforeFunction
- type Endpoint
- type ErrorFunction
- type Handle
- type Handler
- type HandlerOption
- type Middleware
- type Param
- type Params
- type RequestFunction
- type ResponseFunction
- type Router
- func (r *Router) DELETE(path string, handle Handle)
- func (r *Router) GET(path string, handle Handle)
- func (r *Router) HEAD(path string, handle Handle)
- func (r *Router) Handle(method, path string, handle Handle)
- func (r *Router) Handler(method, path string, handler http.Handler)
- func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)
- func (r *Router) Lookup(method, path string) (Handle, Params, bool)
- func (r *Router) OPTIONS(path string, handle Handle)
- func (r *Router) PATCH(path string, handle Handle)
- func (r *Router) POST(path string, handle Handle)
- func (r *Router) PUT(path string, handle Handle)
- func (r *Router) ServeFiles(path string, root http.FileSystem)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
Constants ¶
This section is empty.
Variables ¶
var MatchedRoutePathParam = "$matchedRoutePath"
MatchedRoutePathParam is the Param name under which the path of the matched route is stored, if Router.SaveMatchedRoutePath is set.
var ParamsKey = paramsKey{}
ParamsKey is the request context key under which URL params are stored.
Functions ¶
Types ¶
type AfterFunction ¶
AfterFunction 请求后方法,请求后处理
type BeforeFunction ¶
BeforeFunction 请求前方法,请求前处理
type ErrorFunction ¶
type ErrorFunction func(ctx context.Context, w http.ResponseWriter, err error)
ErrorFunction 错误方法,错误信息
type Handle ¶ added in v0.0.3
type Handle func(http.ResponseWriter, *http.Request, Params)
Handle is a function that can be registered to a route to handle HTTP requests. Like http.HandlerFunc, but has a third parameter for the values of wildcards (path variables).
func WarpHandler ¶ added in v0.0.3
WarpHandler 包装 http.Handler 到 Handle
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler 处理器
func NewHandler ¶
func NewHandler(endpoint Endpoint, request RequestFunction, response ResponseFunction, option ...HandlerOption) (handler *Handler)
NewHandler 新建处理器
type HandlerOption ¶
type HandlerOption func(handler *Handler)
HandlerOption 处理器操作
func AfterOption ¶
func AfterOption(after ...AfterFunction) (handler HandlerOption)
AfterOption 处理后操作
func BeforeOption ¶
func BeforeOption(before ...BeforeFunction) (handler HandlerOption)
BeforeOption 处理前操作
type Middleware ¶
Middleware 中间件,端点中间件调用
type Params ¶ added in v0.0.3
type Params []Param
Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.
func ParamsFromContext ¶ added in v0.0.3
ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.
func (Params) ByName ¶ added in v0.0.3
ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.
func (Params) MatchedRoutePath ¶ added in v0.0.3
MatchedRoutePath retrieves the path of the matched route. Router.SaveMatchedRoutePath must have been enabled when the respective handler was added, otherwise this function always returns an empty string.
type RequestFunction ¶
RequestFunction 请求方法,请求数据解析
type ResponseFunction ¶
type ResponseFunction func(ctx context.Context, w http.ResponseWriter, response interface{}) (err error)
ResponseFunction 响应方法,响应数据解析
type Router ¶
type Router struct { // If enabled, adds the matched route path onto the http.Request context // before invoking the handler. // The matched route path is only added to handlers of routes that were // registered when this option was enabled. SaveMatchedRoutePath bool // Enables automatic redirection if the current route can't be matched but a // handler for the path with (without) the trailing slash exists. // For example if /foo/ is requested but a route only exists for /foo, the // client is redirected to /foo with http status code 301 for GET requests // and 308 for all other request methods. RedirectTrailingSlash bool // If enabled, the router tries to fix the current request path, if no // handle is registered for it. // First superfluous path elements like ../ or // are removed. // Afterwards the router does a case-insensitive lookup of the cleaned path. // If a handle can be found for this route, the router makes a redirection // to the corrected path with status code 301 for GET requests and 308 for // all other request methods. // For example /FOO and /..//Foo could be redirected to /foo. // RedirectTrailingSlash is independent of this option. RedirectFixedPath bool // If enabled, the router checks if another method is allowed for the // current route, if the current request can not be routed. // If this is the case, the request is answered with 'Method Not Allowed' // and HTTP status code 405. // If no other Method is allowed, the request is delegated to the NotFound // handler. HandleMethodNotAllowed bool // If enabled, the router automatically replies to `OPTIONS` requests. // Custom OPTIONS handlers take priority over automatic replies. HandleOPTIONS bool // An optional http.Handler that is called on automatic OPTIONS requests. // The handler is only called if HandleOPTIONS is true and no OPTIONS // handler for the specific path was set. // The "Allowed" header is set before calling the handler. GlobalOPTIONS http.Handler // Configurable http.Handler which is called when no matching route is // found. If it is not set, http.NotFound is used. NotFound http.Handler // Configurable http.Handler which is called when a request // cannot be routed and HandleMethodNotAllowed is true. // If it is not set, http.Error with http.StatusMethodNotAllowed is used. // The "Allow" header with allowed request methods is set before the handler // is called. MethodNotAllowed http.Handler // 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 // uncovered panics. PanicHandler func(http.ResponseWriter, *http.Request, interface{}) // contains filtered or unexported fields }
Router is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes
func NewRouter ¶
func NewRouter() *Router
NewRouter returns a new initialized Router. Path auto-correction, including trailing slashes, is enabled by default.
func (*Router) DELETE ¶ added in v0.0.3
DELETE is a shortcut for router.Handle(http.MethodDelete, path, handle)
func (*Router) GET ¶ added in v0.0.3
GET is a shortcut for router.Handle(http.MethodGet, path, handle)
func (*Router) HEAD ¶ added in v0.0.3
HEAD is a shortcut for router.Handle(http.MethodHead, path, handle)
func (*Router) Handle ¶ added in v0.0.3
Handle registers a new request handle with the given path and method.
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 (*Router) Handler ¶
Handler is an adapter which allows the usage of a http.Handler as a request handle. The Params are available in the request context under ParamsKey.
func (*Router) HandlerFunc ¶ added in v0.0.3
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)
HandlerFunc is an adapter which allows the usage of a http.HandlerFunc as a request handle.
func (*Router) Lookup ¶ added in v0.0.3
Lookup allows the manual lookup of a method + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handle function and the path parameter values. Otherwise, the third return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.
func (*Router) OPTIONS ¶ added in v0.0.3
OPTIONS is a shortcut for router.Handle(http.MethodOptions, path, handle)
func (*Router) PATCH ¶ added in v0.0.3
PATCH is a shortcut for router.Handle(http.MethodPatch, path, handle)
func (*Router) POST ¶ added in v0.0.3
POST is a shortcut for router.Handle(http.MethodPost, path, handle)
func (*Router) PUT ¶ added in v0.0.3
PUT is a shortcut for router.Handle(http.MethodPut, path, handle)
func (*Router) ServeFiles ¶ added in v0.0.3
func (r *Router) ServeFiles(path string, root http.FileSystem)
ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/et" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use http.Dir:
router.ServeFiles("/src/*filepath", http.Dir("/var/www"))