Documentation
¶
Index ¶
- Variables
- func Adapt(handler HandlerFunc, logger Logger) http.Handler
- func AdaptApp(app *App, handler HandlerFunc) http.Handler
- func Adapter(handler HandlerFunc) http.Handler
- func ContextWithLogger(ctx context.Context, logger Logger) context.Context
- func DefaultErrorHandler(c *Context, err error)
- func Validate(v any) error
- func Validator() *validator.Validate
- type App
- func (a *App) ANY(path string, handler HandlerFunc) []*Route
- func (a *App) CONNECT(path string, handler HandlerFunc) *Route
- func (a *App) DELETE(path string, handler HandlerFunc) *Route
- func (a *App) File(urlPath, filePath string)
- func (a *App) GET(path string, handler HandlerFunc) *Route
- func (a *App) Group(prefix string, middlewares ...Middleware) *Group
- func (a *App) HEAD(path string, handler HandlerFunc) *Route
- func (a *App) HTTPServer() *http.Server
- func (a *App) Handler() http.Handler
- func (a *App) ListenAndServe(addr string, cfg ServerConfig) error
- func (a *App) Logger() Logger
- func (a *App) OPTIONS(path string, handler HandlerFunc) *Route
- func (a *App) PATCH(path string, handler HandlerFunc) *Route
- func (a *App) POST(path string, handler HandlerFunc) *Route
- func (a *App) PUT(path string, handler HandlerFunc) *Route
- func (a *App) Reverse(name string, params ...string) (string, error)
- func (a *App) Routes() []Route
- func (a *App) Server(host string, port int) error
- func (a *App) ServerWithConfig(host string, port int, cfg ServerConfig) error
- func (a *App) SetErrorHandler(handler ErrorHandler)
- func (a *App) SetLogger(logger Logger)
- func (a *App) SetMaxBodyBytes(n int64)
- func (a *App) SetMethodNotAllowed(handler HandlerFunc)
- func (a *App) SetNotFound(handler HandlerFunc)
- func (a *App) SetProxyHeaders(headers ...string)
- func (a *App) SetTrustedProxies(cidrs ...string) error
- func (a *App) Shutdown(ctx context.Context) error
- func (a *App) Static(prefix, root string)
- func (a *App) TRACE(path string, handler HandlerFunc) *Route
- func (a *App) Use(middlewares ...Middleware)
- type Context
- func (c *Context) App() *App
- func (c *Context) BearerToken() string
- func (c *Context) Bind(v any) error
- func (c *Context) BindJSON(v any) error
- func (c *Context) BindXML(v any) error
- func (c *Context) Body() ([]byte, error)
- func (c *Context) Cookie(cookie *http.Cookie)
- func (c *Context) CookieGet(name string) (*http.Cookie, error)
- func (c *Context) Cookies() []*http.Cookie
- func (c *Context) Data(status int, contentType string, data []byte) error
- func (c *Context) DeleteCookie(name string, path string)
- func (c *Context) DetachFromPool()
- func (c *Context) DisableShared()
- func (c *Context) Download(filePath string, filename string) error
- func (c *Context) EnableShared()
- func (c *Context) Error(status int, message string) error
- func (c *Context) File(path string) error
- func (c *Context) FormFile(key string) ([]byte, string, error)
- func (c *Context) FormValue(key string) string
- func (c *Context) Get(key string) (any, bool)
- func (c *Context) HTML(status int, value string) error
- func (c *Context) Header(key string, value string)
- func (c *Context) HeaderGet(key string) string
- func (c *Context) HoldForHandler(done <-chan struct{})
- func (c *Context) Host() string
- func (c *Context) IP() string
- func (c *Context) IsHTTPS() bool
- func (c *Context) IsJSON() bool
- func (c *Context) IsXML() bool
- func (c *Context) JSON(status int, data any) error
- func (c *Context) Log() Logger
- func (c *Context) MarkWritten(status int)
- func (c *Context) Method() string
- func (c *Context) MustGet(key string) any
- func (c *Context) NoContent() error
- func (c *Context) Param(key string) string
- func (c *Context) Path() string
- func (c *Context) Query(key string) string
- func (c *Context) QueryDefault(key string, fallback string) string
- func (c *Context) QueryInt(key string, fallback int) int
- func (c *Context) Redirect(status int, location string) error
- func (c *Context) Referer() string
- func (c *Context) Release()
- func (c *Context) Request() *http.Request
- func (c *Context) Response() http.ResponseWriter
- func (c *Context) Set(key string, value any)
- func (c *Context) SetCookie(name string, value string, maxAge int, path string, secure bool, httpOnly bool)
- func (c *Context) SetLogger(logger Logger)
- func (c *Context) SetRequest(r *http.Request)
- func (c *Context) SetWriter(w http.ResponseWriter)
- func (c *Context) Status(status int)
- func (c *Context) StatusCode() int
- func (c *Context) Stream(status int, contentType string, reader io.Reader) error
- func (c *Context) String(status int, value string) error
- func (c *Context) UserAgent() string
- func (c *Context) Validate(v any) error
- func (c *Context) Written() bool
- func (c *Context) XML(status int, data any) error
- type ErrorHandler
- type Group
- func (g *Group) ANY(path string, handler HandlerFunc) []*Route
- func (g *Group) CONNECT(path string, handler HandlerFunc) *Route
- func (g *Group) DELETE(path string, handler HandlerFunc) *Route
- func (g *Group) GET(path string, handler HandlerFunc) *Route
- func (g *Group) Group(prefix string, middlewares ...Middleware) *Group
- func (g *Group) HEAD(path string, handler HandlerFunc) *Route
- func (g *Group) OPTIONS(path string, handler HandlerFunc) *Route
- func (g *Group) PATCH(path string, handler HandlerFunc) *Route
- func (g *Group) POST(path string, handler HandlerFunc) *Route
- func (g *Group) PUT(path string, handler HandlerFunc) *Route
- func (g *Group) TRACE(path string, handler HandlerFunc) *Route
- func (g *Group) Use(middlewares ...Middleware)
- type HTTPError
- type HandlerFunc
- type Logger
- type Middleware
- type ProxyConfig
- type Route
- type ServerConfig
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadRequest = NewHTTPError(http.StatusBadRequest, "bad request") ErrForbidden = NewHTTPError(http.StatusForbidden, "forbidden") ErrNotFound = NewHTTPError(http.StatusNotFound, "not found") ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed, "method not allowed") ErrConflict = NewHTTPError(http.StatusConflict, "conflict") ErrTooManyRequests = NewHTTPError(http.StatusTooManyRequests, "too many requests") ErrInternalServer = NewHTTPError(http.StatusInternalServerError, "internal server error") ErrGatewayTimeout = NewHTTPError(http.StatusGatewayTimeout, "gateway timeout") ErrRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge, "request entity too large") )
Functions ¶
func Adapter ¶
func Adapter(handler HandlerFunc) http.Handler
func DefaultErrorHandler ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
func (*App) HTTPServer ¶
func (*App) ListenAndServe ¶
func (a *App) ListenAndServe(addr string, cfg ServerConfig) error
func (*App) ServerWithConfig ¶
func (a *App) ServerWithConfig(host string, port int, cfg ServerConfig) error
func (*App) SetErrorHandler ¶
func (a *App) SetErrorHandler(handler ErrorHandler)
func (*App) SetMaxBodyBytes ¶
func (*App) SetMethodNotAllowed ¶
func (a *App) SetMethodNotAllowed(handler HandlerFunc)
func (*App) SetNotFound ¶
func (a *App) SetNotFound(handler HandlerFunc)
func (*App) SetProxyHeaders ¶
func (*App) SetTrustedProxies ¶
func (*App) Use ¶
func (a *App) Use(middlewares ...Middleware)
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is request-scoped and may be reused from a pool after the handler returns. Do not retain or access a Context from another goroutine after the handler returns unless you call DetachFromPool and later Release.
Timeout middleware enables shared mode so the request goroutine and the handler goroutine may safely use the same Context until the handler finishes.
func NewContext ¶
func NewContext(w http.ResponseWriter, r *http.Request) *Context
func (*Context) BearerToken ¶
func (*Context) DeleteCookie ¶
func (*Context) DetachFromPool ¶
func (c *Context) DetachFromPool()
DetachFromPool prevents AdaptApp from returning this Context to the pool. The caller must invoke Release when the Context is no longer used.
func (*Context) DisableShared ¶
func (c *Context) DisableShared()
DisableShared ends concurrent Context access. Only call after the handler goroutine has returned.
func (*Context) EnableShared ¶
func (c *Context) EnableShared()
EnableShared marks this Context safe for concurrent use by Timeout's handler goroutine and the request goroutine. Call DisableShared only after the handler goroutine has finished.
func (*Context) HoldForHandler ¶
func (c *Context) HoldForHandler(done <-chan struct{})
HoldForHandler keeps the Context alive until done is signaled, after the middleware chain finishes. Used by Timeout so the request can return 504 without waiting for a slow handler, while still protecting the pool.
func (*Context) IsHTTPS ¶
IsHTTPS reports whether the request arrived over TLS, or was forwarded as HTTPS by a trusted proxy (X-Forwarded-Proto). Untrusted clients cannot spoof HTTPS via X-Forwarded-Proto alone.
func (*Context) MarkWritten ¶
MarkWritten records that a response was already committed (status + body), so the error handler will not write again.
func (*Context) Release ¶
func (c *Context) Release()
Release returns a previously detached Context to the pool.
func (*Context) Response ¶
func (c *Context) Response() http.ResponseWriter
func (*Context) SetRequest ¶
func (*Context) SetWriter ¶
func (c *Context) SetWriter(w http.ResponseWriter)
func (*Context) StatusCode ¶
type ErrorHandler ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
func (*Group) Use ¶
func (g *Group) Use(middlewares ...Middleware)
type HTTPError ¶
type HTTPError struct {
Code int `json:"-"`
Message string `json:"message"`
Details map[string]any `json:"details,omitempty"`
Internal error `json:"-"`
}
func AsHTTPError ¶
func NewHTTPError ¶
type HandlerFunc ¶
func Chain ¶
func Chain(handler HandlerFunc, middlewares ...Middleware) HandlerFunc
type Logger ¶
type Logger interface {
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
With(args ...any) Logger
}
func DefaultLogger ¶
func DefaultLogger() Logger
func NewTextLogger ¶
type Middleware ¶
type Middleware func(HandlerFunc) HandlerFunc
type ProxyConfig ¶
func DefaultProxyConfig ¶
func DefaultProxyConfig() ProxyConfig
type Route ¶
type Route struct {
Method string
Path string
Handler HandlerFunc
Name string
// contains filtered or unexported fields
}
type ServerConfig ¶
type ServerConfig struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ReadHeaderTimeout time.Duration
ShutdownTimeout time.Duration
MaxHeaderBytes int
CertFile string
KeyFile string
}
func DefaultServerConfig ¶
func DefaultServerConfig() ServerConfig
func StreamingServerConfig ¶
func StreamingServerConfig() ServerConfig
StreamingServerConfig disables WriteTimeout so SSE and WebSocket handlers can hold the connection open. Prefer this (or ServerWithConfig) for realtime routes.