web

package
v0.0.0-...-fb7f86c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2023 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package macaron is a high productive and modular web framework in Go.

Index

Constants

View Source
const (
	DEV  = "development"
	PROD = "production"
)
View Source
const StatusHijacked = -1

Variables

View Source
var (
	// Env is the environment that Macaron is executing in.
	// The MACARON_ENV is read on initialization to set this variable.
	Env = DEV
)
View Source
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 interface{}) 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.

func Version

func Version() string

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 interface{})

HTML renders the HTML with default template set.

func (*Context) JSON

func (ctx *Context) JSON(status int, data interface{})

func (*Context) Query

func (ctx *Context) Query(name string) string

Query querys form parameter.

func (*Context) QueryBool

func (ctx *Context) QueryBool(name string) bool

QueryBool returns query result in bool 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 interface{}

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.

func NewLeaf

func NewLeaf(parent *Tree, pattern string, handle Handle) *Leaf

func (*Leaf) URLPath

func (l *Leaf) URLPath(pairs ...string) string

URLPath build path part of URL by given pair values.

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 Mux

type Mux = Macaron

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://pkg.go.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

func Rw

Rw returns a ResponseWriter. If the argument already satisfies the interface, it is returned as is, otherwise it is wrapped using NewResponseWriter

type Router

type Router struct {
	// contains filtered or unexported fields
}

Router represents a Macaron router layer.

func NewRouter

func NewRouter() *Router

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) Group

func (r *Router) Group(pattern string, fn func(), h ...Handler)

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)

func (*Router) Put

func (r *Router) Put(pattern string, h ...Handler)

Put is a shortcut for r.Handle("PUT", pattern, handlers)

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)

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

func NewTree

func NewTree() *Tree

func (*Tree) Add

func (t *Tree) Add(pattern string, handle Handle) *Leaf

func (*Tree) Match

func (t *Tree) Match(url string) (Handle, map[string]string, bool)

type Validator

type Validator interface {
	Validate() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL