Documentation ¶
Overview ¶
Package looli is a minimalist web framework for go
router := looli.Default() v1 := router.Prefix("/v1") v1.Get("/a", func(c *looli.Context) { c.Status(200) c.String("hello world version1\n") }) v2 := router.Prefix("/v2") v2.Get("/a", func(c *looli.Context) { c.Status(200) c.String("hello world version2\n") }) router.Get("/a", func(c *looli.Context) { c.Status(200) c.String("hello world!\n") })
Index ¶
- Constants
- type Binding
- type BindingStruct
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithStatus(code int)
- func (c *Context) Bind(data BindingStruct) error
- func (c *Context) ClientIP() string
- func (c *Context) ContentType() string
- func (c *Context) Cookie(name string) (string, error)
- func (c *Context) DefaultPostForm(key, defaultValue string) string
- func (c *Context) DefaultQuery(key, defaultValue string) string
- func (c *Context) Error(err error)
- func (c *Context) HTML(name string, data interface{})
- func (c *Context) Header(key string) string
- func (c *Context) IsAborted() bool
- func (c *Context) JSON(data interface{})
- func (c *Context) Next()
- func (c *Context) Param(name string) string
- func (c *Context) PostForm(key string) string
- func (c *Context) Query(key string) string
- func (c *Context) Redirect(location string)
- func (c *Context) ServeFile(filepath string)
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) SetHeader(key, value string)
- func (c *Context) Status(code int)
- func (c *Context) String(format string, values ...interface{})
- type Engine
- func (engine *Engine) LoadHTMLFiles(files ...string)
- func (engine *Engine) LoadHTMLGlob(pattern string)
- func (engine *Engine) NoMethod(handlers ...HandlerFunc)
- func (engine *Engine) NoRoute(handlers ...HandlerFunc)
- func (engine *Engine) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (engine *Engine) SetIgnoreCase(ignoreCase bool)
- func (engine *Engine) SetTrailingSlashRedirect(redirect bool)
- type Error
- type Handle
- type Handler
- type HandlerFunc
- type JSON
- type Params
- type Router
- type RouterPrefix
- func (p *RouterPrefix) Any(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Delete(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Get(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Handle(method, pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Head(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Options(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Patch(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Post(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Prefix(basePath string) *RouterPrefix
- func (p *RouterPrefix) Put(pattern string, handlers ...HandlerFunc)
- func (p *RouterPrefix) Static(pattern, dir string)
- func (p *RouterPrefix) StaticFile(pattern, filepath string)
- func (p *RouterPrefix) Use(middleware ...HandlerFunc)
- func (p *RouterPrefix) UseHandler(handlers ...Handler)
Constants ¶
const ( MIMEJSON = "application/json" MIMEXML = "application/xml" MIMEXML2 = "text/xml" MIMEPOSTForm = "application/x-www-form-urlencoded" MIMEMultipartPOSTForm = "multipart/form-data" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BindingStruct ¶
type BindingStruct interface {
Validate() error
}
type Context ¶
type Context struct { http.ResponseWriter // Short for http.Request Request *http.Request // Param is URL parameter, a map[string]string. Params Params // Short for Request.URL.Path Path string // Short for Request.Method Method string // Error when processing request Err *Error // contains filtered or unexported fields }
Context construct Request and ResponseWriter, provide useful methods
func NewContext ¶
func NewContext(p *RouterPrefix, rw http.ResponseWriter, req *http.Request) *Context
func (*Context) Abort ¶
func (c *Context) Abort()
Abort prevents pending handlers from being called. Note that this will not stop the current handler. if you want to stop current handler you should return, after call abort, call Abort to ensure the remaining handlers for this request are not called.
func (*Context) AbortWithStatus ¶
AbortWithStatus prevents pending handlers from being called and set statuscode. Note that this will not stop the current handler. if you want to stop current handler you should return, after call abort, call Abort to ensure the remaining handlers for this request are not called.
func (*Context) Bind ¶
func (c *Context) Bind(data BindingStruct) error
Bind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:
"application/json" --> JSON "application/xml" --> XML
otherwise --> returns an error It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. Like ParseBody() but this method also writes a 400 error if the json is not valid.
func (*Context) ClientIP ¶
ClientIP implements a best effort algorithm to return the real client IP, it parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy.
func (*Context) ContentType ¶
ContentType return content-type from header
func (*Context) Cookie ¶
Cookie get cookie from request header by name, if err != nil, return "", err
func (*Context) DefaultPostForm ¶
DefaultPostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns the specified defaultValue string.
func (*Context) DefaultQuery ¶
Query returns the keyed url query value if it exists, othewise it returns spectfic defaultValue. It is shortcut for `c.Request.URL.Query().Get(key)` GET /path?&name=cssivision&age=23
c.DefaultQuery("name", "balabala") == "cssivision" c.Query("age", "24") == "23" c.Query("sex", "male") == "male"
func (*Context) Header ¶
Get gets the first value associated with the given key. It is case insensitive
func (*Context) Next ¶
func (c *Context) Next()
Next should be used only inside middleware. It executes the pending handlers in the chain inside the calling handler
func (*Context) PostForm ¶
PostForm returns the specified key from a POST urlencoded form or multipart form when it exists, otherwise it returns an empty string.
func (*Context) Query ¶
Query returns the keyed url query value if it exists, othewise it returns an empty string `("")`. It is shortcut for `c.Request.URL.Query().Get(key)` GET /path?&name=cssivision&age=23
c.Query("name") == "cssivision" c.Query("age") == "23" c.Query("sex") == ""
func (*Context) Redirect ¶
Redirect replies to the request with a redirect to url, which may be a path relative to the request path.
func (*Context) ServeFile ¶
As a special case, ServeFile redirects any request where r.URL.Path ends in "/index.html" to the same path, without the final "index.html". To avoid such redirects either modify the path or use ServeContent.
func (*Context) SetHeader ¶
Set sets the header entries associated with key to the single element value. It replaces any existing values associated with key.
type Engine ¶
type Engine struct { // router with basePath, default basePath = "" RouterPrefix // when set true, implements a best effort algorithm to return the real client IP, it parses // X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such us: nginx or haproxy. ForwardedByClientIP bool // template used to render HTML Template *template.Template // contains filtered or unexported fields }
func Default ¶
func Default() *Engine
Default return engine instance, add logger, recover handler to it.
func (*Engine) LoadHTMLFiles ¶
func (*Engine) LoadHTMLGlob ¶
func (*Engine) NoMethod ¶
func (engine *Engine) NoMethod(handlers ...HandlerFunc)
NoMethod which is called when method is not registered. If it is not set, noMethod is used.
func (*Engine) NoRoute ¶
func (engine *Engine) NoRoute(handlers ...HandlerFunc)
NoRoute which is called when no matching route is found. If it is not set, noRoute is used.
func (*Engine) ServeHTTP ¶
func (engine *Engine) ServeHTTP(rw http.ResponseWriter, req *http.Request)
http.Handler interface
func (*Engine) SetIgnoreCase ¶
set IgnoreCase value
func (*Engine) SetTrailingSlashRedirect ¶
set TrailingSlashRedirect value
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 named/wildcards parameters.
type HandlerFunc ¶
type HandlerFunc func(*Context)
func Logger ¶
func Logger() HandlerFunc
func LoggerWithWriter ¶
func LoggerWithWriter(out io.Writer) HandlerFunc
func Recover ¶
func Recover() HandlerFunc
func RecoverWithWriter ¶
func RecoverWithWriter(out io.Writer) HandlerFunc
type Router ¶
type Router struct { // Ignore case when matching URL path. IgnoreCase bool // Enables automatic redirection if the current route can't be matched but a // handler for the path with (without) the trailing slash exists. // TrailingSlashRedirect: /a/b/ -> /a/b // TrailingSlashRedirect: /a/b -> /a/b/ TrailingSlashRedirect bool // Configurable http.Handler which is called when no matching route is // found. If it is not set, http.NotFound is used. NoRoute []HandlerFunc // Configurable http.Handler which is called when method is not allowed. If it is not set, http.NotFound is used. NoMethod []HandlerFunc // contains filtered or unexported fields }
Router is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes
func NewRouter ¶
func NewRouter() *Router
New returns a new initialized Router, with default configuration
func (*Router) Handle ¶
func (r *Router) Handle(method, pattern string, handlers []HandlerFunc)
Handle registers a new request handle with the given path and method. For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
type RouterPrefix ¶
type RouterPrefix struct { Middlewares []HandlerFunc // contains filtered or unexported fields }
RouterPrefix is used internally to configure router, a RouterPrefix is associated with a basePath and an array of handlers (middleware)
func (*RouterPrefix) Any ¶
func (p *RouterPrefix) Any(pattern string, handlers ...HandlerFunc)
Any registers a route that matches all the HTTP methods. GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
func (*RouterPrefix) Delete ¶
func (p *RouterPrefix) Delete(pattern string, handlers ...HandlerFunc)
Delete is a shortcut for router.Handle("DELETE", path, handle)
func (*RouterPrefix) Get ¶
func (p *RouterPrefix) Get(pattern string, handlers ...HandlerFunc)
Get is a shortcut for router.Handle("GET", path, handle)
func (*RouterPrefix) Handle ¶
func (p *RouterPrefix) Handle(method, pattern string, handlers ...HandlerFunc)
Handle registers a new request handle and middleware with the given path and method.
func (*RouterPrefix) Head ¶
func (p *RouterPrefix) Head(pattern string, handlers ...HandlerFunc)
Head is a shortcut for router.Handle("HEAD", path, handle)
func (*RouterPrefix) Options ¶
func (p *RouterPrefix) Options(pattern string, handlers ...HandlerFunc)
Options is a shortcut for router.Handle("OPTIONS", path, handle)
func (*RouterPrefix) Patch ¶
func (p *RouterPrefix) Patch(pattern string, handlers ...HandlerFunc)
Patch is a shortcut for router.Handle("PATCH", path, handle)
func (*RouterPrefix) Post ¶
func (p *RouterPrefix) Post(pattern string, handlers ...HandlerFunc)
Post is a shortcut for router.Handle("Post", path, handle)
func (*RouterPrefix) Prefix ¶
func (p *RouterPrefix) Prefix(basePath string) *RouterPrefix
Prefix creates a new router prefix. You should add all the routes that have common middlwares or the same path prefix. For example, all the routes that use a common middlware could be grouped.
func (*RouterPrefix) Put ¶
func (p *RouterPrefix) Put(pattern string, handlers ...HandlerFunc)
Put is a shortcut for router.Handle("Put", path, handle)
func (*RouterPrefix) Static ¶
func (p *RouterPrefix) Static(pattern, dir string)
Static register router pattern and response file in the request url
func (*RouterPrefix) StaticFile ¶
func (p *RouterPrefix) StaticFile(pattern, filepath string)
StaticFile register router pattern and response file in path
func (*RouterPrefix) Use ¶
func (p *RouterPrefix) Use(middleware ...HandlerFunc)
Use adds middleware to the router.
func (*RouterPrefix) UseHandler ¶
func (p *RouterPrefix) UseHandler(handlers ...Handler)
Use adds handlers as middleware to the router.