Documentation
¶
Overview ¶
Package lion is a fast HTTP router for building modern scalable modular REST APIs in Go.
Install and update:
go get -u github.com/celrenheit/lion
Getting started:
Start by importing "github.com/celrenheit/lion" into your project. Then you need to create a new instance of the router using lion.New() for a blank router or lion.Classic() for a router with default middlewares included.
Here is a simple hello world example:
package main import ( "fmt" "net/http" "github.com/celrenheit/lion" "golang.org/x/net/context" ) func Home(c context.Context, w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Home") } func Hello(c context.Context, w http.ResponseWriter, r *http.Request) { name := lion.Param(c, "name") fmt.Fprintf(w, "Hello "+name) } func main() { l := lion.Classic() l.GetFunc("/", Home) l.GetFunc("/hello/:name", Hello) l.Run() }
You can open your web browser to http://localhost:3000/hello/world and you should see "Hello world". If it finds a PORT environnement variable it will use that. Otherwise, it will use run the server at localhost:3000. If you wish to provide a specific port you can run it using: l.Run(":8080")
Index ¶
- Constants
- func Param(c context.Context, key string) string
- func UnWrap(h Handler) http.Handler
- type ConnectResource
- type ConnectResourceMiddlewares
- type Context
- type DeleteResource
- type DeleteResourceMiddlewares
- type GetResource
- type GetResourceMiddlewares
- type Handler
- type HandlerFunc
- type HeadResource
- type HeadResourceMiddlewares
- type Logger
- type M
- type Middleware
- type MiddlewareFunc
- type Middlewares
- type Module
- type ModuleRequirements
- type OptionsResource
- type OptionsResourceMiddlewares
- type PatchResource
- type PatchResourceMiddlewares
- type PostResource
- type PostResourceMiddlewares
- type PutResource
- type PutResourceMiddlewares
- type Recovery
- type RegisterMatcher
- type Resource
- type ResourceUses
- type ResponseWriter
- type Router
- func (r *Router) Any(pattern string, handler Handler)
- func (r *Router) AnyFunc(pattern string, handler HandlerFunc)
- func (r *Router) Connect(pattern string, handler Handler)
- func (r *Router) ConnectFunc(pattern string, fn HandlerFunc)
- func (r *Router) ConnectH(pattern string, handler http.Handler)
- func (r *Router) Define(name string, mws ...Middleware)
- func (r *Router) DefineFunc(name string, mws ...MiddlewareFunc)
- func (r *Router) Delete(pattern string, handler Handler)
- func (r *Router) DeleteFunc(pattern string, fn HandlerFunc)
- func (r *Router) DeleteH(pattern string, handler http.Handler)
- func (r *Router) Get(pattern string, handler Handler)
- func (r *Router) GetFunc(pattern string, fn HandlerFunc)
- func (r *Router) GetH(pattern string, handler http.Handler)
- func (r *Router) Group(pattern string, mws ...Middleware) *Router
- func (r *Router) Handle(method, pattern string, handler Handler)
- func (r *Router) HandleFunc(method, pattern string, fn HandlerFunc)
- func (r *Router) Head(pattern string, handler Handler)
- func (r *Router) HeadFunc(pattern string, fn HandlerFunc)
- func (r *Router) HeadH(pattern string, handler http.Handler)
- func (r *Router) Host(hostpattern string) *Router
- func (r *Router) Module(modules ...Module)
- func (r *Router) Mount(pattern string, router *Router, mws ...Middleware)
- func (r *Router) NotFoundHandler(handler Handler)
- func (r *Router) Options(pattern string, handler Handler)
- func (r *Router) OptionsFunc(pattern string, fn HandlerFunc)
- func (r *Router) OptionsH(pattern string, handler http.Handler)
- func (r *Router) Patch(pattern string, handler Handler)
- func (r *Router) PatchFunc(pattern string, fn HandlerFunc)
- func (r *Router) PatchH(pattern string, handler http.Handler)
- func (r *Router) Post(pattern string, handler Handler)
- func (r *Router) PostFunc(pattern string, fn HandlerFunc)
- func (r *Router) PostH(pattern string, handler http.Handler)
- func (r *Router) Put(pattern string, handler Handler)
- func (r *Router) PutFunc(pattern string, fn HandlerFunc)
- func (r *Router) PutH(pattern string, handler http.Handler)
- func (r *Router) Resource(pattern string, resource Resource)
- func (r *Router) Run(addr ...string)
- func (r *Router) RunTLS(addr, certFile, keyFile string)
- func (r *Router) ServeFile(base, path string)
- func (r *Router) ServeFiles(base string, root http.FileSystem)
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) ServeHTTPC(c context.Context, w http.ResponseWriter, req *http.Request)
- func (r *Router) Subrouter(mws ...Middleware) *Router
- func (r *Router) Trace(pattern string, handler Handler)
- func (r *Router) TraceFunc(pattern string, fn HandlerFunc)
- func (r *Router) TraceH(pattern string, handler http.Handler)
- func (r *Router) Use(middlewares ...Middleware)
- func (r *Router) UseFunc(middlewareFuncs ...MiddlewareFunc)
- func (r *Router) UseHandler(handler Handler)
- func (r *Router) UseHandlerFunc(fn HandlerFunc)
- func (r *Router) UseNamed(name string)
- func (r *Router) UseNegroni(n negroniHandler)
- func (r *Router) UseNegroniFunc(n func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc))
- type Static
- type TraceResource
- type TraceResourceMiddlewares
Constants ¶
const ( GET = "GET" HEAD = "HEAD" POST = "POST" PUT = "PUT" DELETE = "DELETE" TRACE = "TRACE" OPTIONS = "OPTIONS" CONNECT = "CONNECT" PATCH = "PATCH" )
HTTP methods constants
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ConnectResource ¶
type ConnectResource interface {
Connect(c context.Context, w http.ResponseWriter, r *http.Request)
}
ConnectResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type ConnectResourceMiddlewares ¶
type ConnectResourceMiddlewares interface {
ConnectMiddlewares() Middlewares
}
ConnectResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type Context ¶
Context implements golang.org/x/net/context.Context and stores values of url parameters
func C ¶
C returns a Context based on a context.Context passed. If it does not convert to Context, it creates a new one with the context passed as argument.
func NewContextWithParent ¶
NewContextWithParent creates a new context with a parent context specified
func (*Context) Param ¶
Param returns the value of a param. If it does not exist it returns an empty string
type DeleteResource ¶
DeleteResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type DeleteResourceMiddlewares ¶
type DeleteResourceMiddlewares interface {
DeleteMiddlewares() Middlewares
}
DeleteResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type GetResource ¶
GetResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type GetResourceMiddlewares ¶
type GetResourceMiddlewares interface {
GetMiddlewares() Middlewares
}
GetResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type Handler ¶
Handler responds to an HTTP request
func WrapFunc ¶
func WrapFunc(fn http.HandlerFunc) Handler
WrapFunc converts an http.HandlerFunc to return a Handler
type HandlerFunc ¶
HandlerFunc is a wrapper for a function to implement the Handler interface
func (HandlerFunc) ServeHTTP ¶
func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP makes HandlerFunc implement net/http.Handler interface
func (HandlerFunc) ServeHTTPC ¶
func (h HandlerFunc) ServeHTTPC(c context.Context, w http.ResponseWriter, r *http.Request)
ServeHTTPC makes HandlerFunc implement Handler interface
type HeadResource ¶
HeadResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type HeadResourceMiddlewares ¶
type HeadResourceMiddlewares interface {
HeadMiddlewares() Middlewares
}
HeadResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type Middleware ¶
Middleware interface that takes as input a Handler and returns a Handler
func MaxAge ¶
func MaxAge(dur time.Duration) Middleware
MaxAge is a middleware that defines the max duration headers
func MaxAgeWithFilter ¶
func MaxAgeWithFilter(dur time.Duration, filter func(c context.Context, w http.ResponseWriter, r *http.Request) bool) Middleware
MaxAgeWithFilter is a middleware that defines the max duration headers with a filter function. If the filter returns true then the headers will be set. Otherwise, if it returns false the headers will not be set.
func NoCache ¶
func NoCache() Middleware
NoCache middleware sets headers to disable browser caching. Inspired by https://github.com/mytrile/nocache
func RealIP ¶
func RealIP() Middleware
RealIP is a middleware that sets a http.Request's RemoteAddr to the results of parsing either the X-Forwarded-For header or the X-Real-IP header (in that order).
This middleware should be inserted fairly early in the middleware stack to ensure that subsequent layers (e.g., request loggers) which examine the RemoteAddr will see the intended value.
You should only use this middleware if you can trust the headers passed to you (in particular, the two headers this middleware uses), for example because you have placed a reverse proxy like HAProxy or nginx in front of Goji. If your reverse proxies are configured to pass along arbitrary header values from the client, or if you use this middleware without a reverse proxy, malicious clients will be able to make you very sad (or, depending on how you're using RemoteAddr, vulnerable to an attack of some sort). Taken from https://github.com/zenazn/goji/blob/master/web/middleware/realip.go
type MiddlewareFunc ¶
MiddlewareFunc wraps a function that takes as input a Handler and returns a Handler. So that it implements the Middlewares interface
func (MiddlewareFunc) ServeNext ¶
func (m MiddlewareFunc) ServeNext(next Handler) Handler
ServeNext makes MiddlewareFunc implement Middleware
type Middlewares ¶
type Middlewares []Middleware
Middlewares is an array of Middleware
func (Middlewares) BuildHandler ¶
func (middlewares Middlewares) BuildHandler(handler Handler) Handler
BuildHandler builds a chain of middlewares from a passed Handler and returns a Handler
type Module ¶
Module represent an independent router entity. It should be used to group routes and subroutes together.
type ModuleRequirements ¶
type ModuleRequirements interface {
Requires() []string
}
ModuleRequirements specify that the module requires specific named middlewares.
type OptionsResource ¶
type OptionsResource interface {
Options(c context.Context, w http.ResponseWriter, r *http.Request)
}
OptionsResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type OptionsResourceMiddlewares ¶
type OptionsResourceMiddlewares interface {
OptionsMiddlewares() Middlewares
}
OptionsResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type PatchResource ¶
PatchResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type PatchResourceMiddlewares ¶
type PatchResourceMiddlewares interface {
PatchMiddlewares() Middlewares
}
PatchResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type PostResource ¶
PostResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type PostResourceMiddlewares ¶
type PostResourceMiddlewares interface {
PostMiddlewares() Middlewares
}
PostResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type PutResource ¶
PutResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type PutResourceMiddlewares ¶
type PutResourceMiddlewares interface {
PutMiddlewares() Middlewares
}
PutResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type Recovery ¶
Recovery is a middleware that recovers from panics Taken from https://github.com/codegangsta/negroni/blob/master/recovery.go
type RegisterMatcher ¶
type RegisterMatcher interface { Register(method, pattern string, handler Handler) Match(*Context, *http.Request) (*Context, Handler) }
RegisterMatcher registers and matches routes to Handlers
type ResourceUses ¶
type ResourceUses interface {
Uses() Middlewares
}
ResourceUses is an interface with the Uses() method which can be used to define global middlewares for the resource. DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter http.Flusher http.Hijacker Status() int BytesWritten() int Tee(io.Writer) Unwrap() http.ResponseWriter }
ResponseWriter is the proxy responseWriter
func WrapResponseWriter ¶
func WrapResponseWriter(w http.ResponseWriter) ResponseWriter
WrapResponseWriter wraps an http.ResponseWriter and returns a ResponseWriter
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is the main component of Lion. It is responsible for registering handlers and middlewares
func Classic ¶
func Classic() *Router
Classic creates a new router instance with default middlewares: Recovery, RealIP, Logger and Static. The static middleware instance is initiated with a directory named "public" located relatively to the current working directory.
func (*Router) Any ¶
Any registers the provided Handler for all of the allowed http methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
func (*Router) AnyFunc ¶
func (r *Router) AnyFunc(pattern string, handler HandlerFunc)
AnyFunc registers the provided HandlerFunc for all of the allowed http methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
func (*Router) Connect ¶
Connect registers an http CONNECT method receiver with the provided Handler
func (*Router) ConnectFunc ¶
func (r *Router) ConnectFunc(pattern string, fn HandlerFunc)
ConnectFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) Define ¶
func (r *Router) Define(name string, mws ...Middleware)
Define registers some middleware using a name for reuse later using UseNamed method.
func (*Router) DefineFunc ¶
func (r *Router) DefineFunc(name string, mws ...MiddlewareFunc)
DefineFunc is a convenience wrapper for Define() to use MiddlewareFunc instead of a Middleware instance
func (*Router) DeleteFunc ¶
func (r *Router) DeleteFunc(pattern string, fn HandlerFunc)
DeleteFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) GetFunc ¶
func (r *Router) GetFunc(pattern string, fn HandlerFunc)
GetFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) Group ¶
func (r *Router) Group(pattern string, mws ...Middleware) *Router
Group creates a subrouter with parent pattern provided.
func (*Router) Handle ¶
Handle is the underling method responsible for registering a handler for a specific method and pattern.
func (*Router) HandleFunc ¶
func (r *Router) HandleFunc(method, pattern string, fn HandlerFunc)
HandleFunc wraps a HandlerFunc and pass it to Handle method
func (*Router) HeadFunc ¶
func (r *Router) HeadFunc(pattern string, fn HandlerFunc)
HeadFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) Host ¶
Host sets the host for the current router instances. You can use patterns in the same way they are currently used for routes but in reverse order (params on the left)
NOTE: You have to use the '$' character instead of ':' for matching host parameters.
The following patterns works:
admin.example.com will match admin.example.com $username.blog.com will match messi.blog.com will not match my.awesome.blog.com *.example.com will match my.admin.example.com
The following patterns are not allowed:
mail.* *
func (*Router) Mount ¶
func (r *Router) Mount(pattern string, router *Router, mws ...Middleware)
Mount mounts a subrouter at the provided pattern
func (*Router) NotFoundHandler ¶
NotFoundHandler gives the ability to use a specific 404 NOT FOUND handler
func (*Router) Options ¶
Options registers an http OPTIONS method receiver with the provided Handler
func (*Router) OptionsFunc ¶
func (r *Router) OptionsFunc(pattern string, fn HandlerFunc)
OptionsFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) PatchFunc ¶
func (r *Router) PatchFunc(pattern string, fn HandlerFunc)
PatchFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) PostFunc ¶
func (r *Router) PostFunc(pattern string, fn HandlerFunc)
PostFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) PutFunc ¶
func (r *Router) PutFunc(pattern string, fn HandlerFunc)
PutFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) Run ¶
Run calls http.ListenAndServe for the current router. If no addresses are specified as arguments, it will use the PORT environnement variable if it is defined. Otherwise, it will listen on port 3000 of the localmachine
r := New() r.Run() // will call r.Run(":8080")
func (*Router) RunTLS ¶
RunTLS calls http.ListenAndServeTLS for the current router
r := New() r.RunTLS(":3443", "cert.pem", "key.pem")
func (*Router) ServeFile ¶
ServeFile serve a specific file located at the passed path
l := New() l.ServeFile("/robots.txt", "path/to/robots.txt")
func (*Router) ServeFiles ¶
func (r *Router) ServeFiles(base string, root http.FileSystem)
ServeFiles serves files located in root http.FileSystem
This can be used as shown below:
r := New() r.ServeFiles("/static", http.Dir("static")) // This will serve files in the directory static with /static prefix
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP calls ServeHTTPC with a context.Background()
func (*Router) ServeHTTPC ¶
ServeHTTPC finds the handler associated with the request's path. If it is not found it calls the NotFound handler
func (*Router) Subrouter ¶
func (r *Router) Subrouter(mws ...Middleware) *Router
Subrouter creates a new router based on the parent router.
A subrouter has the same pattern and host as the parent router. It has it's own middlewares.
func (*Router) TraceFunc ¶
func (r *Router) TraceFunc(pattern string, fn HandlerFunc)
TraceFunc wraps a HandlerFunc as a Handler and registers it to the router
func (*Router) Use ¶
func (r *Router) Use(middlewares ...Middleware)
Use registers middlewares to be used
func (*Router) UseFunc ¶
func (r *Router) UseFunc(middlewareFuncs ...MiddlewareFunc)
UseFunc wraps a MiddlewareFunc as a Middleware and registers it middlewares to be used
func (*Router) UseHandler ¶
UseHandler gives the ability to add and serve a Handler and serve the next handler
func (*Router) UseHandlerFunc ¶
func (r *Router) UseHandlerFunc(fn HandlerFunc)
UseHandlerFunc is a convenience wrapper for UseHandler
func (*Router) UseNamed ¶
UseNamed adds a middleware already defined using Define method. If it cannot find it in the current router, it will look for it in the parent router.
func (*Router) UseNegroni ¶
func (r *Router) UseNegroni(n negroniHandler)
UseNegroni gives the ability to use Negroni.Handler middlewares as lion.Middlewares
func (*Router) UseNegroniFunc ¶
func (r *Router) UseNegroniFunc(n func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc))
UseNegroniFunc is a convenience wrapper for UseNegroni to Negroni.HandlerFunc
type Static ¶
type Static struct { // Dir is the directory to serve static files from Dir http.FileSystem // Prefix is the optional prefix used to serve the static directory content Prefix string // IndexFile defines which file to serve as index if it exists. IndexFile string }
Static is a middleware handler that serves static files in the given directory/filesystem. Taken from https://github.com/codegangsta/negroni/blob/master/static.go
func NewStatic ¶
func NewStatic(directory http.FileSystem) *Static
NewStatic returns a new instance of Static
type TraceResource ¶
TraceResource is an interface for defining a HandlerFunc used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.
type TraceResourceMiddlewares ¶
type TraceResourceMiddlewares interface {
TraceMiddlewares() Middlewares
}
TraceResourceMiddlewares is an interface for defining middlewares used in Resource method DEPRECATED: These methods will be removed in v2. This should not cause problems since they are already publicly exposed for documentation purpose only.