Documentation
¶
Index ¶
- Constants
- type Application
- func (app *Application) AddRoute(method string, pattern string, handlers []HandlerFunc)
- func (app *Application) Delete(pattern string, handlers ...HandlerFunc)
- func (app *Application) Get(pattern string, handlers ...HandlerFunc)
- func (app *Application) Group(prefix string) *Group
- func (app *Application) Head(pattern string, handlers ...HandlerFunc)
- func (app *Application) LoadHTMLGlob(pattern string)
- func (app *Application) Options(pattern string, handlers ...HandlerFunc)
- func (app *Application) Patch(pattern string, handlers ...HandlerFunc)
- func (app *Application) Post(pattern string, handlers ...HandlerFunc)
- func (app *Application) Put(pattern string, handlers ...HandlerFunc)
- func (app *Application) RequestHandler() fasthttp.RequestHandler
- func (app *Application) Run(address ...string) error
- func (app *Application) RunGraceful(shutdownTimeout int, address ...string) error
- func (app *Application) SetFuncMap(funcMap template.FuncMap)
- func (app *Application) Shutdown()
- func (app *Application) Static(root string, prefix string)
- func (app *Application) Use(middlewares ...Middleware)
- type Config
- type Context
- func (c *Context) AcceptedLanguages() []string
- func (c *Context) AddHeader(key, value string)
- func (c *Context) Body() []byte
- func (c *Context) ContentType() string
- func (c *Context) Cookie(name string) string
- func (c *Context) Cookies() map[string]string
- func (c *Context) DelData(key string)
- func (c *Context) DelHeader(key string)
- func (c *Context) Fail(code int, message string)
- func (c *Context) File(filepath string) error
- func (c *Context) FileFromSafeDir(safeDir string, requestPath string) error
- func (c *Context) GetData(key string) any
- func (c *Context) HTML(code int, name string, data any)
- func (c *Context) Header(key string) string
- func (c *Context) Headers() map[string]string
- func (c *Context) IsAjax() bool
- func (c *Context) IsWebSocket() bool
- func (c *Context) JSON(code int, obj any)
- func (c *Context) JSONBody(v any, valid ...bool) error
- func (c *Context) JSONError(code int, message string)
- func (c *Context) Next()
- func (c *Context) Param(key string) string
- func (c *Context) ParamFloat32(key string) (float32, error)
- func (c *Context) ParamFloat64(key string) (float64, error)
- func (c *Context) ParamInt(key string) (int, error)
- func (c *Context) ParamInt64(key string) (int64, error)
- func (c *Context) ParamUInt(key string) (uint, error)
- func (c *Context) ParamUInt64(key string) (uint64, error)
- func (c *Context) Params() map[string]string
- func (c *Context) Queries() map[string][]string
- func (c *Context) Query(key string) string
- func (c *Context) QueryBool(key string) (bool, error)
- func (c *Context) QueryFloat32(key string) (float32, error)
- func (c *Context) QueryFloat64(key string) (float64, error)
- func (c *Context) QueryInt(key string) (int, error)
- func (c *Context) QueryInt8(key string) (int8, error)
- func (c *Context) QueryInt32(key string) (int32, error)
- func (c *Context) QueryInt64(key string) (int64, error)
- func (c *Context) QueryUInt(key string) (uint, error)
- func (c *Context) QueryUInt8(key string) (uint8, error)
- func (c *Context) QueryUInt32(key string) (uint32, error)
- func (c *Context) QueryUInt64(key string) (uint64, error)
- func (c *Context) RawBody() []byte
- func (c *Context) Redirect(code int, url string)
- func (c *Context) RedirectSafe(code int, url string, allowedHosts ...string) error
- func (c *Context) Referer() string
- func (c *Context) RemoteAddr() string
- func (c *Context) SetBody(body []byte)
- func (c *Context) SetCookie(key string, value string)
- func (c *Context) SetCookieWithConfig(config CookieConfig)
- func (c *Context) SetData(key string, value any)
- func (c *Context) SetHeader(key string, value string)
- func (c *Context) SetStatus(code int)
- func (c *Context) SkipFlush()
- func (c *Context) Status() int
- func (c *Context) StringBody() string
- func (c *Context) Success(data any)
- func (c *Context) Text(code int, text string)
- func (c *Context) UserAgent() string
- func (c *Context) XML(code int, obj any)
- type CookieConfig
- type Group
- func (g *Group) AddRoute(method string, pattern string, handlers []HandlerFunc)
- func (g *Group) Delete(pattern string, handlers ...HandlerFunc)
- func (g *Group) Get(pattern string, handlers ...HandlerFunc)
- func (g *Group) Group(prefix string) *Group
- func (g *Group) Head(pattern string, handlers ...HandlerFunc)
- func (g *Group) Options(pattern string, handlers ...HandlerFunc)
- func (g *Group) Patch(pattern string, handlers ...HandlerFunc)
- func (g *Group) Post(pattern string, handlers ...HandlerFunc)
- func (g *Group) Put(pattern string, handlers ...HandlerFunc)
- func (g *Group) Use(middlewares ...HandlerFunc)
- type HandlerFunc
- type JSONMarshal
- type JSONUnmarshal
- type Map
- type Middleware
Constants ¶
const ( MIMETextPlain = "text/plain" MIMETextHTML = "text/html" MIMEApplicationXML = "application/xml" MIMEApplicationJSON = "application/json" MIMEApplicationXMLCharsetUTF8 = "application/xml; charset=utf-8" MIMEApplicationJSONCharsetUTF8 = "application/json; charset=utf-8" MIMEMultipartForm = "multipart/form-data" MIMEOctetStream = "application/octet-stream" )
MIME types
const ( HeaderContentType = "Content-Type" HeaderContentDisposition = "Content-Disposition" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderAccept = "Accept" HeaderAcceptEncoding = "Accept-Encoding" HeaderAcceptLanguage = "Accept-Language" HeaderAuthorization = "Authorization" HeaderCacheControl = "Cache-Control" HeaderConnection = "Connection" HeaderCookie = "Cookie" HeaderHost = "Host" HeaderOrigin = "Origin" HeaderReferer = "Referer" HeaderUserAgent = "User-Agent" HeaderXRequestedWith = "X-Requested-With" HeaderXRealIP = "X-Real-IP" HeaderXForwardedFor = "X-Forwarded-For" HeaderLocation = "Location" HeaderUpgrade = "Upgrade" )
Header keys
const ( StatusContinue = 100 StatusSwitchingProtocols = 101 StatusOK = 200 StatusCreated = 201 StatusAccepted = 202 StatusNoContent = 204 StatusMultipleChoices = 300 StatusMovedPermanently = 301 StatusFound = 302 StatusSeeOther = 303 StatusNotModified = 304 StatusUseProxy = 305 StatusTemporaryRedirect = 307 StatusBadRequest = 400 StatusPaymentRequired = 402 StatusForbidden = 403 StatusNotFound = 404 StatusMethodNotAllowed = 405 StatusNotAcceptable = 406 StatusProxyAuthRequired = 407 StatusRequestTimeout = 408 StatusConflict = 409 StatusGone = 410 StatusLengthRequired = 411 StatusPreconditionFailed = 412 StatusRequestEntityTooLarge = 413 StatusRequestURITooLarge = 414 StatusUnsupportedMediaType = 415 StatusRequestedRangeNotSatisfiable = 416 StatusExpectationFailed = 417 StatusTeapot = 418 StatusUpgradeRequired = 426 StatusInternalServerError = 500 StatusNotImplemented = 501 StatusBadGateway = 502 StatusGatewayTimeout = 504 StatusHTTPVersionNotSupported = 505 )
HTTP status codes
const ( MethodGet = "GET" MethodHead = "HEAD" MethodPost = "POST" MethodPut = "PUT" MethodPatch = "PATCH" MethodDelete = "DELETE" MethodConnect = "CONNECT" MethodOptions = "OPTIONS" MethodTrace = "TRACE" )
HTTP methods
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application struct {
Config *Config
Logger *lightlog.ConsoleLogger
// contains filtered or unexported fields
}
Application is the main struct that holds the router, middlewares, and configuration.
func DefaultApp ¶
func DefaultApp() *Application
DefaultApp returns a new instance of the Application struct with default middlewares
func NewApp ¶
func NewApp(c ...*Config) *Application
NewApp returns a new instance of the Application struct.
func (*Application) AddRoute ¶
func (app *Application) AddRoute(method string, pattern string, handlers []HandlerFunc)
AddRoute adds a new route to the router. It composes the global middlewares, route-specific middlewares, and the actual handler function to form a single MiddlewareFunc, and then adds it to the router.
func (*Application) Delete ¶
func (app *Application) Delete(pattern string, handlers ...HandlerFunc)
Delete adds a new route with method "DELETE" to the router.
func (*Application) Get ¶
func (app *Application) Get(pattern string, handlers ...HandlerFunc)
Get adds a new route with method "GET" to the router.
func (*Application) Group ¶
func (app *Application) Group(prefix string) *Group
Group returns a new instance of the Group struct with the given prefix.
func (*Application) Head ¶
func (app *Application) Head(pattern string, handlers ...HandlerFunc)
Head adds a new route with method "HEAD" to the router.
func (*Application) LoadHTMLGlob ¶ added in v0.5.0
func (app *Application) LoadHTMLGlob(pattern string)
LoadHTMLGlob loads HTML templates from a glob pattern and sets them in the Application struct. It uses the template.Must function to panic if there is an error parsing the templates. It also sets the funcMap in the Application struct to the funcMap passed in as an argument.
func (*Application) Options ¶
func (app *Application) Options(pattern string, handlers ...HandlerFunc)
Options adds a new route with method "OPTIONS" to the router.
func (*Application) Patch ¶
func (app *Application) Patch(pattern string, handlers ...HandlerFunc)
Patch adds a new route with method "PATCH" to the router.
func (*Application) Post ¶
func (app *Application) Post(pattern string, handlers ...HandlerFunc)
Post adds a new route with method "POST" to the router.
func (*Application) Put ¶
func (app *Application) Put(pattern string, handlers ...HandlerFunc)
Put adds a new route with method "PUT" to the router.
func (*Application) RequestHandler ¶ added in v0.9.0
func (app *Application) RequestHandler() fasthttp.RequestHandler
RequestHandler returns a fasthttp.RequestHandler for the Application.
func (*Application) Run ¶
func (app *Application) Run(address ...string) error
Run starts the HTTP server and listens for incoming requests.
func (*Application) RunGraceful ¶ added in v0.8.0
func (app *Application) RunGraceful(shutdownTimeout int, address ...string) error
RunGraceful starts the HTTP server with graceful shutdown support. It listens for SIGINT and SIGTERM signals to trigger graceful shutdown. The shutdownTimeout specifies the maximum duration in seconds to wait for active connections to finish.
func (*Application) SetFuncMap ¶ added in v0.5.0
func (app *Application) SetFuncMap(funcMap template.FuncMap)
SetFuncMap sets the funcMap in the Application struct to the funcMap passed in as an argument.
func (*Application) Shutdown ¶ added in v0.8.0
func (app *Application) Shutdown()
Shutdown gracefully shuts down the server without interrupting active connections.
func (*Application) Static ¶ added in v0.5.0
func (app *Application) Static(root string, prefix string)
Static serves static files from the given root directory with the given prefix. If root is an absolute path, it is used directly. Otherwise, it is resolved relative to the executable's directory. If the file exists, it is served with a 200 status code. If the file does not exist, a 404 status code is returned with the text "Not Found". Accessing files outside the root directory is blocked to prevent path traversal attacks.
func (*Application) Use ¶
func (app *Application) Use(middlewares ...Middleware)
Use adds one or more Middlewares to the array of middlewares in the Application struct.
type Config ¶ added in v0.4.0
type Config struct {
AppName string
JSONEncoder JSONMarshal
JSONDecoder JSONUnmarshal
NotFoundHandler HandlerFunc
EnableDebug bool
DebugToken string
MaxRequestBodySize int64
TrustedProxies []string
}
Config holds the configuration for the Application.
type Context ¶
type Context struct {
App *Application
Method string
Path string
// contains filtered or unexported fields
}
Context represents the context of an HTTP request/response.
func NewContext ¶ added in v0.1.3
func NewContext(ctx *fasthttp.RequestCtx) *Context
NewContext creates a new Context object for the given fasthttp request context.
func (*Context) AcceptedLanguages ¶ added in v0.9.0
AcceptedLanguages returns the accepted languages from the request.
func (*Context) ContentType ¶ added in v0.9.0
ContentType returns the Content-Type header of the request.
func (*Context) File ¶
File writes a file as the response. WARNING: The caller MUST validate that the path does not contain user-controlled input that could lead to directory traversal. Use FileFromSafeDir for safer file serving.
func (*Context) FileFromSafeDir ¶ added in v0.11.0
FileFromSafeDir serves a file from the specified safe directory, preventing path traversal. It validates that the resolved file path stays within safeDir.
func (*Context) HTML ¶ added in v0.5.0
HTML writes an HTML response with the given status code, template name, and data.
func (*Context) IsWebSocket ¶ added in v0.9.0
IsWebSocket checks if the request is a WebSocket upgrade request.
func (*Context) JSONBody ¶
JSONBody parses the request body as JSON and stores the result in v. If valid is true, the struct is validated after parsing.
func (*Context) JSONError ¶ added in v0.9.0
JSONError writes a JSON error response with the given status code and message.
func (*Context) Next ¶
func (c *Context) Next()
Next calls the next middleware function in the chain.
func (*Context) ParamFloat32 ¶ added in v0.6.0
ParamFloat32 returns the value of a URL parameter as a float32.
func (*Context) ParamFloat64 ¶ added in v0.6.0
ParamFloat64 returns the value of a URL parameter as a float64.
func (*Context) ParamInt ¶ added in v0.6.0
ParamInt returns the value of a URL parameter as an integer.
func (*Context) ParamInt64 ¶ added in v0.6.0
ParamInt64 returns the value of a URL parameter as an int64.
func (*Context) ParamUInt ¶ added in v0.6.0
ParamUInt returns the value of a URL parameter as a uint.
func (*Context) ParamUInt64 ¶ added in v0.6.0
ParamUInt64 returns the value of a URL parameter as a uint64.
func (*Context) QueryBool ¶ added in v0.7.3
QueryBool returns the value of a given query parameter as a bool.
func (*Context) QueryFloat32 ¶ added in v0.7.0
QueryFloat32 returns the value of a given query parameter as a float32.
func (*Context) QueryFloat64 ¶ added in v0.7.0
QueryFloat64 returns the value of a given query parameter as a float64.
func (*Context) QueryInt ¶ added in v0.7.0
QueryInt returns the value of a given query parameter as an int.
func (*Context) QueryInt8 ¶ added in v0.7.0
QueryInt8 returns the value of a given query parameter as an int8.
func (*Context) QueryInt32 ¶ added in v0.7.0
QueryInt32 returns the value of a given query parameter as an int32.
func (*Context) QueryInt64 ¶ added in v0.7.0
QueryInt64 returns the value of a given query parameter as an int64.
func (*Context) QueryUInt ¶ added in v0.7.0
QueryUInt returns the value of a given query parameter as a uint.
func (*Context) QueryUInt8 ¶ added in v0.7.0
QueryUInt8 returns the value of a given query parameter as a uint8.
func (*Context) QueryUInt32 ¶ added in v0.7.0
QueryUInt32 returns the value of a given query parameter as a uint32.
func (*Context) QueryUInt64 ¶ added in v0.7.0
QueryUInt64 returns the value of a given query parameter as a uint64.
func (*Context) Redirect ¶
Redirect redirects the request to a new URL with the given status code. WARNING: Do not pass user-controlled input directly. Use RedirectSafe instead.
func (*Context) RedirectSafe ¶ added in v0.11.0
RedirectSafe performs a redirect only if the URL is a safe relative path or belongs to one of the allowed hosts.
func (*Context) RemoteAddr ¶
RemoteAddr returns the remote address of the request.
func (*Context) SetCookie ¶
SetCookie sets a new cookie with the given key-value pair. Deprecated: Use SetCookieWithConfig for proper security attributes (HttpOnly, Secure, SameSite).
func (*Context) SetCookieWithConfig ¶ added in v0.11.0
func (c *Context) SetCookieWithConfig(config CookieConfig)
SetCookieWithConfig sets a cookie with full security configuration.
func (*Context) SkipFlush ¶ added in v0.3.0
func (c *Context) SkipFlush()
SkipFlush sets the skipFlush flag to true, which prevents the response buffer from being flushed.
func (*Context) StringBody ¶
StringBody returns the request body as a string.
type CookieConfig ¶ added in v0.11.0
type CookieConfig struct {
Name string
Value string
Path string
Domain string
MaxAge int
Secure bool
HttpOnly bool
SameSite string
}
CookieConfig holds the configuration for setting a cookie with security attributes.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a group of routes with a common prefix and middleware.
func (*Group) AddRoute ¶
func (g *Group) AddRoute(method string, pattern string, handlers []HandlerFunc)
AddRoute adds a new route to the Application with the given method, pattern, and handlers. The route's path is the full prefix of the Group concatenated with the given pattern. The route's handlers are the middleware functions of the Group and its ancestors concatenated with the given handlers.
func (*Group) Delete ¶
func (g *Group) Delete(pattern string, handlers ...HandlerFunc)
Delete adds a new DELETE route to the Application with the given pattern and handlers.
func (*Group) Get ¶
func (g *Group) Get(pattern string, handlers ...HandlerFunc)
Get adds a new GET route to the Application with the given pattern and handlers.
func (*Group) Group ¶
Group creates a new Group with the given prefix and adds it as a child of the current Group.
func (*Group) Head ¶
func (g *Group) Head(pattern string, handlers ...HandlerFunc)
Head adds a new HEAD route to the Application with the given pattern and handlers.
func (*Group) Options ¶
func (g *Group) Options(pattern string, handlers ...HandlerFunc)
Options adds a new OPTIONS route to the Application with the given pattern and handlers.
func (*Group) Patch ¶
func (g *Group) Patch(pattern string, handlers ...HandlerFunc)
Patch adds a new PATCH route to the Application with the given pattern and handlers.
func (*Group) Post ¶
func (g *Group) Post(pattern string, handlers ...HandlerFunc)
Post adds a new POST route to the Application with the given pattern and handlers.
func (*Group) Put ¶
func (g *Group) Put(pattern string, handlers ...HandlerFunc)
Put adds a new PUT route to the Application with the given pattern and handlers.
func (*Group) Use ¶
func (g *Group) Use(middlewares ...HandlerFunc)
Use adds the given middleware functions to the Group's middleware stack.
type HandlerFunc ¶
type HandlerFunc func(*Context)
HandlerFunc is a function type that represents the actual handler function for a route.
type JSONMarshal ¶ added in v0.4.0
type JSONUnmarshal ¶ added in v0.4.0
type Middleware ¶ added in v0.1.1
type Middleware = HandlerFunc
Middleware is an alias for HandlerFunc, representing middleware functions.
func Helmet ¶ added in v0.11.0
func Helmet() Middleware
Helmet returns a middleware that sets common security response headers.
func Logger ¶
func Logger() Middleware
Logger returns a middleware function that logs incoming requests
func Recovery ¶
func Recovery(handler ...HandlerFunc) Middleware
Recovery returns a middleware that recovers from panics and sends a 500 response with an error message.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
contextual_data
command
|
|
|
cookie
command
|
|
|
group
command
|
|
|
middleware
command
|
|
|
middleware_group
command
|
|
|
not_found
command
|
|
|
parse_request_body
command
|
|
|
ping
command
|
|
|
recovery
command
|
|
|
redirect
command
|
|
|
response_body
command
|
|
|
response_header
command
|
|
|
routing
command
|
|
|
static
command
|
|
|
success_fail
command
|
|
|
validation
command
|