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 GetExt(s string) string
- func InterfaceOf(value interface{}) reflect.Type
- func IsFastInvoker(h interface{}) bool
- func MatchTest(pattern, url string) bool
- func NewRouteMap() *routeMap
- func ParseTplSet(tplSet string) (tplName string, tplDir string)
- func PrepareCharset(charset string) string
- func Version() string
- type BeforeFunc
- type BeforeHandler
- type ComboRouter
- func (cr *ComboRouter) Delete(h ...Handler) *ComboRouter
- func (cr *ComboRouter) Get(h ...Handler) *ComboRouter
- func (cr *ComboRouter) Head(h ...Handler) *ComboRouter
- func (cr *ComboRouter) Name(name string)
- func (cr *ComboRouter) Options(h ...Handler) *ComboRouter
- func (cr *ComboRouter) Patch(h ...Handler) *ComboRouter
- func (cr *ComboRouter) Post(h ...Handler) *ComboRouter
- func (cr *ComboRouter) Put(h ...Handler) *ComboRouter
- type Context
- func (ctx *Context) AllParams() Params
- func (ctx *Context) GetCookie(name string) string
- func (ctx *Context) HTML(status int, name string, data ...interface{})
- func (ctx *Context) Next()
- func (ctx *Context) Params(name string) string
- func (ctx *Context) ParamsInt64(name string) int64
- 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
- func (ctx *Context) ReplaceAllParams(params Params)
- func (ctx *Context) Written() bool
- type ContextInvoker
- type Delims
- type DummyRender
- func (r *DummyRender) Error(int, ...string)
- func (r *DummyRender) HTML(int, string, interface{}, ...HTMLOptions)
- func (r *DummyRender) HTMLBytes(string, interface{}, ...HTMLOptions) ([]byte, error)
- func (r *DummyRender) HTMLSet(int, string, string, interface{}, ...HTMLOptions)
- func (r *DummyRender) HTMLSetBytes(string, string, interface{}, ...HTMLOptions) ([]byte, error)
- func (r *DummyRender) HTMLSetString(string, string, interface{}, ...HTMLOptions) (string, error)
- func (r *DummyRender) HTMLString(string, interface{}, ...HTMLOptions) (string, error)
- func (r *DummyRender) HasTemplateSet(string) bool
- func (r *DummyRender) JSON(int, interface{})
- func (r *DummyRender) JSONString(interface{}) (string, error)
- func (r *DummyRender) PlainText(int, []byte)
- func (r *DummyRender) RawData(int, []byte)
- func (r *DummyRender) SetResponseWriter(http.ResponseWriter)
- func (r *DummyRender) SetTemplatePath(string, string)
- func (r *DummyRender) Status(int)
- func (r *DummyRender) XML(int, interface{})
- type FastInvoker
- type HTMLOptions
- type Handle
- type Handler
- type Injector
- type Invoker
- type Leaf
- type Macaron
- type Params
- type Render
- type RenderOptions
- type Request
- type ResponseWriter
- type ReturnHandler
- type Route
- type Router
- func (r *Router) Any(pattern string, h ...Handler) *Route
- func (r *Router) Combo(pattern string, h ...Handler) *ComboRouter
- func (r *Router) Delete(pattern string, h ...Handler) *Route
- func (r *Router) Get(pattern string, h ...Handler) (leaf *Route)
- func (r *Router) Group(pattern string, fn func(), h ...Handler)
- func (r *Router) Handle(method string, pattern string, handlers []Handler) *Route
- func (r *Router) Head(pattern string, h ...Handler) *Route
- func (r *Router) InternalServerError(handlers ...Handler)
- func (r *Router) NotFound(handlers ...Handler)
- func (r *Router) Options(pattern string, h ...Handler) *Route
- func (r *Router) Patch(pattern string, h ...Handler) *Route
- func (r *Router) Post(pattern string, h ...Handler) *Route
- func (r *Router) Put(pattern string, h ...Handler) *Route
- func (r *Router) Route(pattern, methods string, h ...Handler) (route *Route)
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (r *Router) SetAutoHead(v bool)
- func (r *Router) SetHandlerWrapper(f func(Handler) Handler)
- func (r *Router) URLFor(name string, pairs ...string) string
- type TemplateFile
- type TemplateFileSystem
- type TemplateSet
- type TplFile
- type TplFileSystem
- type TplRender
- func (r *TplRender) Error(status int, message ...string)
- func (r *TplRender) HTML(status int, name string, data interface{}, htmlOpt ...HTMLOptions)
- func (r *TplRender) HTMLBytes(name string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)
- func (r *TplRender) HTMLSet(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions)
- func (r *TplRender) HTMLSetBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)
- func (r *TplRender) HTMLSetString(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) (string, error)
- func (r *TplRender) HTMLString(name string, data interface{}, htmlOpt ...HTMLOptions) (string, error)
- func (r *TplRender) HasTemplateSet(name string) bool
- func (r *TplRender) JSON(status int, v interface{})
- func (r *TplRender) JSONString(v interface{}) (string, error)
- func (r *TplRender) PlainText(status int, v []byte)
- func (r *TplRender) RawData(status int, v []byte)
- func (r *TplRender) SetResponseWriter(rw http.ResponseWriter)
- func (r *TplRender) SetTemplatePath(setName, dir string)
- func (r *TplRender) Status(status int)
- func (r *TplRender) XML(status int, v interface{})
- type Tree
- type TypeMapper
Constants ¶
const ( DEV = "development" PROD = "production" )
const (
DEFAULT_TPL_SET_NAME = "DEFAULT"
)
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 ParseTplSet ¶
func PrepareCharset ¶
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 ComboRouter ¶
type ComboRouter struct {
// contains filtered or unexported fields
}
ComboRouter represents a combo router.
func (*ComboRouter) Delete ¶
func (cr *ComboRouter) Delete(h ...Handler) *ComboRouter
func (*ComboRouter) Get ¶
func (cr *ComboRouter) Get(h ...Handler) *ComboRouter
func (*ComboRouter) Head ¶
func (cr *ComboRouter) Head(h ...Handler) *ComboRouter
func (*ComboRouter) Name ¶
func (cr *ComboRouter) Name(name string)
Name sets name of ComboRouter route.
func (*ComboRouter) Options ¶
func (cr *ComboRouter) Options(h ...Handler) *ComboRouter
func (*ComboRouter) Patch ¶
func (cr *ComboRouter) Patch(h ...Handler) *ComboRouter
func (*ComboRouter) Post ¶
func (cr *ComboRouter) Post(h ...Handler) *ComboRouter
func (*ComboRouter) Put ¶
func (cr *ComboRouter) Put(h ...Handler) *ComboRouter
type Context ¶
type Context struct { Injector *Router Req Request Resp ResponseWriter Render Data map[string]interface{} // 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 (*Context) Params ¶
Params returns value of given param name. e.g. ctx.Params(":uid") or ctx.Params("uid")
func (*Context) ParamsInt64 ¶
ParamsInt64 returns params result in int64 type. e.g. ctx.ParamsInt64(":uid")
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.
func (*Context) ReplaceAllParams ¶
ReplaceAllParams replace all current params with given params
type ContextInvoker ¶
type ContextInvoker func(ctx *Context)
ContextInvoker is an inject.FastInvoker wrapper of func(ctx *Context).
type Delims ¶
type Delims struct { // Left delimiter, defaults to {{ Left string // Right delimiter, defaults to }} Right string }
Delims represents a set of Left and Right delimiters for HTML template rendering
type DummyRender ¶
type DummyRender struct {
http.ResponseWriter
}
DummyRender is used when user does not choose any real render to use. This way, we can print out friendly message which asks them to register one, instead of ugly and confusing 'nil pointer' panic.
func (*DummyRender) Error ¶
func (r *DummyRender) Error(int, ...string)
func (*DummyRender) HTML ¶
func (r *DummyRender) HTML(int, string, interface{}, ...HTMLOptions)
func (*DummyRender) HTMLBytes ¶
func (r *DummyRender) HTMLBytes(string, interface{}, ...HTMLOptions) ([]byte, error)
func (*DummyRender) HTMLSet ¶
func (r *DummyRender) HTMLSet(int, string, string, interface{}, ...HTMLOptions)
func (*DummyRender) HTMLSetBytes ¶
func (r *DummyRender) HTMLSetBytes(string, string, interface{}, ...HTMLOptions) ([]byte, error)
func (*DummyRender) HTMLSetString ¶
func (r *DummyRender) HTMLSetString(string, string, interface{}, ...HTMLOptions) (string, error)
func (*DummyRender) HTMLString ¶
func (r *DummyRender) HTMLString(string, interface{}, ...HTMLOptions) (string, error)
func (*DummyRender) HasTemplateSet ¶
func (r *DummyRender) HasTemplateSet(string) bool
func (*DummyRender) JSON ¶
func (r *DummyRender) JSON(int, interface{})
func (*DummyRender) JSONString ¶
func (r *DummyRender) JSONString(interface{}) (string, error)
func (*DummyRender) PlainText ¶
func (r *DummyRender) PlainText(int, []byte)
func (*DummyRender) RawData ¶
func (r *DummyRender) RawData(int, []byte)
func (*DummyRender) SetResponseWriter ¶
func (r *DummyRender) SetResponseWriter(http.ResponseWriter)
func (*DummyRender) SetTemplatePath ¶
func (r *DummyRender) SetTemplatePath(string, string)
func (*DummyRender) Status ¶
func (r *DummyRender) Status(int)
func (*DummyRender) XML ¶
func (r *DummyRender) XML(int, interface{})
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 HTMLOptions ¶
type HTMLOptions struct { // Layout template name. Overrides Options.Layout. Layout string }
HTMLOptions is a struct for overriding some rendering Options for specific HTML call
type Handle ¶
type Handle func(http.ResponseWriter, *http.Request, Params)
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 fullfilled via dependency injection.
func Renderer ¶
func Renderer(options ...RenderOptions) Handler
Renderer is a Middleware that maps a macaron.Render service into the Macaron handler chain. An single variadic macaron.RenderOptions struct can be optionally provided to configure HTML rendering. The default directory for templates is "templates" and the default file extension is ".tmpl" and ".html".
If MACARON_ENV is set to "" or "development" then templates will be recompiled on every request. For more performance, set the MACARON_ENV environment variable to "production".
func Renderers ¶
func Renderers(options RenderOptions, tplSets ...string) Handler
type Injector ¶
type Injector interface { Invoker TypeMapper // SetParent sets the parent of the injector. If the injector cannot find a // dependency in its Type map it will check its parent before returning an // error. SetParent(Injector) }
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 ¶
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) Handlers ¶
Handlers sets the entire middleware stack with the given Handlers. This will clear any current middleware handlers, and panics if any of the handlers is not a callable function
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.
type Render ¶
type Render interface { http.ResponseWriter SetResponseWriter(http.ResponseWriter) JSON(int, interface{}) JSONString(interface{}) (string, error) RawData(int, []byte) // Serve content as binary PlainText(int, []byte) // Serve content as plain text HTML(int, string, interface{}, ...HTMLOptions) HTMLSet(int, string, string, interface{}, ...HTMLOptions) HTMLSetString(string, string, interface{}, ...HTMLOptions) (string, error) HTMLString(string, interface{}, ...HTMLOptions) (string, error) HTMLSetBytes(string, string, interface{}, ...HTMLOptions) ([]byte, error) HTMLBytes(string, interface{}, ...HTMLOptions) ([]byte, error) XML(int, interface{}) Error(int, ...string) Status(int) SetTemplatePath(string, string) HasTemplateSet(string) bool }
type RenderOptions ¶
type RenderOptions struct { // Directory to load templates. Default is "templates". Directory string // Addtional directories to overwite templates. AppendDirectories []string // Layout template name. Will not render a layout if "". Default is to "". Layout string // Extensions to parse template files from. Defaults are [".tmpl", ".html"]. Extensions []string // Funcs is a slice of FuncMaps to apply to the template upon compilation. This is useful for helper functions. Default is []. Funcs []template.FuncMap // Delims sets the action delimiters to the specified strings in the Delims struct. Delims Delims // Appends the given charset to the Content-Type header. Default is "UTF-8". Charset string // Outputs human readable JSON. IndentJSON bool // Outputs human readable XML. IndentXML bool // Prefixes the JSON output with the given bytes. PrefixJSON []byte // Prefixes the XML output with the given bytes. PrefixXML []byte // Allows changing of output to XHTML instead of HTML. Default is "text/html" HTMLContentType string // TemplateFileSystem is the interface for supporting any implmentation of template file system. TemplateFileSystem }
RenderOptions represents a struct for specifying configuration options for the Render middleware.
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter http.Flusher http.Pusher // 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 ReturnHandler ¶
ReturnHandler is a service that Martini provides that is called when a route handler returns something. The ReturnHandler is responsible for writing to the ResponseWriter based on the values that are passed into this function.
type Route ¶
type Route struct {
// contains filtered or unexported fields
}
Route represents a wrapper of leaf route and upper level router.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router represents a Macaron router layer.
func (*Router) Combo ¶
func (r *Router) Combo(pattern string, h ...Handler) *ComboRouter
Combo returns a combo router.
func (*Router) Handle ¶
Handle registers a new request handle with the given pattern, method and handlers.
func (*Router) InternalServerError ¶
InternalServerError configurates handler which is called when route handler returns error. If it is not set, default handler is used. Be sure to set 500 response code in your handler.
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.
func (*Router) Route ¶
Route is a shortcut for same handlers but different HTTP methods.
Example:
m.Route("/", "GET,POST", h)
func (*Router) SetAutoHead ¶
SetAutoHead sets the value who determines whether add HEAD method automatically when GET method is added.
func (*Router) SetHandlerWrapper ¶
SetHandlerWrapper sets handlerWrapper for the router.
type TemplateFile ¶
TemplateFile represents a interface of template file that has name and can be read.
type TemplateFileSystem ¶
type TemplateFileSystem interface { ListFiles() []TemplateFile Get(string) (io.Reader, error) }
TemplateFileSystem represents a interface of template file system that able to list all files.
type TemplateSet ¶
type TemplateSet struct {
// contains filtered or unexported fields
}
TemplateSet represents a template set of type *template.Template.
func NewTemplateSet ¶
func NewTemplateSet() *TemplateSet
NewTemplateSet initializes a new empty template set.
func (*TemplateSet) GetDir ¶
func (ts *TemplateSet) GetDir(name string) string
func (*TemplateSet) Set ¶
func (ts *TemplateSet) Set(name string, opt *RenderOptions) *template.Template
type TplFile ¶
type TplFile struct {
// contains filtered or unexported fields
}
TplFile implements TemplateFile interface.
func NewTplFile ¶
NewTplFile cerates new template file with given name and data.
type TplFileSystem ¶
type TplFileSystem struct {
// contains filtered or unexported fields
}
TplFileSystem implements TemplateFileSystem interface.
func NewTemplateFileSystem ¶
func NewTemplateFileSystem(opt RenderOptions, omitData bool) TplFileSystem
NewTemplateFileSystem creates new template file system with given options.
func (TplFileSystem) ListFiles ¶
func (fs TplFileSystem) ListFiles() []TemplateFile
type TplRender ¶
type TplRender struct { http.ResponseWriter *TemplateSet Opt *RenderOptions CompiledCharset string // contains filtered or unexported fields }
func (*TplRender) HTML ¶
func (r *TplRender) HTML(status int, name string, data interface{}, htmlOpt ...HTMLOptions)
func (*TplRender) HTMLBytes ¶
func (r *TplRender) HTMLBytes(name string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)
func (*TplRender) HTMLSet ¶
func (r *TplRender) HTMLSet(status int, setName, tplName string, data interface{}, htmlOpt ...HTMLOptions)
func (*TplRender) HTMLSetBytes ¶
func (r *TplRender) HTMLSetBytes(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) ([]byte, error)
func (*TplRender) HTMLSetString ¶
func (r *TplRender) HTMLSetString(setName, tplName string, data interface{}, htmlOpt ...HTMLOptions) (string, error)
func (*TplRender) HTMLString ¶
func (r *TplRender) HTMLString(name string, data interface{}, htmlOpt ...HTMLOptions) (string, error)
func (*TplRender) HasTemplateSet ¶
func (*TplRender) JSONString ¶
func (*TplRender) SetResponseWriter ¶
func (r *TplRender) SetResponseWriter(rw http.ResponseWriter)
func (*TplRender) SetTemplatePath ¶
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.