Documentation
ΒΆ
Overview ΒΆ
Package amaro implements a blazing fast, zero-dependency, zero-allocation HTTP router and framework for Go.
Index ΒΆ
- func DefaultParamParser(segment string) (bool, string)
- func DefaultWildcardParser(segment string) (bool, string)
- type App
- func (a *App) Add(method, path string, handler Handler, middlewares ...Middleware) error
- func (a *App) DELETE(path string, handler Handler, middlewares ...Middleware) error
- func (a *App) Find(method, path string) (*Route, error)
- func (a *App) GET(path string, handler Handler, middlewares ...Middleware) error
- func (a *App) Group(prefix string) *Group
- func (a *App) HEAD(path string, handler Handler, middlewares ...Middleware) error
- func (a *App) OPTIONS(path string, handler Handler, middlewares ...Middleware) error
- func (a *App) PATCH(path string, handler Handler, middlewares ...Middleware) error
- func (a *App) POST(path string, handler Handler, middlewares ...Middleware) error
- func (a *App) PUT(path string, handler Handler, middlewares ...Middleware) error
- func (a *App) Run(address string) error
- func (a *App) RunTLS(address, certFile, keyFile string) error
- func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (a *App) Static(pathPrefix, root string)
- func (a *App) StaticFS(pathPrefix string, fs fs.FS)
- func (a *App) Test(req *http.Request) *httptest.ResponseRecorder
- func (a *App) Use(middleware Middleware)
- type AppOption
- type Context
- func (c *Context) AddParam(key, value string)
- func (c *Context) BindForm(v interface{}) error
- func (c *Context) BindJSON(v interface{}) error
- func (c *Context) BindQuery(v interface{}) error
- func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
- func (c *Context) Get(key string) (value interface{}, exists bool)
- func (c *Context) GetCookie(name string) (*http.Cookie, error)
- func (c *Context) GetHeader(key string) string
- func (c *Context) HTML(statusCode int, html string) error
- func (c *Context) JSON(statusCode int, v interface{}) error
- func (c *Context) PathParam(name string) string
- func (c *Context) QueryParam(name string) string
- func (c *Context) Redirect(statusCode int, url string) error
- func (c *Context) Reset(w http.ResponseWriter, r *http.Request)
- func (c *Context) SaveFile(file *multipart.FileHeader, dst string) error
- func (c *Context) Set(key string, value interface{})
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) SetHeader(key, value string)
- func (c *Context) Status(statusCode int)
- func (c *Context) String(statusCode int, s string) error
- type ContextOption
- type ErrorHandler
- type Group
- func (g *Group) Add(method, path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) DELETE(path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) Find(method, path string) (*Route, error)
- func (g *Group) GET(path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) Group(prefix string) *Group
- func (g *Group) HEAD(path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) OPTIONS(path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) PATCH(path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) POST(path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) PUT(path string, handler Handler, middlewares ...Middleware) error
- func (g *Group) StaticFS(pathPrefix string, fs fs.FS)
- func (g *Group) Use(middleware Middleware)
- type HTTPError
- type Handler
- type Middleware
- type Param
- type ParamParser
- type RecoveryOption
- type Route
- type Router
- type RouterConfig
- type StaticConfig
- type WildcardParser
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func DefaultParamParser ΒΆ added in v0.3.0
DefaultParamParser implements the standard :param and {param} syntax.
func DefaultWildcardParser ΒΆ added in v0.3.0
DefaultWildcardParser implements the standard *wildcard syntax.
Types ΒΆ
type App ΒΆ
type App struct {
// contains filtered or unexported fields
}
App is the main entry point for the Amaro framework. It holds the router, global middlewares, and a context pool.
func (*App) Add ΒΆ
func (a *App) Add(method, path string, handler Handler, middlewares ...Middleware) error
Add registers a new route with the specified method, path, handler, and middlewares.
func (*App) DELETE ΒΆ
func (a *App) DELETE(path string, handler Handler, middlewares ...Middleware) error
func (*App) GET ΒΆ
func (a *App) GET(path string, handler Handler, middlewares ...Middleware) error
GET registers a new GET route with a handler and optional route-specific middlewares.
func (*App) HEAD ΒΆ
func (a *App) HEAD(path string, handler Handler, middlewares ...Middleware) error
func (*App) OPTIONS ΒΆ
func (a *App) OPTIONS(path string, handler Handler, middlewares ...Middleware) error
func (*App) PATCH ΒΆ
func (a *App) PATCH(path string, handler Handler, middlewares ...Middleware) error
func (*App) POST ΒΆ
func (a *App) POST(path string, handler Handler, middlewares ...Middleware) error
func (*App) RunTLS ΒΆ added in v0.1.0
RunTLS starts the HTTPS server with graceful shutdown support.
func (*App) Test ΒΆ added in v0.4.0
func (a *App) Test(req *http.Request) *httptest.ResponseRecorder
Test executes a request against the application and returns the response recorder. This is a helper for writing tests.
func (*App) Use ΒΆ
func (a *App) Use(middleware Middleware)
Use adds a global middleware to the application. Global middlewares are applied to all routes in the order they are added.
type AppOption ΒΆ
type AppOption func(*App)
AppOption defines a function to configure the App during initialization.
func WithErrorHandler ΒΆ added in v0.2.0
func WithErrorHandler(handler ErrorHandler) AppOption
WithErrorHandler returns an AppOption that configures the App to use the specified ErrorHandler.
func WithRouter ΒΆ
WithRouter returns an AppOption that configures the App to use the specified router.
type Context ΒΆ
type Context struct {
Request *http.Request
Writer http.ResponseWriter
Params []Param // efficient slice instead of map
Keys map[string]interface{}
}
Context represents the context of the current HTTP request. It holds the request and response objects, URL parameters, and provides helper methods. It is designed to be reused via sync.Pool to minimize allocations.
func NewContext ΒΆ
func NewContext(w http.ResponseWriter, r *http.Request, options ...ContextOption) *Context
NewContext creates a new context for the request
func (*Context) BindForm ΒΆ added in v0.4.0
BindForm binds the form parameters to the provided struct.
func (*Context) BindQuery ΒΆ added in v0.4.0
BindQuery binds the query parameters to the provided struct.
func (*Context) FormFile ΒΆ added in v0.1.0
func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
FormFile returns the first file for the provided form key.
func (*Context) QueryParam ΒΆ
func (*Context) Reset ΒΆ
func (c *Context) Reset(w http.ResponseWriter, r *http.Request)
Reset resets the context to be reused in sync.Pool
func (*Context) SaveFile ΒΆ added in v0.1.0
func (c *Context) SaveFile(file *multipart.FileHeader, dst string) error
SaveFile saves the uploaded file to the specified destination.
type ContextOption ΒΆ
type ContextOption func(*Context)
type ErrorHandler ΒΆ added in v0.2.0
ErrorHandler is a function that handles errors occurred during request processing.
type Group ΒΆ
type Group struct {
// contains filtered or unexported fields
}
Group represents a route group with a common path prefix and shared middlewares.
func (*Group) Add ΒΆ
func (g *Group) Add(method, path string, handler Handler, middlewares ...Middleware) error
func (*Group) DELETE ΒΆ
func (g *Group) DELETE(path string, handler Handler, middlewares ...Middleware) error
func (*Group) GET ΒΆ
func (g *Group) GET(path string, handler Handler, middlewares ...Middleware) error
func (*Group) HEAD ΒΆ
func (g *Group) HEAD(path string, handler Handler, middlewares ...Middleware) error
func (*Group) OPTIONS ΒΆ
func (g *Group) OPTIONS(path string, handler Handler, middlewares ...Middleware) error
func (*Group) PATCH ΒΆ
func (g *Group) PATCH(path string, handler Handler, middlewares ...Middleware) error
func (*Group) POST ΒΆ
func (g *Group) POST(path string, handler Handler, middlewares ...Middleware) error
func (*Group) PUT ΒΆ
func (g *Group) PUT(path string, handler Handler, middlewares ...Middleware) error
func (*Group) Use ΒΆ
func (g *Group) Use(middleware Middleware)
Use adds a middleware to the group. These middlewares are applied to all routes registered in this group.
type HTTPError ΒΆ added in v0.3.0
HTTPError represents an error with an associated HTTP status code.
func NewHTTPError ΒΆ added in v0.3.0
NewHTTPError creates a new HTTPError.
func (*HTTPError) SetInternal ΒΆ added in v0.3.0
SetInternal sets the internal error.
type Handler ΒΆ
Handler is a function that handles an HTTP request. It returns an error which can be handled by middlewares or the framework.
func Compile ΒΆ
func Compile(handler Handler, middlewares ...Middleware) Handler
func StaticHandler ΒΆ added in v0.3.0
func StaticHandler(config StaticConfig) Handler
StaticHandler creates a handler that serves static files.
type Middleware ΒΆ
Middleware is a function that wraps a Handler to provide additional functionality.
func Chain ΒΆ
func Chain(middlewares ...Middleware) Middleware
func Recovery ΒΆ added in v0.3.0
func Recovery(opts ...RecoveryOption) Middleware
Recovery recovers from panics, logs the stack trace, and returns an Internal Server Error.
type ParamParser ΒΆ added in v0.3.0
ParamParser defines a function that checks if a path segment is a parameter. It returns true and the parameter name if it is, false otherwise.
type RecoveryOption ΒΆ added in v0.4.0
type RecoveryOption func(*recoveryConfig)
RecoveryOption configures the Recovery middleware.
func WithHTMLDebug ΒΆ added in v0.4.0
func WithHTMLDebug(enabled bool) RecoveryOption
WithHTMLDebug enables rendering a pretty HTML debug page for panics. WARNING: Do not use this in production as it exposes stack traces.
type Route ΒΆ
type Route struct {
Method string
Path string
Handler Handler
Middlewares []Middleware
}
Route represents a registered route.
type Router ΒΆ
type Router interface {
GET(path string, handler Handler, middlewares ...Middleware) error
POST(path string, handler Handler, middlewares ...Middleware) error
PUT(path string, handler Handler, middlewares ...Middleware) error
DELETE(path string, handler Handler, middlewares ...Middleware) error
PATCH(path string, handler Handler, middlewares ...Middleware) error
OPTIONS(path string, handler Handler, middlewares ...Middleware) error
HEAD(path string, handler Handler, middlewares ...Middleware) error
Add(method, path string, handler Handler, middlewares ...Middleware) error
Use(middleware Middleware)
Group(prefix string) *Group
Find(method, path string, ctx *Context) (*Route, error)
StaticFS(pathPrefix string, fs fs.FS)
Routes() []Route
}
Router is the interface that all router implementations must satisfy. It allows for swappable routing strategies.
type RouterConfig ΒΆ added in v0.3.0
type RouterConfig struct {
ParamParser ParamParser
WildcardParser WildcardParser
}
RouterConfig defines configuration for Router.
func DefaultRouterConfig ΒΆ added in v0.3.0
func DefaultRouterConfig() RouterConfig
DefaultRouterConfig returns the default configuration.
type StaticConfig ΒΆ added in v0.3.0
type StaticConfig struct {
// Root is the filesystem to serve from.
Root fs.FS
// Prefix is the URL path prefix.
Prefix string
// Index is the index file name (default: "index.html").
Index string
// Browse enables directory listing (default: false).
Browse bool
// SPA mode: if file not found, serve Index (default: false).
SPA bool
// ModifyResponse allows setting custom headers.
ModifyResponse func(c *Context)
}
StaticConfig defines configuration for serving static files.
type WildcardParser ΒΆ added in v0.3.0
WildcardParser defines a function that checks if a path segment is a wildcard. It returns true and the wildcard name if it is, false otherwise.