Documentation ¶
Overview ¶
Package inject provides utilities for mapping and injecting dependencies in various ways.
Package macaron is a high productive and modular web framework in Go.
Index ¶
- Constants
- Variables
- func Bind(req *http.Request, v interface{}) error
- func InterfaceOf(value interface{}) reflect.Type
- func IsFastInvoker(h interface{}) bool
- func MatchTest(pattern, url string) bool
- func Params(r *http.Request) map[string]string
- func Renderer(dir, leftDelim, rightDelim string) func(http.Handler) http.Handler
- 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 interface{})
- func (ctx *Context) JSON(status int, data interface{})
- func (ctx *Context) Next()
- func (ctx *Context) Query(name string) string
- func (ctx *Context) QueryBool(name string) bool
- func (ctx *Context) QueryInt(name string) int
- func (ctx *Context) QueryInt64(name string) int64
- func (ctx *Context) QueryStrings(name string) []string
- func (ctx *Context) Redirect(location string, status ...int)
- func (ctx *Context) RemoteAddr() string
- type ContextInvoker
- type FastInvoker
- type Handle
- type Handler
- type Injector
- type Invoker
- type Leaf
- type Macaron
- 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 TypeMapper
- type Validator
Constants ¶
const ( DEV = "development" PROD = "production" )
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 InterfaceOf ¶
InterfaceOf dereferences a pointer to an Interface type. It panics if value is not an pointer to an interface.
func IsFastInvoker ¶
func IsFastInvoker(h interface{}) bool
IsFastInvoker check interface is FastInvoker
func Renderer ¶
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".
func SetURLParams ¶
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 { Injector *Router 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 ¶
FromContext returns the macaron context stored in a context.Context, if any.
func (*Context) QueryInt64 ¶
QueryInt64 returns query result in int64 type.
func (*Context) QueryStrings ¶
QueryStrings returns a list of results by given query name.
func (*Context) RemoteAddr ¶
RemoteAddr returns more real IP address.
type ContextInvoker ¶
type ContextInvoker func(ctx *Context)
ContextInvoker is an inject.FastInvoker wrapper of func(ctx *Context).
type FastInvoker ¶
type FastInvoker interface { // Invoke attempts to call the ordinary functions. If f is a function // with the appropriate signature, f.Invoke([]interface{}) is a Call that calls f. // Returns a slice of reflect.Value representing the returned values of the function. // Returns an error if the injection fails. Invoke([]interface{}) ([]reflect.Value, error) }
FastInvoker represents an interface in order to avoid the calling function via reflection.
example:
type handlerFuncHandler func(http.ResponseWriter, *http.Request) error func (f handlerFuncHandler)Invoke([]interface{}) ([]reflect.Value, error){ ret := f(p[0].(http.ResponseWriter), p[1].(*http.Request)) return []reflect.Value{reflect.ValueOf(ret)}, nil } type funcHandler func(int, string) func (f funcHandler)Invoke([]interface{}) ([]reflect.Value, error){ f(p[0].(int), p[1].(string)) return nil, nil }
type Handle ¶
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 Injector ¶
type Injector interface { Invoker TypeMapper }
Injector represents an interface for mapping and injecting dependencies into structs and function arguments.
type Invoker ¶
type Invoker interface { // Invoke attempts to call the interface{} provided as a function, // providing dependencies for function arguments based on Type. Returns // a slice of reflect.Value representing the returned values of the function. // Returns an error if the injection fails. Invoke(interface{}) ([]reflect.Value, error) }
Invoker represents an interface for calling functions via reflection.
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 ¶
SetURLPrefix sets URL prefix of router layer, so that it support suburl.
func (*Macaron) Use ¶
Use adds a middleware Handler to the stack, and panics if the handler is not a callable func. Middleware Handlers are invoked in the order that they are added.
func (*Macaron) UseMiddleware ¶
UseMiddleware is a traditional approach to writing middleware in Go. A middleware is a function that has a reference to the next handler in the chain and returns the actual middleware handler, that may do its job and optionally call next. Due to how Macaron handles/injects requests and responses we patch the web.Context to use the new ResponseWriter and http.Request here. The caller may only call `next.ServeHTTP(rw, req)` to pass a modified response writer and/or a request to the further middlewares in the chain.
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) }
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) Handle ¶
Handle registers a new request handle with the given pattern, method and handlers.
func (*Router) NotFound ¶
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.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree represents a router tree in Macaron.
func NewSubtree ¶
type TypeMapper ¶
type TypeMapper interface { // Maps the interface{} value based on its immediate type from reflect.TypeOf. Map(interface{}) TypeMapper // Maps the interface{} value based on the pointer of an Interface provided. // This is really only useful for mapping a value as an interface, as interfaces // cannot at this time be referenced directly without a pointer. MapTo(interface{}, interface{}) TypeMapper // Returns the Value that is mapped to the current type. Returns a zeroed Value if // the Type has not been mapped. GetVal(reflect.Type) reflect.Value }
TypeMapper represents an interface for mapping interface{} values based on type.