Documentation
¶
Overview ¶
Package fox provides a lightweight web framework for building web applications.
Index ¶
- Constants
- Variables
- func IsDebugging() bool
- func IsValidHandlerFunc(handler HandlerFunc) bool
- func Logger(config ...LoggerConfig) gin.HandlerFunc
- func Mode() string
- func NewXResponseTimer(key ...string) gin.HandlerFunc
- func SetMode(value string)
- type Context
- func (c *Context) Copy() *Context
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Next()
- func (c *Context) RequestBody() (body []byte, err error)
- func (c *Context) TraceID() string
- func (c *Context) Value(key any) any
- type DefaultValidator
- type DomainEngine
- type Engine
- func (engine *Engine) CORS(config cors.Config)
- func (engine *Engine) Load(f RouterConfigFunc, fs ...embed.FS)
- func (engine *Engine) NoMethod(handlers ...HandlerFunc)
- func (engine *Engine) NoRoute(handlers ...HandlerFunc)
- func (engine *Engine) NotFound(handlers ...HandlerFunc)
- func (engine *Engine) Use(middleware ...HandlerFunc)
- type HandlerFunc
- type HandlersChain
- type IsValider
- type LoggerConfig
- type RenderErrorFunc
- type RouterConfigFunc
- type RouterGroup
- func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup
- func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) gin.IRoutes
- func (group *RouterGroup) Use(middleware ...HandlerFunc) gin.IRoutes
- type StatusCoder
- type XResponseTimer
Constants ¶
const ( // DebugMode indicates gin mode is debug. DebugMode = gin.DebugMode // ReleaseMode indicates gin mode is release. ReleaseMode = gin.ReleaseMode // TestMode indicates gin mode is test. TestMode = gin.TestMode )
Variables ¶
var DefaultBinder binding.Binding = binding.JSON
DefaultBinder default binder
var DefaultErrorWriter io.Writer = os.Stderr
DefaultErrorWriter is the default io.Writer used by Gin to debug errors.
var DefaultWriter io.Writer = os.Stdout
DefaultWriter is the default io.Writer used by Gin for debug output and middleware output like Logger() or Recovery(). Note that both Logger and Recovery provides custom ways to configure their output io.Writer. To support coloring in Windows use:
import "github.com/mattn/go-colorable" gin.DefaultWriter = colorable.NewColorableStdout()
var ErrBindNonPointerValue = errors.New("can not bind to non-pointer value")
ErrBindNonPointerValue is required bind pointer
var ErrInvalidHandlerType = "invalid handler type: %s\n" +
"handler signature: %s\n" +
"Supported handler types:\n" +
"1. func()\n" +
"2. func(ctx *Context) T\n" +
"3. func(ctx *Context) (T, error)\n" +
"4. func(ctx *Context, args S) T\n" +
"5. func(ctx *Context, args S) (T, error)\n" +
"Where:\n" +
"- S can be struct or map type, S will be auto binding from request body\n" +
"- T can be any type, T will be auto render to response body\n" +
"- error can be any type that implements error interface"
ErrInvalidHandlerType is the error message for invalid handler type.
var LoggerContextKey = "_fox-goinc/fox/logger/context/key"
LoggerContextKey logger save in gin context
var Query = &queryBinding{}
Query binder
var Recovery = gin.Recovery
var Validate = validator.New()
Validate global validator
Functions ¶
func IsDebugging ¶
func IsDebugging() bool
IsDebugging returns true if the framework is running in debug mode. Use SetMode(gin.ReleaseMode) to disable debug mode.
func IsValidHandlerFunc ¶ added in v0.0.3
func IsValidHandlerFunc(handler HandlerFunc) bool
IsValidHandlerFunc checks if the handler matches the HandlerFunc type requirements.
func NewXResponseTimer ¶
func NewXResponseTimer(key ...string) gin.HandlerFunc
NewXResponseTimer x-response-time middleware
Types ¶
type Context ¶
type Context struct {
*gin.Context
Logger logger.Logger
// Request is the http request copy from gin.Context.
Request *http.Request
// contains filtered or unexported fields
}
Context with engine
func (*Context) RequestBody ¶
RequestBody return request body bytes see c.ShouldBindBodyWith
type DefaultValidator ¶
type DefaultValidator struct {
// contains filtered or unexported fields
}
DefaultValidator is the default implementation of Validator.
func (*DefaultValidator) ValidateStruct ¶
func (v *DefaultValidator) ValidateStruct(obj any) error
ValidateStruct check struct
type DomainEngine ¶
type DomainEngine struct {
*Engine
GetEngine func() *Engine
// contains filtered or unexported fields
}
DomainEngine subdomain engine
func NewDefaultDomainEngine ¶
func NewDefaultDomainEngine() *DomainEngine
NewDefaultDomainEngine new default domain engine
func NewDomainEngine ¶
func NewDomainEngine(get ...func() *Engine) *DomainEngine
NewDomainEngine new domain engine
func (*DomainEngine) Domain ¶
func (engine *DomainEngine) Domain(name string, engineFunc func(subEngine *Engine))
Domain add domain handler
func (*DomainEngine) DomainRegexp ¶
func (engine *DomainEngine) DomainRegexp(name string, engineFunc func(subEngine *Engine))
DomainRegexp add domain handler
func (*DomainEngine) ServeHTTP ¶
func (engine *DomainEngine) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP conforms to the http.Handler interface.
type Engine ¶
type Engine struct {
*gin.Engine
RouterGroup
// DefaultRenderErrorStatusCode is the default http status code used for automatic rendering
DefaultRenderErrorStatusCode int
RenderErrorFunc RenderErrorFunc
}
Engine for server.
func Default ¶
func Default() *Engine
Default return an Engine instance with Logger and Recovery middleware already attached.
func (*Engine) Load ¶
func (engine *Engine) Load(f RouterConfigFunc, fs ...embed.FS)
Load router config.
func (*Engine) NoMethod ¶ added in v0.0.3
func (engine *Engine) NoMethod(handlers ...HandlerFunc)
func (*Engine) NoRoute ¶ added in v0.0.3
func (engine *Engine) NoRoute(handlers ...HandlerFunc)
func (*Engine) NotFound ¶
func (engine *Engine) NotFound(handlers ...HandlerFunc)
NotFound adds handlers for NoRoute. It returns a 404 code by default.
type HandlerFunc ¶
type HandlerFunc any
HandlerFunc is a function that can be registered to a route to handle HTTP requests. Like http.HandlerFunc, but support auto binding and auto render.
Support handler types:
- func(){}
- func(ctx *Context) T { ... }
- func(ctx *Context) (T, error) { ... }
- func(ctx *Context, args S) T { ... }
- func(ctx *Context, args S) (T, error) { ... }
Where:
- S can be struct or map type, S will be auto binding from request body
- T can be any type, T will be auto render to response body
- error can be any type that implements error interface
type HandlersChain ¶
type HandlersChain []HandlerFunc
HandlersChain defines a HandlerFunc slice.
func (HandlersChain) Last ¶
func (c HandlersChain) Last() HandlerFunc
Last returns the last handler in the chain. i.e. the last handler is the main one.
type LoggerConfig ¶
type LoggerConfig struct {
// SkipPaths is an url path array which logs are not written.
// Optional.
SkipPaths []string
}
LoggerConfig defines the config for Logger middleware.
type RenderErrorFunc ¶ added in v0.0.3
type RouterConfigFunc ¶
RouterConfigFunc engine load router config func.
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
RouterGroup is gin.RouterGroup wrapper.
func (*RouterGroup) Any ¶
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc)
Any registers a route that matches all the HTTP methods. GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (*RouterGroup) DELETE ¶
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) gin.IRoutes
DELETE is a shortcut for router.Handle("DELETE", path, handle).
func (*RouterGroup) GET ¶
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) gin.IRoutes
GET is a shortcut for router.Handle("GET", path, handle).
func (*RouterGroup) Group ¶
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup
Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix. For example, all the routes that use a common middleware for authorization could be grouped.
func (*RouterGroup) HEAD ¶
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) gin.IRoutes
HEAD is a shortcut for router.Handle("HEAD", path, handle).
func (*RouterGroup) Handle ¶
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) gin.IRoutes
Handle gin.Handle wrapper.
func (*RouterGroup) OPTIONS ¶
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) gin.IRoutes
OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle).
func (*RouterGroup) PATCH ¶
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) gin.IRoutes
PATCH is a shortcut for router.Handle("PATCH", path, handle).
func (*RouterGroup) POST ¶
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) gin.IRoutes
POST is a shortcut for router.Handle("POST", path, handle).
func (*RouterGroup) PUT ¶
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) gin.IRoutes
PUT is a shortcut for router.Handle("PUT", path, handle).
func (*RouterGroup) Use ¶
func (group *RouterGroup) Use(middleware ...HandlerFunc) gin.IRoutes
Use adds middleware to the group, see example code in GitHub.
type StatusCoder ¶
type StatusCoder interface {
StatusCode() int
}
StatusCoder is a interface for http status code
type XResponseTimer ¶
type XResponseTimer struct {
gin.ResponseWriter
// contains filtered or unexported fields
}
XResponseTimer wrap gin response writer add start time
func (*XResponseTimer) Write ¶
func (w *XResponseTimer) Write(b []byte) (int, error)
Write implement http.ResponseWriter
func (*XResponseTimer) WriteHeader ¶
func (w *XResponseTimer) WriteHeader(statusCode int)
WriteHeader implement http.ResponseWriter
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
01-basic
command
|
|
|
02-binding
command
|
|
|
03-middleware
command
|
|
|
04-domain-routing
command
|
|
|
05-custom-validator
command
|
|
|
06-error-handling
command
|
|
|
07-logger-config
command
|
|
|
Package logger provides a customizable logging system for the application.
|
Package logger provides a customizable logging system for the application. |