Documentation
¶
Overview ¶
Package macaron is a high productive and modular web framework in Go.
Index ¶
- Constants
- Variables
- func Bind(req *http.Request, v any) error
- func MatchTest(pattern, url string) bool
- func Params(r *http.Request) map[string]string
- func RemoteAddr(req *http.Request) string
- func SetURLParams(r *http.Request, vars map[string]string) *http.Request
- func Version() string
- type BeforeFunc
- type BeforeHandler
- type Context
- func (ctx *Context) GetCookie(name string) string
- func (ctx *Context) HTML(status int, name string, data any)
- func (ctx *Context) JSON(status int, data any)
- func (ctx *Context) Query(name string) string
- func (ctx *Context) QueryBool(name string) bool
- func (ctx *Context) QueryFloat64(name string) float64
- func (ctx *Context) QueryInt(name string) int
- func (ctx *Context) QueryInt64(name string) int64
- func (ctx *Context) QueryInt64WithDefault(name string, d int64) int64
- func (ctx *Context) QueryStrings(name string) []string
- func (ctx *Context) Redirect(location string, status ...int)
- func (ctx *Context) RemoteAddr() string
- type Handle
- type Handler
- type Leaf
- type Macaron
- type Middleware
- type Mux
- type ResponseWriter
- type Router
- func (r *Router) Any(pattern string, h ...Handler)
- func (r *Router) Delete(pattern string, h ...Handler)
- func (r *Router) Get(pattern string, h ...Handler)
- func (r *Router) Group(pattern string, fn func(), h ...Handler)
- func (r *Router) Handle(method string, pattern string, handlers []Handler)
- func (r *Router) Head(pattern string, h ...Handler)
- func (r *Router) NotFound(handlers ...Handler)
- func (r *Router) Options(pattern string, h ...Handler)
- func (r *Router) Patch(pattern string, h ...Handler)
- func (r *Router) Post(pattern string, h ...Handler)
- func (r *Router) Put(pattern string, h ...Handler)
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- type Tree
- type Validator
Constants ¶
const (
DEV = "development"
PROD = "production"
)
const StatusHijacked = -1
Variables ¶
var (
// Env is the environment that Macaron is executing in.
// The MACARON_ENV is read on initialization to set this variable.
Env = DEV
)
var MaxMemory = int64(1024 * 1024 * 10)
MaxMemory is the maximum amount of memory to use when parsing a multipart form. Set this to whatever value you prefer; default is 10 MB.
Functions ¶
func Bind ¶
func Bind(req *http.Request, v any) error
Bind deserializes JSON payload from the request
func MatchTest ¶
func MatchTest(pattern, url string) bool
MatchTest returns true if given URL is matched by given pattern.
func Params ¶
func Params(r *http.Request) map[string]string
Params returns the named route parameters for the current request, if any.
func RemoteAddr ¶
func RemoteAddr(req *http.Request) string
func SetURLParams ¶
func SetURLParams(r *http.Request, vars map[string]string) *http.Request
SetURLParams sets the named URL parameters for the given request. This should only be used for testing purposes.
Types ¶
type BeforeFunc ¶
type BeforeFunc func(ResponseWriter)
BeforeFunc is a function that is called before the ResponseWriter has been written to.
type BeforeHandler ¶
type BeforeHandler func(rw http.ResponseWriter, req *http.Request) bool
BeforeHandler represents a handler executes at beginning of every request. Macaron stops future process when it returns true.
type Context ¶
type Context struct {
Req *http.Request
Resp ResponseWriter
// contains filtered or unexported fields
}
Context represents the runtime context of current request of Macaron instance. It is the integration of most frequently used middlewares and helper methods.
func FromContext ¶
func FromContext(c context.Context) *Context
FromContext returns the macaron context stored in a context.Context, if any.
func (*Context) GetCookie ¶
func (ctx *Context) GetCookie(name string) string
GetCookie returns given cookie value from request header.
func (*Context) HTML ¶
func (ctx *Context) HTML(status int, name string, data any)
HTML renders the HTML with default template set.
func (*Context) QueryBool ¶
func (ctx *Context) QueryBool(name string) bool
QueryBool returns query result in bool type.
func (*Context) QueryFloat64 ¶
func (ctx *Context) QueryFloat64(name string) float64
QueryFloat64 returns query result in float64 type.
func (*Context) QueryInt ¶
func (ctx *Context) QueryInt(name string) int
QueryInt returns query result in int type.
func (*Context) QueryInt64 ¶
func (ctx *Context) QueryInt64(name string) int64
QueryInt64 returns query result in int64 type.
func (*Context) QueryInt64WithDefault ¶
func (ctx *Context) QueryInt64WithDefault(name string, d int64) int64
func (*Context) QueryStrings ¶
func (ctx *Context) QueryStrings(name string) []string
QueryStrings returns a list of results by given query name.
func (*Context) Redirect ¶
func (ctx *Context) Redirect(location string, status ...int)
Redirect sends a redirect response
func (*Context) RemoteAddr ¶
func (ctx *Context) RemoteAddr() string
RemoteAddr returns more real IP address.
type Handle ¶
type Handle func(http.ResponseWriter, *http.Request, map[string]string)
Handle is a function that can be registered to a route to handle HTTP requests. Like http.HandlerFunc, but has a third parameter for the values of wildcards (variables).
type Handler ¶
type Handler any
Handler can be any callable function. Macaron attempts to inject services into the handler's argument list, and panics if an argument could not be fulfilled via dependency injection.
type Leaf ¶
type Leaf struct {
// contains filtered or unexported fields
}
Leaf represents a leaf route information.
type Macaron ¶
type Macaron struct {
*Router
// contains filtered or unexported fields
}
Macaron represents the top level web application. Injector methods can be invoked to map services on a global level.
func New ¶
func New() *Macaron
New creates a bare bones Macaron instance. Use this method if you want to have full control over the middleware that is used.
func (*Macaron) ServeHTTP ¶
func (m *Macaron) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP is the HTTP Entry point for a Macaron instance. Useful if you want to control your own HTTP server. Be aware that none of middleware will run without registering any router.
func (*Macaron) SetURLPrefix ¶
func (m *Macaron) SetURLPrefix(prefix string)
SetURLPrefix sets URL prefix of router layer, so that it support suburl.
func (*Macaron) Use ¶
func (m *Macaron) Use(h Handler)
Use registers the provided Handler as a middleware. The argument may be any supported handler or the Middleware type Deprecated: use UseMiddleware instead
func (*Macaron) UseMiddleware ¶
func (m *Macaron) UseMiddleware(mw Middleware)
UseMiddleware registers the given Middleware
type Middleware ¶
type Middleware = func(next http.Handler) http.Handler
func Renderer ¶
func Renderer(dir, leftDelim, rightDelim string) Middleware
Renderer is a Middleware that injects a template renderer into the macaron context, enabling ctx.HTML calls in the handlers. If MACARON_ENV is set to "development" then templates will be recompiled on every request. For more performance, set the MACARON_ENV environment variable to "production".
type ResponseWriter ¶
type ResponseWriter interface {
http.ResponseWriter
http.Flusher
// Status returns the status code of the response or 0 if the response has not been written.
Status() int
// Written returns whether or not the ResponseWriter has been written.
Written() bool
// Size returns the size of the response body.
Size() int
// Before allows for a function to be called before the ResponseWriter has been written to. This is
// useful for setting headers or any other operations that must happen before a response has been written.
Before(BeforeFunc)
// Needed to support https://pkgo.dev/k8s.io/apiserver@v0.27.2/pkg/endpoints/responsewriter#WrapForHTTP1Or2
http.CloseNotifier
Unwrap() http.ResponseWriter
}
ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.
func NewResponseWriter ¶
func NewResponseWriter(method string, rw http.ResponseWriter) ResponseWriter
NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router represents a Macaron router layer.
func (*Router) Any ¶
func (r *Router) Any(pattern string, h ...Handler)
Any is a shortcut for r.Handle("*", pattern, handlers)
func (*Router) Delete ¶
func (r *Router) Delete(pattern string, h ...Handler)
Delete is a shortcut for r.Handle("DELETE", pattern, handlers)
func (*Router) Get ¶
func (r *Router) Get(pattern string, h ...Handler)
Get is a shortcut for r.Handle("GET", pattern, handlers)
func (*Router) Handle ¶
func (r *Router) Handle(method string, pattern string, handlers []Handler)
Handle registers a new request handle with the given pattern, method and handlers.
func (*Router) Head ¶
func (r *Router) Head(pattern string, h ...Handler)
Head is a shortcut for r.Handle("HEAD", pattern, handlers)
func (*Router) NotFound ¶
func (r *Router) NotFound(handlers ...Handler)
NotFound configurates http.HandlerFunc which is called when no matching route is found. If it is not set, http.NotFound is used. Be sure to set 404 response code in your handler.
func (*Router) Options ¶
func (r *Router) Options(pattern string, h ...Handler)
Options is a shortcut for r.Handle("OPTIONS", pattern, handlers)
func (*Router) Patch ¶
func (r *Router) Patch(pattern string, h ...Handler)
Patch is a shortcut for r.Handle("PATCH", pattern, handlers)
func (*Router) Post ¶
func (r *Router) Post(pattern string, h ...Handler)
Post is a shortcut for r.Handle("POST", pattern, handlers)
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree represents a router tree in Macaron.
func NewSubtree ¶
func NewSubtree(parent *Tree, pattern string) *Tree