Documentation
¶
Index ¶
- Variables
- func BindingValue(valueField reflect.Value, rawValue []string, tags reflect.StructTag) error
- func SetRecoveryHandler(handler func(ctx *Context, panicValue interface{}))
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(err *HTTPError)
- func (c *Context) AbortWithStatus(status int)
- func (c *Context) AbortWithStatusJSON(status int, data interface{})
- func (c *Context) BindingInput(input interface{}) error
- func (c *Context) ClientIP() string
- func (c *Context) GetPathParameterAsInt(key string) (int, error)
- func (c *Context) GetPathParameterAsString(key string) string
- func (c *Context) GetQueryInt(key string) (int, error)
- func (c *Context) GetQueryString(key string) string
- func (c *Context) GetQueryStrings(key string) []string
- func (c *Context) HTML(templatePath string, data interface{}) (err error)
- func (c *Context) InitSSE() error
- func (c *Context) JSON(data interface{}) (err error)
- func (c *Context) JSONError(err *HTTPError) error
- func (c *Context) JSONWithStatus(data interface{}, status int) (err error)
- func (c *Context) ParseJson(target interface{}) error
- func (c *Context) ParseXML(target interface{}) error
- func (c *Context) RawWriter() http.ResponseWriter
- func (c *Context) SendSSEData(data interface{}) error
- func (c *Context) SendSSEEvent(event string, data interface{}) error
- func (c *Context) StartTime() time.Time
- func (c *Context) StatusCode() int
- func (c *Context) UpgradeToWebSocket() (*websocket.Conn, error)
- func (c *Context) UpgradeToWebSocketWith(upgrader *websocket.Upgrader) (*websocket.Conn, error)
- func (c *Context) XML(data interface{}) (err error)
- func (c *Context) XMLWithStatus(data interface{}, status int) (err error)
- type Engine
- func (e *Engine) HTTPHandler() http.Handler
- func (e *Engine) RunAndListen(addr string) error
- func (e *Engine) RunAndListenTLS(addr, certFile, keyFile string) error
- func (e *Engine) Shutdown(ctx context.Context) error
- func (e *Engine) ShutdownWithTimeout(timeout time.Duration) error
- func (e *Engine) Stop() error
- func (e *Engine) UseCors(cors *cors.Cors)
- func (e *Engine) UseMiddleware(middleware Middleware)
- type FormFile
- type Group
- func (g *Group) AddHandler(pattern string, handler RequestHandler)
- func (g *Group) DELETE(pattern string, handler RequestHandler)
- func (g *Group) GET(pattern string, handler RequestHandler)
- func (g *Group) Group(prefix string, middlewares ...Middleware) *Group
- func (g *Group) METHODS(pattern string, methods []string, handler RequestHandler)
- func (g *Group) PATCH(pattern string, handler RequestHandler)
- func (g *Group) POST(pattern string, handler RequestHandler)
- func (g *Group) PUT(pattern string, handler RequestHandler)
- func (g *Group) UseMiddleware(middlewares ...Middleware)
- type HTMLView
- type HTTPError
- func ErrBadRequest(message string) *HTTPError
- func ErrForbidden(message string) *HTTPError
- func ErrInternal(message string) *HTTPError
- func ErrNotFound(message string) *HTTPError
- func ErrUnauthorized(message string) *HTTPError
- func NewHTTPError(statusCode int, message string) *HTTPError
- func NewHTTPErrorWithCode(statusCode, code int, message string) *HTTPError
- type JSON
- type JSONParser
- type JsonView
- type Middleware
- type Parser
- type RequestHandler
- type ResponseMiddleware
- type RouteInfo
- type Router
- func (r *Router) AddHandler(pattern string, handler RequestHandler)
- func (r *Router) DELETE(pattern string, handler RequestHandler)
- func (r *Router) GET(pattern string, handler RequestHandler)
- func (r *Router) Group(prefix string, middlewares ...Middleware) *Group
- func (r *Router) METHODS(pattern string, methods []string, handler RequestHandler)
- func (r *Router) MakeHandlerContext(writer http.ResponseWriter, request *http.Request, pattern string) *Context
- func (r *Router) PATCH(pattern string, handler RequestHandler)
- func (r *Router) POST(pattern string, handler RequestHandler)
- func (r *Router) PUT(pattern string, handler RequestHandler)
- func (r *Router) Prefix(pattern string, handler RequestHandler)
- func (r *Router) Routes() []RouteInfo
- func (r *Router) Static(pattern string, staticPath string)
- type RouterMapping
- type View
- type XML
- type XMLParser
- type XMLView
Constants ¶
This section is empty.
Variables ¶
var DefaultWebSocketUpgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r *http.Request) bool { return true }, }
DefaultWebSocketUpgrader is used by UpgradeToWebSocket. Replace or adjust CheckSameOrigin for stricter origin policies.
Functions ¶
func BindingValue ¶
func SetRecoveryHandler ¶
func SetRecoveryHandler(handler func(ctx *Context, panicValue interface{}))
SetRecoveryHandler replaces the built-in panic recovery handler. The default one logs the stack and renders a structured 500 error (or the panicked *HTTPError itself).
Types ¶
type Context ¶
type Context struct {
// Pattern matched url path
Pattern string
// The http.ResponseWriter object.
Writer http.ResponseWriter
// The http.Request object.
Request *http.Request
// Path params
Parameters map[string]string
// For middleware to store dara
Param map[string]interface{}
// contains filtered or unexported fields
}
Context for request
func (*Context) AbortWithError ¶
AbortWithError writes err via JSONError and aborts the chain.
func (*Context) AbortWithStatus ¶
AbortWithStatus writes the status code and aborts the chain.
func (*Context) AbortWithStatusJSON ¶
AbortWithStatusJSON writes data as JSON with the given status and aborts the chain.
func (*Context) BindingInput ¶
func (*Context) GetPathParameterAsInt ¶
get path parameter as int
func (*Context) GetPathParameterAsString ¶
get path parameter as string
func (*Context) GetQueryInt ¶
get query string as int
func (*Context) GetQueryString ¶
get query string from url
func (*Context) GetQueryStrings ¶
get query string as []string
func (*Context) InitSSE ¶
InitSSE writes the event-stream headers and performs the first flush so browsers open the stream immediately.
func (*Context) JSONError ¶
JSONError writes err as a structured JSON envelope:
{"error":{"code":400,"message":"..."}}
func (*Context) JSONWithStatus ¶
func (*Context) RawWriter ¶
func (c *Context) RawWriter() http.ResponseWriter
RawWriter exposes the original http.ResponseWriter before any framework wrapping. Needed when integration code must satisfy unexported interfaces of net/http (e.g. MaxBytesReader's 413 hook).
func (*Context) SendSSEData ¶
SendSSEData writes a default (unnamed) event with JSON data.
func (*Context) SendSSEEvent ¶
SendSSEEvent writes a named event with JSON encoded data.
func (*Context) StatusCode ¶
StatusCode returns the HTTP status code written to the response, or 0 when nothing has been written yet.
func (*Context) UpgradeToWebSocket ¶
UpgradeToWebSocket upgrades the current request to a WebSocket connection using DefaultWebSocketUpgrader.
func (*Context) UpgradeToWebSocketWith ¶
UpgradeToWebSocketWith upgrades the request with a custom upgrader.
func (*Context) XMLWithStatus ¶
type Engine ¶
type Engine struct {
Router *Router
Middlewares []Middleware
Logger *logrus.Logger
Cros *cors.Cors
// ReadHeaderTimeout guards against slowloris attacks.
// Zero means the default of 15 seconds.
ReadHeaderTimeout time.Duration
// ShutdownTimeout is the maximum time to wait for in-flight
// requests during graceful shutdown. Zero means 10 seconds.
ShutdownTimeout time.Duration
// contains filtered or unexported fields
}
func (*Engine) HTTPHandler ¶
HTTPHandler assembles the final root http.Handler (CORS included).
func (*Engine) RunAndListen ¶
func (*Engine) RunAndListenTLS ¶
RunAndListenTLS serves HTTPS. certFile/keyFile are PEM paths.
func (*Engine) Shutdown ¶
Shutdown gracefully drains in-flight connections. The context controls the deadline; callers should not wait forever.
func (*Engine) ShutdownWithTimeout ¶
ShutdownWithTimeout gracefully drains in-flight connections, aborting after the given timeout.
func (*Engine) Stop ¶
Stop gracefully shuts the server down using ShutdownTimeout (default 10s). It will never block indefinitely.
func (*Engine) UseMiddleware ¶
func (e *Engine) UseMiddleware(middleware Middleware)
UseMiddleware registers a global middleware. It takes effect for requests handled after the call.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a collection of routes sharing a common prefix and a common middleware chain. Groups can be nested; the effective chain is the concatenation of all ancestor chains (outermost first).
func (*Group) AddHandler ¶
func (g *Group) AddHandler(pattern string, handler RequestHandler)
func (*Group) DELETE ¶
func (g *Group) DELETE(pattern string, handler RequestHandler)
func (*Group) GET ¶
func (g *Group) GET(pattern string, handler RequestHandler)
func (*Group) Group ¶
func (g *Group) Group(prefix string, middlewares ...Middleware) *Group
Group creates a sub group with an extended prefix and additional middlewares.
func (*Group) METHODS ¶
func (g *Group) METHODS(pattern string, methods []string, handler RequestHandler)
func (*Group) PATCH ¶
func (g *Group) PATCH(pattern string, handler RequestHandler)
func (*Group) POST ¶
func (g *Group) POST(pattern string, handler RequestHandler)
func (*Group) PUT ¶
func (g *Group) PUT(pattern string, handler RequestHandler)
func (*Group) UseMiddleware ¶
func (g *Group) UseMiddleware(middlewares ...Middleware)
UseMiddleware appends middlewares to the group chain. They only apply to routes registered after this call.
type HTTPError ¶
type HTTPError struct {
// StatusCode is the HTTP status written to the response.
// It is not serialized into the body.
StatusCode int `json:"-"`
// Code is an application specific error code (optional).
Code int `json:"code,omitempty"`
// Message is a human readable description.
Message string `json:"message"`
}
HTTPError is a structured API error. Handlers may return it to the client via JSONError / AbortWithError, or simply panic with it — the built-in recovery renders it with its status code.
func ErrBadRequest ¶
func ErrForbidden ¶
func ErrInternal ¶
func ErrNotFound ¶
func ErrUnauthorized ¶
func NewHTTPError ¶
func NewHTTPErrorWithCode ¶
type JSONParser ¶
type JSONParser struct {
}
type Middleware ¶
type Middleware interface {
OnRequest(ctx *Context)
}
Middleware processes a request before it reaches the handler. Implementations may also implement ResponseMiddleware to receive a callback after the handler has finished.
type RequestHandler ¶
type RequestHandler func(context *Context)
type ResponseMiddleware ¶
type ResponseMiddleware interface {
OnResponse(ctx *Context)
}
ResponseMiddleware is an optional interface that a Middleware can implement to observe (or finalize) the response after the handler returns. It is invoked even when the chain was aborted or the handler panicked.
type Router ¶
type Router struct {
Handlers []RouterMapping
HandlerRouter *mux.Router
Middleware []Middleware
// contains filtered or unexported fields
}
func (*Router) AddHandler ¶
func (r *Router) AddHandler(pattern string, handler RequestHandler)
func (*Router) DELETE ¶
func (r *Router) DELETE(pattern string, handler RequestHandler)
func (*Router) GET ¶
func (r *Router) GET(pattern string, handler RequestHandler)
func (*Router) Group ¶
func (r *Router) Group(prefix string, middlewares ...Middleware) *Group
Group creates a route group sharing the given prefix and middlewares.
func (*Router) METHODS ¶
func (r *Router) METHODS(pattern string, methods []string, handler RequestHandler)
func (*Router) MakeHandlerContext ¶
func (r *Router) MakeHandlerContext(writer http.ResponseWriter, request *http.Request, pattern string) *Context
MakeHandlerContext builds a Context and runs the global middleware chain. It returns nil when the chain was aborted. Kept for backward compatibility; the internal dispatch path uses more features.
func (*Router) PATCH ¶
func (r *Router) PATCH(pattern string, handler RequestHandler)
func (*Router) POST ¶
func (r *Router) POST(pattern string, handler RequestHandler)
func (*Router) PUT ¶
func (r *Router) PUT(pattern string, handler RequestHandler)
func (*Router) Prefix ¶
func (r *Router) Prefix(pattern string, handler RequestHandler)
type RouterMapping ¶
type RouterMapping struct {
Pattern string
HandlerFunc RequestHandler
}