Documentation ¶
Overview ¶
Package httpsvr ...
Package httpsvr ...
Package httpsvr ...
Package httpsvr ...
Package httpsvr ...
Package httpsvr ...
Package httpsvr ...
Package httpsvr ...
Index ¶
- Variables
- func GenExtraResponse(extData map[string]interface{}, code int, msg string) interface{}
- type APIResponseHeader
- type Context
- type ControllerOption
- func AddResponseHeader(fn func() http.Header) ControllerOption
- func SetControllerMarshalFunc(fn func(v interface{}, err idl.APIErr) ([]byte, error)) ControllerOption
- func SetControllerUnmarshalFunc(fn func(r *http.Request, req interface{}) error) ControllerOption
- func SetHandleTimeout(to int64) ControllerOption
- type HandlerFunc
- type Middleware
- type PHPResponseHeader
- type Response
- type Server
- func (s *Server) AddMiddleware(md Middleware)
- func (s *Server) AddRoute(method, path string, ctrl idl.IController, opts ...ControllerOption)
- func (s *Server) HandleFunc(method, path string, hd func(w http.ResponseWriter, r *http.Request))
- func (s *Server) Serve() error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) ServeTLS(certFile, keyFile string) error
- type ServerOption
- func EnableElasped(enable bool) ServerOption
- func EnableValidate(enable bool) ServerOption
- func SetHandleDefaultTimeout(to int64) ServerOption
- func SetLocalLanguage(lang string) ServerOption
- func SetServerMarshalFunc(marshalFunc func(v interface{}, err idl.APIErr) ([]byte, error)) ServerOption
- func SetServerReadTimeout(rt time.Duration) ServerOption
- func SetServerUnmarshalFunc(fn func(*http.Request, interface{}) error) ServerOption
- func SetServerWriteTimeout(wt time.Duration) ServerOption
- func WithDumpAccess(dump bool) ServerOption
- func WithDumpResponse(dump bool) ServerOption
- func WithLogger(log logger.Logger) ServerOption
- type TraceHeader
Constants ¶
This section is empty.
Variables ¶
var Access = func(ctx context.Context, r *http.Request, w http.ResponseWriter, next HandlerFunc) HandlerFunc { return func(ctx context.Context, r *http.Request, w http.ResponseWriter) { next(ctx, r, w) } }
Access 处理下一个回调方法
var Degrade = func(ctx context.Context, r *http.Request, w http.ResponseWriter, next HandlerFunc) HandlerFunc { return func(ctx context.Context, r *http.Request, w http.ResponseWriter) { } }
Degrade 降级
var ( // ErrEmptyBody body为空的错误消息 ErrEmptyBody = errors.New("empty request body") )
var Language = func(ctx context.Context, r *http.Request, w http.ResponseWriter, next HandlerFunc) HandlerFunc { return func(ctx context.Context, r *http.Request, w http.ResponseWriter) { r.ParseForm() lang := r.Form.Get(languageKey) if lang == "" { content := r.Header.Get(hintContentKey) v := make(map[string]interface{}) if err := json.Unmarshal([]byte(content), &v); err != nil { logger.Infof(ctx, logger.DLTagUndefined, "_msg=hint_content not json,hintcontent=%s", content) } if lh, ok := v[languageKey]; ok { lang = fmt.Sprint(lh) } } if lang == "" { logger.Infof(ctx, logger.DLTagUndefined, "_msg=not found language") lang = localLanguage } ctx = ctxutil.SetLang(ctx, lang) next(ctx, r, w) } }
Language ...
var Recovery = func(ctx context.Context, r *http.Request, w http.ResponseWriter, next HandlerFunc) HandlerFunc { return func(ctx context.Context, r *http.Request, w http.ResponseWriter) { defer func() { if err := recover(); err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("Server is busy.")) stack := make([]byte, 2048) stack = stack[:runtime.Stack(stack, false)] f := "PANIC: %s\n%s" logger.Errorf(ctx, logger.DLTagUndefined, f, err, stack) } }() if next != nil { next(ctx, r, w) } } }
Recovery 捕获panic的通用处理方法
var RecoveryWithMetric = func(ctx context.Context, r *http.Request, w http.ResponseWriter, next HandlerFunc) HandlerFunc { return func(ctx context.Context, r *http.Request, w http.ResponseWriter) { defer func() { if err := recover(); err != nil { metrics.Add("panic", 1) w.WriteHeader(http.StatusInternalServerError) w.Write([]byte("Server is busy.")) stack := make([]byte, 1024) stack = stack[:runtime.Stack(stack, false)] f := "PANIC: %s\n%s" logger.Errorf(ctx, logger.DLTagUndefined, f, err, stack) } }() if next != nil { next(ctx, r, w) } } }
RecoveryWithMetric 捕获panic,记录metric
Functions ¶
func GenExtraResponse ¶
GenExtraResponse 根据map产生响应
Types ¶
type APIResponseHeader ¶
APIResponseHeader api响应头格式
type Context ¶
type Context struct { Params httprouter.Params Req *http.Request TraceID string SpanID string ParentSpanID string // contains filtered or unexported fields }
Context 上下文结构,TraceID、SpanID、ResponseWriter、Request、Params等
func (*Context) FormValues ¶
FormValues 解析参数并格式化为map
func (*Context) GetRequestBody ¶
GetRequestBody 获取请求体
type ControllerOption ¶
type ControllerOption func(o *ctrlOption)
ControllerOption 定义ControllerOption类型
func AddResponseHeader ¶
func AddResponseHeader(fn func() http.Header) ControllerOption
AddResponseHeader ...
func SetControllerMarshalFunc ¶
func SetControllerMarshalFunc(fn func(v interface{}, err idl.APIErr) ([]byte, error)) ControllerOption
SetControllerMarshalFunc ...
func SetControllerUnmarshalFunc ¶
func SetControllerUnmarshalFunc(fn func(r *http.Request, req interface{}) error) ControllerOption
SetControllerUnmarshalFunc 设置某个Controller的Unmarshal方法
type HandlerFunc ¶
HandlerFunc 回调方法类型定义
type Middleware ¶
type Middleware func(ctx context.Context, r *http.Request, w http.ResponseWriter, next HandlerFunc) HandlerFunc
Middleware 定义Middleware类型
type PHPResponseHeader ¶
type PHPResponseHeader struct { Code interface{} `json:"errno"` Msg string `json:"errmsg"` }
PHPResponseHeader php响应头格式
func (*PHPResponseHeader) GetCode ¶
func (ph *PHPResponseHeader) GetCode() int
GetCode 获取PHPResponseHeader响应码
func (*PHPResponseHeader) GetMsg ¶
func (ph *PHPResponseHeader) GetMsg() string
GetMsg 获取PHPResponseHeader响应消息
type Response ¶
type Response struct { Code int `json:"errno"` Msg string `json:"errmsg"` Data interface{} `json:"data"` }
Response 响应结构体
func GenNotImplementResponse ¶
GenNotImplementResponse 产生404 响应
func GenResponse ¶
GenResponse 根据参数产生响应
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server 定义Server结构体
func (*Server) AddMiddleware ¶
func (s *Server) AddMiddleware(md Middleware)
AddMiddleware 添加middleware
func (*Server) AddRoute ¶
func (s *Server) AddRoute(method, path string, ctrl idl.IController, opts ...ControllerOption)
AddRoute 添加路由
func (*Server) HandleFunc ¶
type ServerOption ¶
type ServerOption func(o *option)
ServerOption 定义ServerOption类型
func SetHandleDefaultTimeout ¶
func SetHandleDefaultTimeout(to int64) ServerOption
SetHandleDefaultTimeout ...
func SetServerMarshalFunc ¶
func SetServerMarshalFunc(marshalFunc func(v interface{}, err idl.APIErr) ([]byte, error)) ServerOption
SetServerMarshalFunc ...
func SetServerReadTimeout ¶
func SetServerReadTimeout(rt time.Duration) ServerOption
SetServerReadTimeout ...
func SetServerUnmarshalFunc ¶
func SetServerUnmarshalFunc(fn func(*http.Request, interface{}) error) ServerOption
SetServerUnmarshalFunc ...
func SetServerWriteTimeout ¶
func SetServerWriteTimeout(wt time.Duration) ServerOption
SetServerWriteTimeout ...
func WithDumpResponse ¶
func WithDumpResponse(dump bool) ServerOption
WithDumpResponse dump response
type TraceHeader ¶
type TraceHeader struct {
// contains filtered or unexported fields
}
TraceHeader header中的traceID、spanID和parentSpanID
func NewTraceHeader ¶
func NewTraceHeader(r *http.Request) *TraceHeader
NewTraceHeader 创建TraceHeader
func (*TraceHeader) Encode ¶
func (th *TraceHeader) Encode() map[string]string
Encode 将traceID和spanID格式化为map
func (*TraceHeader) GetParentSpanID ¶
func (th *TraceHeader) GetParentSpanID() string
GetParentSpanID 从TraceHeader获取parentSpanID
func (*TraceHeader) GetSpanID ¶
func (th *TraceHeader) GetSpanID() string
GetSpanID 从TraceHeader获取spanID
func (*TraceHeader) GetTraceID ¶
func (th *TraceHeader) GetTraceID() string
GetTraceID 从TraceHeader获取traceID