evohttp

package
v0.0.0-...-0350bc7 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

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

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

func FindGlobalHandler[T Handler](s *Server) T

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

func NewStdHandlerF

func NewStdHandlerF[Req any, Resp any](f func(c context.Context, req *Req) (*Resp, error)) Handler

type HandlerChain

type HandlerChain = base.Chain[*RequestInfo]

type HandlerFunc

type HandlerFunc = base.InterceptorFunc[*RequestInfo]

type HttpResp

type HttpResp[T any] struct {
	Code  codes.Code `json:"code"`
	Msg   string     `json:"msg"`
	Event string     `json:"event"`
	Data  T          `json:"data"`
}

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 MethodRouteNode struct {
	HttpMethod string
	Node       *RouteNode
}

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

func (w *ResponseWriter) Write(p []byte) (int, error)

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 NewRouter

func NewRouter() *Router

func (*Router) Find

func (r *Router) Find(httpMethod, urlPath string, params map[string]string) *RouteNode

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 NewServer

func NewServer() *Server

func (*Server) FindRoute

func (svr *Server) FindRoute(method, urlPath string, params map[string]string) *RouteNode

func (*Server) NoRoute

func (svr *Server) NoRoute(handler ...Handler)

func (*Server) Run

func (svr *Server) Run(addr string)

func (*Server) ServeHTTP

func (svr *Server) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Server) ServeHTTPRouted

func (svr *Server) ServeHTTPRouted(n *RouteNode, params map[string]string, w http.ResponseWriter, req *http.Request)

func (*Server) Use

func (svr *Server) Use(handlers ...Handler)

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

Jump to

Keyboard shortcuts

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