Documentation
¶
Overview ¶
Package marten is a minimal, elegant web framework for Go. Philosophy: The framework you reach for when you want nothing in the way.
Index ¶
- func E(message string) map[string]string
- type App
- func (a *App) OnError(fn func(*Ctx, error))
- func (a *App) OnShutdown(fn func())
- func (a *App) OnStart(fn func())
- func (a *App) Run(addr string) error
- func (a *App) RunGraceful(addr string, timeout time.Duration) error
- func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (a *App) SetTrailingSlash(mode TrailingSlashMode)
- type BindError
- type Ctx
- func (c *Ctx) BadRequest(message string) error
- func (c *Ctx) Bearer() string
- func (c *Ctx) Bind(v any) error
- func (c *Ctx) BindValid(v any, validate func() error) error
- func (c *Ctx) Blob(code int, contentType string, data []byte) error
- func (c *Ctx) ClientIP() string
- func (c *Ctx) Context() context.Context
- func (c *Ctx) Cookie(name string) string
- func (c *Ctx) Created(v any) error
- func (c *Ctx) File(name string) (*multipart.FileHeader, error)
- func (c *Ctx) Forbidden(message string) error
- func (c *Ctx) FormValue(name string) string
- func (c *Ctx) Get(key string) any
- func (c *Ctx) GetBool(key string) bool
- func (c *Ctx) GetHeader(key string) string
- func (c *Ctx) GetInt(key string) int
- func (c *Ctx) GetString(key string) string
- func (c *Ctx) HTML(code int, html string) error
- func (c *Ctx) Header(key, value string) *Ctx
- func (c *Ctx) IsAJAX() bool
- func (c *Ctx) IsJSON() bool
- func (c *Ctx) JSON(code int, v any) error
- func (c *Ctx) Method() string
- func (c *Ctx) NoContent() error
- func (c *Ctx) NotFound(message string) error
- func (c *Ctx) OK(v any) error
- func (c *Ctx) Param(name string) string
- func (c *Ctx) ParamInt(name string) int
- func (c *Ctx) ParamInt64(name string) int64
- func (c *Ctx) Path() string
- func (c *Ctx) Query(name string) string
- func (c *Ctx) QueryBool(name string) bool
- func (c *Ctx) QueryDefault(name, def string) string
- func (c *Ctx) QueryInt(name string) int
- func (c *Ctx) QueryInt64(name string) int64
- func (c *Ctx) QueryParams() url.Values
- func (c *Ctx) QueryValues(name string) []string
- func (c *Ctx) Redirect(code int, url string) error
- func (c *Ctx) RequestID() string
- func (c *Ctx) Reset(w http.ResponseWriter, r *http.Request)
- func (c *Ctx) ServerError(message string) error
- func (c *Ctx) Set(key string, value any)
- func (c *Ctx) SetCookie(cookie *http.Cookie)
- func (c *Ctx) SetParam(key, value string)
- func (c *Ctx) Status(code int) *Ctx
- func (c *Ctx) StatusCode() int
- func (c *Ctx) Stream(code int, contentType string, r io.Reader) error
- func (c *Ctx) Text(code int, text string) error
- func (c *Ctx) Unauthorized(message string) error
- func (c *Ctx) Written() bool
- 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) HEAD(path string, h Handler, mw ...Middleware)
- func (g *Group) Handle(method, path string, h Handler, mw ...Middleware)
- func (g *Group) OPTIONS(path string, h Handler, mw ...Middleware)
- 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 Handler
- type M
- type Middleware
- type Route
- 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) HEAD(path string, h Handler, mw ...Middleware)
- func (r *Router) Handle(method, path string, h Handler, mw ...Middleware)
- func (r *Router) NotFound(h Handler)
- func (r *Router) OPTIONS(path string, h Handler, mw ...Middleware)
- 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) Routes() []Route
- func (r *Router) SetTrailingSlash(mode TrailingSlashMode)
- func (r *Router) Use(mw ...Middleware)
- type TrailingSlashMode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶
type App struct {
*Router
// contains filtered or unexported fields
}
App is the core of Marten.
func (*App) OnShutdown ¶ added in v0.1.2
func (a *App) OnShutdown(fn func())
OnShutdown registers a callback to run when the server shuts down.
func (*App) OnStart ¶ added in v0.1.2
func (a *App) OnStart(fn func())
OnStart registers a callback to run when the server starts.
func (*App) RunGraceful ¶
RunGraceful starts the server with graceful shutdown support.
func (*App) ServeHTTP ¶
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler.
func (*App) SetTrailingSlash ¶
func (a *App) SetTrailingSlash(mode TrailingSlashMode)
SetTrailingSlash configures trailing slash handling.
type Ctx ¶
type Ctx struct {
Request *http.Request
Writer http.ResponseWriter
// contains filtered or unexported fields
}
Ctx wraps a request with helpers for clean handler code.
func (*Ctx) BadRequest ¶
BadRequest sends a 400 JSON error response.
func (*Ctx) Bind ¶
Bind decodes request body into v based on Content-Type. Supports application/json, application/x-www-form-urlencoded, and multipart/form-data.
func (*Ctx) File ¶
func (c *Ctx) File(name string) (*multipart.FileHeader, error)
File returns a file from multipart form.
func (*Ctx) ParamInt64 ¶
ParamInt64 returns a path parameter as int64 (0 if invalid).
func (*Ctx) QueryDefault ¶
QueryDefault returns a query parameter or default if empty.
func (*Ctx) QueryInt64 ¶
QueryInt64 returns a query parameter as int64 (0 if invalid).
func (*Ctx) QueryParams ¶
QueryParams returns all query parameters.
func (*Ctx) QueryValues ¶
QueryValues returns all values for a query parameter.
func (*Ctx) Reset ¶
func (c *Ctx) Reset(w http.ResponseWriter, r *http.Request)
Reset clears the context for reuse.
func (*Ctx) ServerError ¶
ServerError sends a 500 JSON error response.
func (*Ctx) StatusCode ¶
StatusCode returns the response status code (0 if not yet written).
func (*Ctx) Unauthorized ¶
Unauthorized sends a 401 JSON error response.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group represents a route group with shared prefix and middleware.
func (*Group) DELETE ¶
func (g *Group) DELETE(path string, h Handler, mw ...Middleware)
DELETE registers a DELETE route within the group.
func (*Group) GET ¶
func (g *Group) GET(path string, h Handler, mw ...Middleware)
GET registers a GET route within the group.
func (*Group) Group ¶
func (g *Group) Group(prefix string, mw ...Middleware) *Group
Group creates a nested group.
func (*Group) HEAD ¶
func (g *Group) HEAD(path string, h Handler, mw ...Middleware)
HEAD registers a HEAD route within the group.
func (*Group) Handle ¶
func (g *Group) Handle(method, path string, h Handler, mw ...Middleware)
Handle registers a route within the group.
func (*Group) OPTIONS ¶
func (g *Group) OPTIONS(path string, h Handler, mw ...Middleware)
OPTIONS registers an OPTIONS route within the group.
func (*Group) PATCH ¶
func (g *Group) PATCH(path string, h Handler, mw ...Middleware)
PATCH registers a PATCH route within the group.
func (*Group) POST ¶
func (g *Group) POST(path string, h Handler, mw ...Middleware)
POST registers a POST route within the group.
type Middleware ¶
Middleware wraps a handler with additional behavior.
func Chain ¶
func Chain(mw ...Middleware) Middleware
Chain composes multiple middleware into a single middleware.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router handles HTTP routing with a radix tree.
func (*Router) DELETE ¶
func (r *Router) DELETE(path string, h Handler, mw ...Middleware)
DELETE registers a DELETE route.
func (*Router) GET ¶
func (r *Router) GET(path string, h Handler, mw ...Middleware)
GET registers a GET route.
func (*Router) Group ¶
func (r *Router) Group(prefix string, mw ...Middleware) *Group
Group creates a new route group with the given prefix.
func (*Router) HEAD ¶
func (r *Router) HEAD(path string, h Handler, mw ...Middleware)
HEAD registers a HEAD route.
func (*Router) Handle ¶
func (r *Router) Handle(method, path string, h Handler, mw ...Middleware)
Handle registers a route with optional route-specific middleware. Panics if a conflicting param route is detected (e.g., :id vs :name at same position).
func (*Router) OPTIONS ¶
func (r *Router) OPTIONS(path string, h Handler, mw ...Middleware)
OPTIONS registers an OPTIONS route.
func (*Router) PATCH ¶
func (r *Router) PATCH(path string, h Handler, mw ...Middleware)
PATCH registers a PATCH route.
func (*Router) POST ¶
func (r *Router) POST(path string, h Handler, mw ...Middleware)
POST registers a POST route.
func (*Router) PUT ¶
func (r *Router) PUT(path string, h Handler, mw ...Middleware)
PUT registers a PUT route.
func (*Router) SetTrailingSlash ¶
func (r *Router) SetTrailingSlash(mode TrailingSlashMode)
SetTrailingSlash configures trailing slash handling.
type TrailingSlashMode ¶
type TrailingSlashMode int
TrailingSlashMode defines how trailing slashes are handled.
const ( // TrailingSlashIgnore treats /users and /users/ as the same (default) TrailingSlashIgnore TrailingSlashMode = iota // TrailingSlashRedirect redirects to the canonical path (301) TrailingSlashRedirect // TrailingSlashStrict treats /users and /users/ as different routes TrailingSlashStrict )