fox

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 21 Imported by: 1

Documentation

Overview

Package fox provides a lightweight web framework for building web applications.

Index

Constants

View Source
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

View Source
var DefaultBinder binding.Binding = binding.JSON

DefaultBinder default binder

View Source
var DefaultErrorWriter io.Writer = os.Stderr

DefaultErrorWriter is the default io.Writer used by Gin to debug errors.

View Source
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()
View Source
var ErrBindNonPointerValue = errors.New("can not bind to non-pointer value")

ErrBindNonPointerValue is required bind pointer

View Source
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.

View Source
var LoggerContextKey = "_fox-goinc/fox/logger/context/key"

LoggerContextKey logger save in gin context

View Source
var Query = &queryBinding{}

Query binder

View Source
var Recovery = gin.Recovery
View Source
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 Logger

func Logger(config ...LoggerConfig) gin.HandlerFunc

Logger middleware

func Mode

func Mode() string

Mode returns current fox mode.

func NewXResponseTimer

func NewXResponseTimer(key ...string) gin.HandlerFunc

NewXResponseTimer x-response-time middleware

func SetMode

func SetMode(value string)

SetMode sets gin mode according to input string.

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) Copy added in v0.0.7

func (c *Context) Copy() *Context

func (*Context) Deadline added in v0.0.6

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done added in v0.0.6

func (c *Context) Done() <-chan struct{}

func (*Context) Err added in v0.0.6

func (c *Context) Err() error

func (*Context) Next added in v0.0.6

func (c *Context) Next()

func (*Context) RequestBody

func (c *Context) RequestBody() (body []byte, err error)

RequestBody return request body bytes see c.ShouldBindBodyWith

func (*Context) TraceID

func (c *Context) TraceID() string

TraceID return request id

func (*Context) Value added in v0.0.6

func (c *Context) Value(key any) any

type DefaultValidator

type DefaultValidator struct {
	// contains filtered or unexported fields
}

DefaultValidator is the default implementation of Validator.

func (*DefaultValidator) Engine

func (v *DefaultValidator) Engine() any

Engine return validate

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 New

func New() *Engine

New return engine instance.

func (*Engine) CORS

func (engine *Engine) CORS(config cors.Config)

CORS config.

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.

func (*Engine) Use

func (engine *Engine) Use(middleware ...HandlerFunc)

Use middleware.

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:

  1. func(){}
  2. func(ctx *Context) T { ... }
  3. func(ctx *Context) (T, error) { ... }
  4. func(ctx *Context, args S) T { ... }
  5. 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 IsValider

type IsValider interface {
	IsValid() error
}

IsValider interface

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 RenderErrorFunc func(ctx *Context, err error)

type RouterConfigFunc

type RouterConfigFunc func(router *Engine, embedFS ...embed.FS)

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

Directories

Path Synopsis
examples
01-basic command
02-binding command
03-middleware command
Package logger provides a customizable logging system for the application.
Package logger provides a customizable logging system for the application.

Jump to

Keyboard shortcuts

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