Documentation
¶
Index ¶
- func Bind[T any](c *Context) (T, error)
- func Get[T any](c *Context, key Key[T]) (T, bool)
- func Param[T paramType](c *Context, name string) (T, error)
- func Set[T any](c *Context, key Key[T], value T)
- type App
- type Config
- type Context
- func (c *Context) Bind(v any) error
- func (c *Context) HTML(status int, html string) error
- func (c *Context) JSON(status int, v any) error
- func (c *Context) PathParam(name string) string
- func (c *Context) QueryParam(name string) string
- func (c *Context) ReplaceWriter(w http.ResponseWriter)
- func (c *Context) Request() *http.Request
- func (c *Context) Response() http.ResponseWriter
- func (c *Context) Status(code int) error
- func (c *Context) String(status int, s string) error
- type Group
- func (g *Group) DELETE(path string, h Handler, mw ...Middleware)
- func (g *Group) GET(path string, h Handler, mw ...Middleware)
- func (g *Group) Group(prefix string, mw ...Middleware) *Group
- func (g *Group) PATCH(path string, h Handler, mw ...Middleware)
- func (g *Group) POST(path string, h Handler, mw ...Middleware)
- func (g *Group) PUT(path string, h Handler, mw ...Middleware)
- func (g *Group) Use(mw ...Middleware)
- type HTTPError
- type Handler
- type Key
- type Middleware
- type Router
- func (r *Router) DELETE(path string, h Handler, mw ...Middleware)
- func (r *Router) GET(path string, h Handler, mw ...Middleware)
- func (r *Router) Group(prefix string, mw ...Middleware) *Group
- func (r *Router) PATCH(path string, h Handler, mw ...Middleware)
- func (r *Router) POST(path string, h Handler, mw ...Middleware)
- func (r *Router) PUT(path string, h Handler, mw ...Middleware)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(mw ...Middleware)
- type Serializer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bind ¶
Bind decodes the request body into a value of type T and returns it. This is the preferred binding method — it is type-safe and ARX itself does not call the reflect package to make it work.
Example:
type CreateUserRequest struct {
Name string `json:"name"`
Email string `json:"email"`
}
req, err := arx.Bind[CreateUserRequest](c)
func Get ¶ added in v0.5.0
Get retrieves the value stored under key from the request context. Returns the zero value of T and false if the key is not present. The return type is T — no type assertion required at the call site.
Types ¶
type App ¶
type App struct {
*Router
// contains filtered or unexported fields
}
App is the top-level ARX application. It owns the router, HTTP server, and the error handler. Create one with New(), add routes, then call Run().
type Config ¶
type Config struct {
// Addr is the TCP address to listen on, e.g. ":8080" or "127.0.0.1:9000".
// There is no default — binding to the wrong address causes silent ops headaches.
Addr string
// ReadTimeout caps how long the server waits for a full request (headers + body).
// Protects against slow-read attacks like Slowloris. Default: 5s.
ReadTimeout time.Duration
// WriteTimeout caps how long the server waits to write the full response.
// For streaming endpoints this should be higher than ReadTimeout. Default: 10s.
WriteTimeout time.Duration
// IdleTimeout caps how long an idle keep-alive connection is kept open. Default: 60s.
IdleTimeout time.Duration
// MaxHeaderBytes limits the size of request headers in bytes. Default: 1MB (stdlib default).
MaxHeaderBytes int
// Serializer overrides the default encoding/json backend.
// Leave nil to use the stdlib JSON serializer.
Serializer Serializer
// ErrorHandler is called when a handler returns a non-nil error.
// If nil, the default handler logs the error and sends a 500 or the HTTPError's code.
ErrorHandler func(*Context, error)
// Logger is used for framework-level messages (startup, shutdown, handler errors).
// Defaults to slog.Default().
Logger *slog.Logger
}
Config holds everything needed to initialize an ARX app. All fields have sensible defaults except Addr, which you must provide.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is the per-request object passed to every handler. It wraps stdlib's ResponseWriter and Request, adds typed response helpers, and carries the app-level Serializer so handlers never need to import encoding/json (or whatever the configured backend is) directly.
func (*Context) Bind ¶
Bind decodes the request body into v using the configured Serializer. Prefer the generic Bind[T] function over this method when you want compile-time type safety without a type assertion on the caller's side.
func (*Context) HTML ¶ added in v0.5.0
HTML sends an HTML response with the given status code. Content-Type is set to "text/html; charset=utf-8". The html string is written as-is — callers are responsible for safe content. When using Templ, render to a bytes.Buffer first: component.Render(ctx, &buf), then c.HTML(200, buf.String()).
func (*Context) JSON ¶
JSON encodes v as JSON and writes it with the given HTTP status code. It uses whatever Serializer was configured on the app at startup.
func (*Context) PathParam ¶
PathParam returns a URL path parameter by name. For a route registered as /users/{id}, call c.PathParam("id"). Returns an empty string if the name was not in the route pattern.
func (*Context) QueryParam ¶
QueryParam returns the first value for a URL query parameter. Returns an empty string if the key is absent.
func (*Context) ReplaceWriter ¶ added in v0.2.0
func (c *Context) ReplaceWriter(w http.ResponseWriter)
ReplaceWriter swaps the underlying ResponseWriter. Middleware that needs to buffer the response (to set headers like Set-Cookie after the handler returns but before the bytes reach the client) should call this before invoking the next handler.
func (*Context) Response ¶
func (c *Context) Response() http.ResponseWriter
Response returns the underlying http.ResponseWriter. Use this when you need direct access to headers or want to stream a response.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group is a set of routes that share a URL prefix and a middleware chain. Create one with Router.Group() or App.Group().
Groups can be nested — calling Group on a Group produces a child group whose prefix and middleware stack are inherited from the parent.
func (*Group) Group ¶
func (g *Group) Group(prefix string, mw ...Middleware) *Group
Group creates a nested child group with an appended prefix and additional middleware. The child inherits all middleware from its parent.
func (*Group) Use ¶
func (g *Group) Use(mw ...Middleware)
Use adds middleware that applies to all routes registered on this group.
type HTTPError ¶
HTTPError is returned from handlers when you want the framework to send a specific HTTP status code. The default error handler knows how to unwrap it and respond correctly without leaking stack traces to the client.
func ErrBadRequest ¶
func ErrForbidden ¶
func ErrForbidden() *HTTPError
func ErrInternalServer ¶
func ErrInternalServer() *HTTPError
func ErrMethodNotAllowed ¶
func ErrMethodNotAllowed() *HTTPError
func ErrNotFound ¶
func ErrNotFound() *HTTPError
func ErrUnauthorized ¶
func ErrUnauthorized() *HTTPError
func NewHTTPError ¶
NewHTTPError creates an HTTPError with the given status code and message.
type Handler ¶
Handler is the function signature every ARX route handler must implement. Returning an error lets the framework centralize error-to-response logic rather than scattering w.WriteHeader calls across every handler.
type Key ¶ added in v0.5.0
type Key[T any] struct { // contains filtered or unexported fields }
Key is a typed context key. Create one with NewKey[T](). Keys are unique — no two NewKey calls produce the same key. The type parameter T determines what value type this key stores and retrieves, making Get return T directly without any type assertion.
type Middleware ¶
Middleware wraps a Handler to inject behavior before or after it runs. The standard contract: call next(c) to continue down the chain, skip it to short-circuit (e.g., auth rejection).
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router wraps net/http.ServeMux with ARX's handler signature, middleware support, and route grouping. It does not replace the stdlib — it composes on top of it, meaning any stdlib-compatible handler can still be registered via ServeHTTP.
func (*Router) Group ¶
func (r *Router) Group(prefix string, mw ...Middleware) *Group
Group returns a RouteGroup that prefixes all its routes with the given path and applies the given middleware to all of them.
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP makes Router implement http.Handler so it can plug into any stdlib-compatible server, test helper, or reverse proxy setup.
func (*Router) Use ¶
func (r *Router) Use(mw ...Middleware)
Use appends middleware to the router's global chain. All routes registered after this call will have these middleware applied. Call Use before registering routes, not after.
type Serializer ¶
type Serializer interface {
Encode(w io.Writer, v any) error
Decode(r io.Reader, v any) error
// ContentType returns the MIME type this serializer produces,
// used to set the Content-Type response header automatically.
ContentType() string
}
Serializer handles encoding responses and decoding request bodies. The default implementation uses encoding/json from stdlib. Swap it out at app initialization if you need better performance (e.g., goccy/go-json) or a zero-reflection code-generated marshaler in v2.
func DefaultSerializer ¶
func DefaultSerializer() Serializer
DefaultSerializer returns the stdlib JSON serializer. Pass a different implementation to Config.Serializer when initializing the app if you need a faster or code-generated alternative.