Documentation ¶
Index ¶
- Constants
- Variables
- type Ctx
- func (ctx *Ctx) Context() context.Context
- func (ctx *Ctx) Form() url.Values
- func (ctx *Ctx) FormJson(stc interface{}) error
- func (ctx *Ctx) Header() http.Header
- func (ctx *Ctx) Param(name string) string
- func (ctx *Ctx) Request() *http.Request
- func (ctx *Ctx) SetResponseHandler(response IHttpResponse)
- func (ctx *Ctx) Value(key interface{}) interface{}
- func (ctx *Ctx) WithValue(key, val interface{})
- func (ctx *Ctx) Write(resp []byte) (int, error)
- func (ctx *Ctx) WriteHeader(code int)
- func (ctx *Ctx) WriteStr(resp string) (int, error)
- func (ctx *Ctx) Writer() http.ResponseWriter
- type HttpForwardHandler
- type HttpResponse
- func (resp *HttpResponse) GetCode() int64
- func (resp *HttpResponse) GetData() interface{}
- func (resp *HttpResponse) GetMsg() string
- func (resp *HttpResponse) Response(params ...interface{})
- func (resp *HttpResponse) SetCode(code int64)
- func (resp *HttpResponse) SetData(data interface{})
- func (resp *HttpResponse) SetMsg(msg string)
- type HttpResponseData
- type HttpRouteFunc
- type HttpServer
- func (s *HttpServer) DefaultRouteHealth() *HttpServer
- func (s *HttpServer) Name() string
- func (s *HttpServer) RouteDelete(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
- func (s *HttpServer) RouteFiles(path string, f http.FileSystem) *HttpServer
- func (s *HttpServer) RouteGet(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
- func (s *HttpServer) RouteHandler(method, path string, f http.Handler, middleware []Middleware) *HttpServer
- func (s *HttpServer) RouteOptions(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
- func (s *HttpServer) RoutePost(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
- func (s *HttpServer) RoutePut(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
- func (s *HttpServer) Router() *httprouter.Router
- func (s *HttpServer) Run() error
- func (s *HttpServer) Stop() error
- type IHttpResponse
- type Middleware
- type Router
- type ServerConfig
- type ServerOps
Constants ¶
View Source
const HealthUrl = "/health"
Variables ¶
View Source
var BaseMiddlewareArr = []Middleware{ GlobalMiddlewarePanic, GlobalMiddlewareAddRequestId(requestIdFiled), GlobalMiddlewareCorsMiddleware(nil), GlobalMiddlewareOptionRequest, }
View Source
var GlobalMiddlewareAddRequestId = func(requestIdField string) Middleware { return func(ctx *Ctx, next func(*Ctx)) { c := context.WithValue(ctx.request.Context(), requestIdField, utils.NewRequestId()) ctx.request = ctx.request.WithContext(c) next(ctx) } }
添加requestId
View Source
var GlobalMiddlewareCorsMiddleware = func(domainWhites []string) Middleware { return func(ctx *Ctx, next func(*Ctx)) { allow := false if origin := ctx.request.Header.Get("Origin"); origin != "" { if domainWhites == nil { allow = true } else { for _, white := range domainWhites { if white == origin { allow = true break } } } if allow { ctx.writer.Header().Set("Access-Control-Allow-Origin", origin) } } ctx.writer.Header().Set("Access-Control-Allow-Credentials", "true") ctx.writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") ctx.writer.Header().Set("Access-Control-Allow-Headers", "Content-type,Authorization,x-requested-with,Request-Service") next(ctx) } }
跨域资源请求
View Source
var GlobalMiddlewareQPSLimiter = func(r rate.Limit, b int) Middleware { limiter := rate.NewLimiter(r, b) return func(ctx *Ctx, next func(ctx *Ctx)) { if !limiter.Allow() { ctx.writer.WriteHeader(http.StatusNoContent) return } next(ctx) } }
qps限流
View Source
var HealthMiddlewareArr = []Middleware{ GlobalMiddlewarePanic, GlobalMiddlewareAddRequestId(requestIdFiled), GlobalMiddlewareCorsMiddleware(nil), GlobalMiddlewareOptionRequest, GlobalMiddlewareQPSLimiter(limiterR, limiterInit), }
View Source
var WithOpsRequestId = func(requestIdField string) ServerOps { return func(server *HttpServer) { server.config.requestId = requestIdField != "" server.config.requestIdField = requestIdField } }
Functions ¶
This section is empty.
Types ¶
type Ctx ¶
type Ctx struct { IHttpResponse // contains filtered or unexported fields }
func (*Ctx) SetResponseHandler ¶
func (ctx *Ctx) SetResponseHandler(response IHttpResponse)
func (*Ctx) WriteHeader ¶
func (*Ctx) Writer ¶
func (ctx *Ctx) Writer() http.ResponseWriter
type HttpForwardHandler ¶
type HttpForwardHandler struct {
// contains filtered or unexported fields
}
转发http的handler
func NewHttpForwardHandler ¶
func NewHttpForwardHandler(forward string, timeout time.Duration) *HttpForwardHandler
func (*HttpForwardHandler) AddForwardRule ¶
func (h *HttpForwardHandler) AddForwardRule(rule map[string]string)
func (*HttpForwardHandler) ServeHTTP ¶
func (h *HttpForwardHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type HttpResponse ¶
type HttpResponse struct {
// contains filtered or unexported fields
}
func (*HttpResponse) GetCode ¶
func (resp *HttpResponse) GetCode() int64
func (*HttpResponse) GetData ¶
func (resp *HttpResponse) GetData() interface{}
func (*HttpResponse) GetMsg ¶
func (resp *HttpResponse) GetMsg() string
func (*HttpResponse) Response ¶
func (resp *HttpResponse) Response(params ...interface{})
func (*HttpResponse) SetCode ¶
func (resp *HttpResponse) SetCode(code int64)
func (*HttpResponse) SetData ¶
func (resp *HttpResponse) SetData(data interface{})
func (*HttpResponse) SetMsg ¶
func (resp *HttpResponse) SetMsg(msg string)
type HttpResponseData ¶
type HttpResponseData struct { Code int64 `json:"code"` Data interface{} `json:"data"` Msg string `json:"msg"` RequestId string `json:"requestId"` }
func (*HttpResponseData) Encode ¶
func (d *HttpResponseData) Encode() []byte
type HttpRouteFunc ¶
type HttpRouteFunc = func(ctx *Ctx)
type HttpServer ¶
func NewHttpServer ¶
func NewHttpServer(name string, addr string, ops ...ServerOps) (*HttpServer, error)
func (*HttpServer) DefaultRouteHealth ¶
func (s *HttpServer) DefaultRouteHealth() *HttpServer
func (*HttpServer) Name ¶
func (s *HttpServer) Name() string
func (*HttpServer) RouteDelete ¶
func (s *HttpServer) RouteDelete(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
func (*HttpServer) RouteFiles ¶
func (s *HttpServer) RouteFiles(path string, f http.FileSystem) *HttpServer
func (*HttpServer) RouteGet ¶
func (s *HttpServer) RouteGet(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
func (*HttpServer) RouteHandler ¶
func (s *HttpServer) RouteHandler(method, path string, f http.Handler, middleware []Middleware) *HttpServer
func (*HttpServer) RouteOptions ¶
func (s *HttpServer) RouteOptions(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
func (*HttpServer) RoutePost ¶
func (s *HttpServer) RoutePost(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
func (*HttpServer) RoutePut ¶
func (s *HttpServer) RoutePut(path string, f HttpRouteFunc, middleware []Middleware) *HttpServer
func (*HttpServer) Router ¶
func (s *HttpServer) Router() *httprouter.Router
func (*HttpServer) Run ¶
func (s *HttpServer) Run() error
func (*HttpServer) Stop ¶
func (s *HttpServer) Stop() error
type IHttpResponse ¶
type IHttpResponse interface { SetCode(code int64) SetMsg(msg string) SetData(data interface{}) GetCode() int64 GetMsg() string GetData() interface{} Response(...interface{}) }
func NewHttpResponse ¶
func NewHttpResponse(writer http.ResponseWriter) IHttpResponse
type Middleware ¶
var DefaultResponseMiddleware Middleware = func(ctx *Ctx, next func(*Ctx)) { ctx.SetResponseHandler(NewHttpResponse(ctx.Writer())) next(ctx) }
response
var GlobalMiddlewareOptionRequest Middleware = func(ctx *Ctx, next func(*Ctx)) { if ctx.request.Method == "OPTIONS" { ctx.writer.WriteHeader(http.StatusNoContent) return } next(ctx) }
OPTION请求过滤
var GlobalMiddlewarePanic Middleware = func(ctx *Ctx, next func(*Ctx)) { defer func() { if err := recover(); err != nil { debug.PrintStack() ctx.WriteHeader(http.StatusInternalServerError) ctx.Response() return } }() next(ctx) }
panic处理
type Router ¶
type Router struct { Path string Method string RouteHandler HttpRouteFunc Middleware []Middleware HttpHandler http.Handler }
type ServerConfig ¶
type ServerConfig struct {
// contains filtered or unexported fields
}
type ServerOps ¶
type ServerOps = func(*HttpServer)
Click to show internal directories.
Click to hide internal directories.