Documentation ¶
Overview ¶
Example (ParamInUrl) ¶
svr := NewServer() type GetUserReq struct { UserId int64 `json:"user_id"` Name string `json:"name"` } type GetUserResp struct { UserId int64 `json:"user_id"` Name string `json:"name"` } h := func(c context.Context, req *GetUserReq) (resp *GetUserResp, err error) { resp = new(GetUserResp) copier.Copy(resp, req) return } svr.POST("/api/users/:user_id/:name", NewStdHandlerF(h)) req := &GetUserReq{} httpRequest := httptest.NewRequest(http.MethodPost, "/api/users/123/jack", bytes.NewReader(base.AnyToJson(req))) httpRecoder := httptest.NewRecorder() svr.ServeHTTP(httpRecoder, httpRequest) result, _ := io.ReadAll(httpRecoder.Body) fmt.Print(string(result))
Output: {"code":0,"msg":"","event":"","data":{"user_id":123,"name":"jack"}}
Index ¶
- func Dir(root string, listDirectory bool) http.FileSystem
- func FindGlobalHandler[T Handler](s *Server) T
- type EmptyRequest
- type EmptyResponse
- type Handler
- type HandlerChain
- type HandlerFunc
- type HttpResp
- type LogHandler
- type MethodRouteNode
- type RequestInfo
- type ResponseWriter
- type RouteNode
- type Router
- type RouterBuilder
- func (b *RouterBuilder) GET(relativePath string, handlers ...Handler)
- func (b *RouterBuilder) GETF(relativePath string, handlers ...HandlerFunc)
- func (b *RouterBuilder) HEAD(relativePath string, handlers ...Handler)
- func (b *RouterBuilder) HEADF(relativePath string, handlers ...HandlerFunc)
- func (b *RouterBuilder) POST(relativePath string, handlers ...Handler)
- func (b *RouterBuilder) POSTF(relativePath string, handlers ...HandlerFunc)
- func (b *RouterBuilder) Static(relativePath string, root string)
- func (b *RouterBuilder) StaticFS(relativePath string, fs http.FileSystem)
- func (b *RouterBuilder) SubBuilder(relativePath string, handlers ...Handler) *RouterBuilder
- func (b *RouterBuilder) Use(middleware ...Handler)
- func (b *RouterBuilder) UseF(middleware ...HandlerFunc)
- type Server
- func (svr *Server) FindRoute(method, urlPath string, params map[string]string) *RouteNode
- func (svr *Server) NoRoute(handler ...Handler)
- func (svr *Server) Run(addr string)
- func (svr *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (svr *Server) ServeHTTPRouted(n *RouteNode, params map[string]string, w http.ResponseWriter, ...)
- func (svr *Server) Use(handlers ...Handler)
- func (svr *Server) UseF(handlers ...HandlerFunc)
- type StdHandler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dir ¶
func Dir(root string, listDirectory bool) http.FileSystem
Dir returns a http.FileSystem that can be used by http.FileServer(). It is used internally in router.Static(). if listDirectory == true, then it works the same as http.Dir() otherwise it returns a filesystem that prevents http.FileServer() to list the directory files.
func FindGlobalHandler ¶
Types ¶
type EmptyRequest ¶
type EmptyRequest struct { }
type EmptyResponse ¶
type EmptyResponse struct { }
type Handler ¶
type Handler = base.Interceptor[*RequestInfo]
func NewCodecHandler ¶
func NewCodecHandler() Handler
func NewRawHandler ¶
func NewRawHandler(h func(w http.ResponseWriter, req *http.Request)) Handler
func NewRecoverHandler ¶
func NewRecoverHandler() Handler
type HandlerChain ¶
type HandlerChain = base.Chain[*RequestInfo]
type HandlerFunc ¶
type HandlerFunc = base.InterceptorFunc[*RequestInfo]
type LogHandler ¶
type LogHandler struct {
// contains filtered or unexported fields
}
func NewLogHandler ¶
func NewLogHandler() *LogHandler
func (*LogHandler) Handle ¶
func (h *LogHandler) Handle(c context.Context, req any, info *RequestInfo, nexter base.Nexter[*RequestInfo]) (resp any, err error)
func (*LogHandler) SetRequestLogger ¶
func (h *LogHandler) SetRequestLogger(rl *plog.RequestLogger)
type MethodRouteNode ¶
type RequestInfo ¶
type RequestInfo struct { Request *http.Request Writer ResponseWriter UrlParams map[string]string PatternPath string ParsedRequest any }
func GetRequestInfo ¶
func GetRequestInfo(c context.Context) *RequestInfo
func NewRequestInfo ¶
func NewRequestInfo() *RequestInfo
func (*RequestInfo) UrlParam ¶
func (info *RequestInfo) UrlParam(key string) string
type ResponseWriter ¶
type ResponseWriter struct {
// contains filtered or unexported fields
}
func (*ResponseWriter) GetStatus ¶
func (w *ResponseWriter) GetStatus() int
func (*ResponseWriter) Header ¶
func (w *ResponseWriter) Header() http.Header
func (*ResponseWriter) Reset ¶
func (w *ResponseWriter) Reset(rawW http.ResponseWriter)
func (*ResponseWriter) WriteHeader ¶
func (w *ResponseWriter) WriteHeader(statusCode int)
type RouteNode ¶
type RouteNode struct { Handlers HandlerChain FullPath string // contains filtered or unexported fields }
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) ListAll ¶
func (r *Router) ListAll() []*MethodRouteNode
type RouterBuilder ¶
type RouterBuilder struct {
// contains filtered or unexported fields
}
func NewRouterBuilder ¶
func NewRouterBuilder() *RouterBuilder
func (*RouterBuilder) GET ¶
func (b *RouterBuilder) GET(relativePath string, handlers ...Handler)
func (*RouterBuilder) GETF ¶
func (b *RouterBuilder) GETF(relativePath string, handlers ...HandlerFunc)
func (*RouterBuilder) HEAD ¶
func (b *RouterBuilder) HEAD(relativePath string, handlers ...Handler)
func (*RouterBuilder) HEADF ¶
func (b *RouterBuilder) HEADF(relativePath string, handlers ...HandlerFunc)
func (*RouterBuilder) POST ¶
func (b *RouterBuilder) POST(relativePath string, handlers ...Handler)
func (*RouterBuilder) POSTF ¶
func (b *RouterBuilder) POSTF(relativePath string, handlers ...HandlerFunc)
func (*RouterBuilder) Static ¶
func (b *RouterBuilder) Static(relativePath string, root string)
func (*RouterBuilder) StaticFS ¶
func (b *RouterBuilder) StaticFS(relativePath string, fs http.FileSystem)
func (*RouterBuilder) SubBuilder ¶
func (b *RouterBuilder) SubBuilder(relativePath string, handlers ...Handler) *RouterBuilder
func (*RouterBuilder) Use ¶
func (b *RouterBuilder) Use(middleware ...Handler)
func (*RouterBuilder) UseF ¶
func (b *RouterBuilder) UseF(middleware ...HandlerFunc)
type Server ¶
type Server struct { *RouterBuilder // contains filtered or unexported fields }
func (*Server) ServeHTTPRouted ¶
func (*Server) UseF ¶
func (svr *Server) UseF(handlers ...HandlerFunc)
type StdHandler ¶
type StdHandler struct {
// contains filtered or unexported fields
}
func (StdHandler) Handle ¶
func (h StdHandler) Handle(c context.Context, req any, info *RequestInfo, nexter base.Nexter[*RequestInfo]) (any, error)
func (StdHandler) NewReq ¶
func (h StdHandler) NewReq() any
Click to show internal directories.
Click to hide internal directories.