tot

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2022 License: MIT Imports: 7 Imported by: 0

README

tot

介绍

参考go-kit与httprouter实现的go web开发工具

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MatchedRoutePathParam = "$matchedRoutePath"

MatchedRoutePathParam is the Param name under which the path of the matched route is stored, if Router.SaveMatchedRoutePath is set.

View Source
var ParamsKey = paramsKey{}

ParamsKey is the request context key under which URL params are stored.

Functions

func CleanPath added in v0.0.3

func CleanPath(p string) string

Types

type AfterFunction

type AfterFunction func(beforeCtx context.Context, w http.ResponseWriter) (ctx context.Context)

AfterFunction 请求后方法,请求后处理

type BeforeFunction

type BeforeFunction func(beforeCtx context.Context, r *http.Request) (ctx context.Context)

BeforeFunction 请求前方法,请求前处理

type Endpoint

type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)

Endpoint 端点,传入上下文与请求,返回响应与错误信息

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

func WarpHandler(handler http.Handler) Handle

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 新建处理器

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP 实现HTTP处理器

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 处理前操作

func ErrorOption

func ErrorOption(error ErrorFunction) (handler HandlerOption)

ErrorOption 错误操作

type Middleware

type Middleware func(Endpoint) Endpoint

Middleware 中间件,端点中间件调用

func Chain

func Chain(outer Middleware, others ...Middleware) Middleware

Chain 中间件链式调用

type Param added in v0.0.3

type Param struct {
	Key   string
	Value string
}

Param is a single URL parameter, consisting of a key and a value.

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

func ParamsFromContext(ctx context.Context) Params

ParamsFromContext pulls the URL parameters from a request context, or returns nil if none are present.

func (Params) ByName added in v0.0.3

func (ps Params) ByName(name string) string

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

func (ps Params) MatchedRoutePath() string

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

type RequestFunction func(ctx context.Context, r *http.Request) (request interface{}, err error)

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

func (r *Router) DELETE(path string, handle Handle)

DELETE is a shortcut for router.Handle(http.MethodDelete, path, handle)

func (*Router) GET added in v0.0.3

func (r *Router) GET(path string, handle Handle)

GET is a shortcut for router.Handle(http.MethodGet, path, handle)

func (*Router) HEAD added in v0.0.3

func (r *Router) HEAD(path string, handle Handle)

HEAD is a shortcut for router.Handle(http.MethodHead, path, handle)

func (*Router) Handle added in v0.0.3

func (r *Router) Handle(method, path string, handle Handle)

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

func (r *Router) Handler(method, path string, handler http.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

func (r *Router) Lookup(method, path string) (Handle, Params, bool)

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

func (r *Router) OPTIONS(path string, handle Handle)

OPTIONS is a shortcut for router.Handle(http.MethodOptions, path, handle)

func (*Router) PATCH added in v0.0.3

func (r *Router) PATCH(path string, handle Handle)

PATCH is a shortcut for router.Handle(http.MethodPatch, path, handle)

func (*Router) POST added in v0.0.3

func (r *Router) POST(path string, handle Handle)

POST is a shortcut for router.Handle(http.MethodPost, path, handle)

func (*Router) PUT added in v0.0.3

func (r *Router) PUT(path string, handle Handle)

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

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes the router implement the http.Handler interface.

Jump to

Keyboard shortcuts

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