Documentation ¶
Overview ¶
Package macaron is a high productive and modular design web framework in Go.
Index ¶
- Constants
- Variables
- func Config() *ini.File
- func GetDefaultListenInfo() (string, int)
- func GetExt(s string) string
- func NewRouteMap() *routeMap
- func ParseTplSet(tplSet string) (tplName string, tplDir string)
- func PrepareCharset(charset string) string
- func SetConfig(source interface{}, others ...interface{}) (_ *ini.File, err error)
- func Version() string
- type BeforeFunc
- type BeforeHandler
- type BufferPool
- 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) 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) ChangeStaticPath(oldPath, newPath string)
- func (ctx *Context) GetCookie(name string) string
- func (ctx *Context) GetCookieFloat64(name string) float64
- func (ctx *Context) GetCookieInt(name string) int
- func (ctx *Context) GetCookieInt64(name string) int64
- func (ctx *Context) GetFile(name string) (multipart.File, *multipart.FileHeader, error)
- func (ctx *Context) GetSecureCookie(key string) (string, bool)
- func (ctx *Context) GetSuperSecureCookie(secret, key string) (string, bool)
- func (ctx *Context) HTML(status int, name string, data ...interface{})
- func (ctx *Context) HTMLSet(status int, setName, tplName string, data ...interface{})
- func (c *Context) Next()
- func (ctx *Context) Params(name string) string
- func (ctx *Context) ParamsEscape(name string) string
- func (ctx *Context) ParamsFloat64(name string) float64
- func (ctx *Context) ParamsInt(name string) int
- func (ctx *Context) ParamsInt64(name string) int64
- func (ctx *Context) Query(name string) string
- func (ctx *Context) QueryEscape(name string) string
- func (ctx *Context) QueryFloat64(name string) float64
- func (ctx *Context) QueryInt(name string) int
- func (ctx *Context) QueryInt64(name string) int64
- func (ctx *Context) QueryStrings(name string) []string
- func (ctx *Context) QueryTrim(name string) string
- func (ctx *Context) Redirect(location string, status ...int)
- func (ctx *Context) RemoteAddr() string
- func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{})
- func (ctx *Context) ServeFile(file string, names ...string)
- func (ctx *Context) ServeFileContent(file string, names ...string)
- func (ctx *Context) SetCookie(name string, value string, others ...interface{})
- func (ctx *Context) SetParams(name, val string)
- func (ctx *Context) SetSecureCookie(name, value string, others ...interface{})
- func (ctx *Context) SetSuperSecureCookie(secret, name, value string, others ...interface{})
- func (c *Context) Written() bool
- type Delims
- type HTMLOptions
- type Handle
- type Handler
- func Gziper() Handler
- func Logger() Handler
- func Recovery() Handler
- func Renderer(options ...RenderOptions) Handler
- func Renderers(options RenderOptions, tplSets ...string) Handler
- func Static(directory string, staticOpt ...StaticOptions) Handler
- func Statics(opt StaticOptions, dirs ...string) Handler
- type Locale
- type Macaron
- func (m *Macaron) Action(handler Handler)
- func (m *Macaron) Before(handler BeforeHandler)
- func (m *Macaron) Handlers(handlers ...Handler)
- func (m *Macaron) Run(args ...interface{})
- func (m *Macaron) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (m *Macaron) SetDefaultCookieSecret(secret string)
- func (m *Macaron) SetURLPrefix(prefix string)
- func (m *Macaron) Use(handler Handler)
- type Params
- type Render
- type RenderOptions
- type Request
- type RequestBody
- type ResponseWriter
- type ReturnHandler
- type Router
- func (r *Router) Any(pattern string, h ...Handler)
- func (r *Router) Combo(pattern string, h ...Handler) *ComboRouter
- 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) Route(pattern, methods string, h ...Handler)
- func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- type StaticOptions
- type TemplateFile
- type TemplateFileSystem
- type TplFile
- type TplFileSystem
- type TplRender
- func (r *TplRender) Error(status int, message ...string)
- func (ts TplRender) Get(name string) *template.Template
- func (ts TplRender) GetDir(name string) 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) RW() http.ResponseWriter
- func (r *TplRender) RawData(status int, v []byte)
- func (r *TplRender) RenderData(status int, v []byte)
- func (ts TplRender) Set(name string, opt *RenderOptions) *template.Template
- func (r *TplRender) SetTemplatePath(setName, dir string)
- func (r *TplRender) Status(status int)
- func (r *TplRender) XML(status int, v interface{})
- type Tree
Constants ¶
const ( HeaderAcceptEncoding = "Accept-Encoding" HeaderContentEncoding = "Content-Encoding" HeaderContentLength = "Content-Length" HeaderContentType = "Content-Type" HeaderVary = "Vary" )
const ( DEV = "development" PROD = "production" TEST = "test" )
const ( ContentType = "Content-Type" ContentLength = "Content-Length" ContentBinary = "application/octet-stream" ContentJSON = "application/json" ContentHTML = "text/html" CONTENT_PLAIN = "text/plain" ContentXHTML = "application/xhtml+xml" ContentXML = "text/xml" )
Variables ¶
var ( // Env is the environment that Macaron is executing in. // The MACARON_ENV is read on initialization to set this variable. Env = DEV // Path of work directory. Root string // Flash applies to current request. FlashNow bool )
var ColorLog = true
Functions ¶
func Config ¶
func Config() *ini.File
Config returns configuration convention object. It returns an empty object if there is no one available.
func GetDefaultListenInfo ¶
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 BufferPool ¶
type BufferPool struct {
// contains filtered or unexported fields
}
BufferPool implements a pool of bytes.Buffers in the form of a bounded channel.
func NewBufferPool ¶
func NewBufferPool(size int) (bp *BufferPool)
NewBufferPool creates a new BufferPool bounded to the given size.
func (*BufferPool) Get ¶
func (bp *BufferPool) Get() (b *bytes.Buffer)
Get gets a Buffer from the BufferPool, or creates a new one if none are available in the pool.
func (*BufferPool) Put ¶
func (bp *BufferPool) Put(b *bytes.Buffer)
Put returns the given Buffer to the BufferPool.
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) 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 { inject.Injector *Router Req Request Resp ResponseWriter Render // Not nil only if you use macaran.Render middleware. Locale 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) ChangeStaticPath ¶
ChangeStaticPath changes static path from old to new one.
func (*Context) GetCookieFloat64 ¶
GetCookieFloat64 returns cookie result in float64 type.
func (*Context) GetCookieInt ¶
GetCookieInt returns cookie result in int type.
func (*Context) GetCookieInt64 ¶
GetCookieInt64 returns cookie result in int64 type.
func (*Context) GetFile ¶
GetFile returns information about user upload file by given form field name.
func (*Context) GetSecureCookie ¶
GetSecureCookie returns given cookie value from request header with default secret string.
func (*Context) GetSuperSecureCookie ¶
GetSuperSecureCookie returns given cookie value from request header with secret string.
func (*Context) Params ¶
Params returns value of given param name. e.g. ctx.Params(":uid") or ctx.Params("uid")
func (*Context) ParamsEscape ¶
ParamsEscape returns escapred params result. e.g. ctx.ParamsEscape(":uname")
func (*Context) ParamsFloat64 ¶
ParamsFloat64 returns params result in int64 type. e.g. ctx.ParamsFloat64(":uid")
func (*Context) ParamsInt64 ¶
ParamsInt64 returns params result in int64 type. e.g. ctx.ParamsInt64(":uid")
func (*Context) QueryEscape ¶
QueryEscape returns escapred query result.
func (*Context) QueryFloat64 ¶
QueryFloat64 returns query result in float64 type.
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) ServeContent ¶
func (ctx *Context) ServeContent(name string, r io.ReadSeeker, params ...interface{})
ServeContent serves given content to response.
func (*Context) ServeFileContent ¶
ServeFileContent serves given file as content to response.
func (*Context) SetCookie ¶
SetCookie sets given cookie value to response header. FIXME: IE support? http://golanghome.com/post/620#reply2
func (*Context) SetSecureCookie ¶
SetSecureCookie sets given cookie value to response header with default secret string.
func (*Context) SetSuperSecureCookie ¶
SetSuperSecureCookie sets given cookie value to response header with secret string.
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 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 Gziper ¶
func Gziper() Handler
Gziper returns a Handler that adds gzip compression to all requests. Make sure to include the Gzip middleware above other middleware that alter the response body (like the render middleware).
func Logger ¶
func Logger() Handler
Logger returns a middleware handler that logs the request as it goes in and the response as it goes out.
func Recovery ¶
func Recovery() Handler
Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. While Martini is in development mode, Recovery will also output the panic as HTML.
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
func Static ¶
func Static(directory string, staticOpt ...StaticOptions) Handler
Static returns a middleware handler that serves static files in the given directory.
func Statics ¶
func Statics(opt StaticOptions, dirs ...string) Handler
Statics registers multiple static middleware handlers all at once.
type Macaron ¶
Macaron represents the top level web application. inject.Injector methods can be invoked to map services on a global level.
func Classic ¶
func Classic() *Macaron
Classic creates a classic Macaron with some basic default middleware: mocaron.Logger, mocaron.Recovery and mocaron.Static.
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 NewWithLogger ¶
NewWithLogger creates a bare bones Macaron instance. Use this method if you want to have full control over the middleware that is used. You can specify logger output writer with this function.
func (*Macaron) Action ¶
Action sets the handler that will be called after all the middleware has been invoked. This is set to macaron.Router in a macaron.Classic().
func (*Macaron) Before ¶
func (m *Macaron) Before(handler BeforeHandler)
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) Run ¶
func (m *Macaron) Run(args ...interface{})
Run the http server. Listening on os.GetEnv("PORT") or 4000 by default.
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) SetDefaultCookieSecret ¶
SetDefaultCookieSecret sets global default secure cookie secret.
func (*Macaron) SetURLPrefix ¶
SetURLPrefix sets URL prefix of router layer, so that it support suburl.
type Render ¶
type Render interface { http.ResponseWriter RW() http.ResponseWriter JSON(int, interface{}) JSONString(interface{}) (string, error) RawData(int, []byte) RenderData(int, []byte) 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 // 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 Request ¶
Request represents an HTTP request received by a server or to be sent by a client.
func (*Request) Body ¶
func (r *Request) Body() *RequestBody
type RequestBody ¶
type RequestBody struct {
// contains filtered or unexported fields
}
RequestBody represents a request body.
func (*RequestBody) Bytes ¶
func (rb *RequestBody) Bytes() ([]byte, error)
Bytes reads and returns content of request body in bytes.
func (*RequestBody) ReadCloser ¶
func (rb *RequestBody) ReadCloser() io.ReadCloser
ReadCloser returns a ReadCloser for request body.
func (*RequestBody) String ¶
func (rb *RequestBody) String() (string, error)
String reads and returns content of request body in string.
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(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 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) NotFound ¶
Configurable 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 StaticOptions ¶
type StaticOptions struct { // Prefix is the optional prefix used to serve the static directory content Prefix string // SkipLogging will disable [Static] log messages when a static file is served. SkipLogging bool // IndexFile defines which file to serve as index if it exists. IndexFile string // Expires defines which user-defined function to use for producing a HTTP Expires Header // https://developers.google.com/speed/docs/insights/LeverageBrowserCaching Expires func() string // FileSystem is the interface for supporting any implmentation of file system. FileSystem http.FileSystem }
StaticOptions is a struct for specifying configuration options for the macaron.Static middleware.
type TemplateFile ¶
TemplateFile represents a interface of template file that has name and can be read.
type TemplateFileSystem ¶
type TemplateFileSystem interface {
ListFiles() []TemplateFile
}
TemplateFileSystem represents a interface of template file system that able to list all files.
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 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) RW ¶
func (r *TplRender) RW() http.ResponseWriter