Documentation ¶
Overview ¶
Package echop is a wrapper of echo framework, which provides some useful features.
Package echop Group. Since methods and properties cannot be renamed, and to accommodate the habit of using Group methods to create new route groups, use nominal inheritance and override all methods, regardless of whether the method needs to add WrapHandlerFunc
Index ¶
- Variables
- func GetAppRunPath() (runPath string)
- func GetRequestID(c echo.Context) (id string)
- func InitLogger(l *zap.Logger)
- func LogDebug(msg string, fields ...zap.Field)
- func LogDebugWithContext(c echo.Context, msg string, fields ...zap.Field)
- func LogError(msg string, fields ...zap.Field)
- func LogErrorWithContext(c echo.Context, msg string, fields ...zap.Field)
- func LogInfo(msg string, fields ...zap.Field)
- func LogInfoWithContext(c echo.Context, msg string, fields ...zap.Field)
- func LogWarn(msg string, fields ...zap.Field)
- func LogWarnWithContext(c echo.Context, msg string, fields ...zap.Field)
- func OverrideContext() echo.MiddlewareFunc
- func WrapHandlerFunc(h HandlerFunc) echo.HandlerFunc
- type Context
- type CurrentUser
- type Echop
- func (ep *Echop) Add(method, path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) Any(path string, h HandlerFunc, m ...echo.MiddlewareFunc) []*echo.Route
- func (ep *Echop) CONNECT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) DELETE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) DefaultHTTPErrorHandler(err error, c echo.Context)
- func (ep *Echop) GET(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) Group(prefix string, m ...echo.MiddlewareFunc) *Group
- func (ep *Echop) HEAD(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) Match(methods []string, path string, h HandlerFunc, m ...echo.MiddlewareFunc) []*echo.Route
- func (ep *Echop) OPTIONS(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) PATCH(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) POST(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) PUT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) RouteNotFound(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) TRACE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (ep *Echop) URI(handler HandlerFunc, params ...interface{}) string
- func (ep *Echop) URL(handler HandlerFunc, params ...interface{}) string
- type Group
- func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) Any(path string, handler HandlerFunc, middleware ...echo.MiddlewareFunc) []*echo.Route
- func (g *Group) CONNECT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) DELETE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) File(path, file string)
- func (g *Group) FileFS(path, file string, filesystem fs.FS, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) GET(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) Group(prefix string, m ...echo.MiddlewareFunc) *Group
- func (g *Group) HEAD(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) Match(methods []string, path string, handler HandlerFunc, ...) []*echo.Route
- func (g *Group) OPTIONS(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) PATCH(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) POST(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) PUT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) RouteNotFound(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) Static(pathPrefix, fsRoot string)
- func (g *Group) StaticFS(pathPrefix string, filesystem fs.FS)
- func (g *Group) TRACE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
- func (g *Group) Use(middleware ...echo.MiddlewareFunc)
- type HandlerFunc
- type JSONResponse
- type LoggerFields
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ( // AppName is the name of the application, which will be used in log file name AppName = "echop" // RequestIDConfig middleware.RequestIDWithConfig RequestIDConfig = middleware.DefaultRequestIDConfig GetCurrentUser func(c echo.Context) (CurrentUser, error) = nil // RequestLoggerConfig middleware.RequestLoggerConfig RequestLoggerConfig = middleware.RequestLoggerConfig{ LogURI: true, LogStatus: true, LogMethod: true, LogRemoteIP: true, LogHost: true, LogLatency: true, LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error { var logFunc func(c echo.Context, msg string, fields ...zap.Field) if v.Status >= 500 { logFunc = LogErrorWithContext } else if v.Status >= 400 { logFunc = LogWarnWithContext } else { logFunc = LogInfoWithContext } logFields := []zap.Field{ zap.String("request_id", GetRequestID(c)), zap.String("method", v.URI), zap.String("method", v.Method), zap.String("remote_ip", v.RemoteIP), zap.String("host", v.Host), zap.String("latency", v.Latency.String()), } var currentUser CurrentUser if GetCurrentUser != nil { currentUser, _ = GetCurrentUser(c) } if currentUser.ID > 0 { logFields = append(logFields, zap.Uint64("operator_id", currentUser.ID)) } if currentUser.Name != "" { logFields = append(logFields, zap.String("operator_name", currentUser.Name)) } logFunc( c, "request", logFields..., ) return nil }, } DefaultResponseSuccessCode = 0 DefaultResponseFailCode = 1 DefaultResponseSuccessMsg = "ok" DefaultResponseFailMsg = "fail" RouteNotFound = "echo_route_not_found" Logger *zap.Logger )
Functions ¶
func GetAppRunPath ¶
func GetAppRunPath() (runPath string)
GetAppRunPath returns the path where the app is running.
func GetRequestID ¶ added in v0.1.1
func GetRequestID(c echo.Context) (id string)
func InitLogger ¶
InitLogger initializes the logger. default logger is initialized if l is nil.
func LogDebugWithContext ¶
func LogErrorWithContext ¶
func LogInfoWithContext ¶
func LogWarnWithContext ¶
func OverrideContext ¶
func OverrideContext() echo.MiddlewareFunc
func WrapHandlerFunc ¶
func WrapHandlerFunc(h HandlerFunc) echo.HandlerFunc
WrapHandlerFunc wraps HandlerFunc to echo.HandlerFunc
Types ¶
type Context ¶
type Context interface { echo.Context Json(httpCode int, resp JSONResponse) error // JsonSuccess is a shortcut of JsonSuccessWithCode with DefaultResponseSuccessCode JsonSuccess(i any, msg string) error // JsonFail is a shortcut of JsonFailWithCode with DefaultResponseFailCode JsonFail(i any, msg string) error // JsonSuccessWithCode is a shortcut of Json with http code is http.StatusOK and default msg is DefaultResponseSuccessMsg JsonSuccessWithCode(code any, data any, msg string) error // JsonFailWithCode is a shortcut of Json with http code is http.StatusOK and default msg is DefaultResponseFailMsg JsonFailWithCode(code any, data any, msg string) error // LogInfo is a shortcut of LogInfoWithContext LogInfo(msg string, fields ...zap.Field) // LogError is a shortcut of LogErrorWithContext LogError(msg string, fields ...zap.Field) // LogWarn is a shortcut of LogWarnWithContext LogWarn(msg string, fields ...zap.Field) // BindAndValidate is a shortcut of echo context Bind and Validate BindAndValidate(payload any) error }
Context is a wrapper of echo.Context
type CurrentUser ¶ added in v0.1.2
type CurrentUser struct { ID uint64 `json:"userID"` Name string `json:"name"` Nickname string `json:"nickname"` }
CurrentUser Get info about the user who initiated the request
type Echop ¶
type Echop struct {
*echo.Echo
}
func New ¶
func New() (ep *Echop)
New creates an instance of Echop.
It will set the following echo middlewares:
middleware.RequestIDWithConfig middleware.RequestLoggerWithConfig
func (*Echop) Add ¶
func (ep *Echop) Add(method, path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) Any ¶
func (ep *Echop) Any(path string, h HandlerFunc, m ...echo.MiddlewareFunc) []*echo.Route
func (*Echop) CONNECT ¶
func (ep *Echop) CONNECT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) DELETE ¶
func (ep *Echop) DELETE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) DefaultHTTPErrorHandler ¶
func (*Echop) GET ¶
func (ep *Echop) GET(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) HEAD ¶
func (ep *Echop) HEAD(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) Match ¶
func (ep *Echop) Match(methods []string, path string, h HandlerFunc, m ...echo.MiddlewareFunc) []*echo.Route
func (*Echop) OPTIONS ¶
func (ep *Echop) OPTIONS(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) PATCH ¶
func (ep *Echop) PATCH(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) POST ¶
func (ep *Echop) POST(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) PUT ¶
func (ep *Echop) PUT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) RouteNotFound ¶
func (ep *Echop) RouteNotFound(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) TRACE ¶
func (ep *Echop) TRACE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Echop) URI ¶
func (ep *Echop) URI(handler HandlerFunc, params ...interface{}) string
func (*Echop) URL ¶
func (ep *Echop) URL(handler HandlerFunc, params ...interface{}) string
type Group ¶
type Group struct {
G *echo.Group
}
func (*Group) Add ¶
func (g *Group) Add(method, path string, handler HandlerFunc, middleware ...echo.MiddlewareFunc) *echo.Route
func (*Group) Any ¶
func (g *Group) Any(path string, handler HandlerFunc, middleware ...echo.MiddlewareFunc) []*echo.Route
func (*Group) CONNECT ¶
func (g *Group) CONNECT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) DELETE ¶
func (g *Group) DELETE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) GET ¶
func (g *Group) GET(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) HEAD ¶
func (g *Group) HEAD(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) Match ¶
func (g *Group) Match(methods []string, path string, handler HandlerFunc, middleware ...echo.MiddlewareFunc) []*echo.Route
func (*Group) OPTIONS ¶
func (g *Group) OPTIONS(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) PATCH ¶
func (g *Group) PATCH(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) POST ¶
func (g *Group) POST(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) PUT ¶
func (g *Group) PUT(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) RouteNotFound ¶
func (g *Group) RouteNotFound(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
func (*Group) TRACE ¶
func (g *Group) TRACE(path string, h HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
type HandlerFunc ¶
type JSONResponse ¶
type JSONResponse struct { Code any `json:"code"` Message string `json:"message"` Data any `json:"data,omitempty"` }
JSONResponse is a standard response format