Search and commands

↑ ↓ to select · Enter to open · Esc to close

Type to search packages.

lion

v1.0.0-20161101201550-7c6ce5d22d64
gopkg.in/celrenheit/lion.v1

Documentation

pkg.go.dev ↗

Table of contents

↓ to select · Enter to jump· Shift+Enter for source

130 matches

Top ↑

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")

Constants

GET (9 declarations)

const (
	GET     = "GET"
	HEAD    = "HEAD"
	POST    = "POST"
	PUT     = "PUT"
	DELETE  = "DELETE"
	TRACE   = "TRACE"
	OPTIONS = "OPTIONS"
	CONNECT = "CONNECT"
	PATCH   = "PATCH"
)

HTTP methods constants

Functions

Param

func Param(c context.Context, key string) string

Param returns the value of a url param base on the passed context

UnWrap

func UnWrap(h Handler) http.Handler

UnWrap converts a Handler to an http.Handler

Types

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.

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.

Context

type Context struct {
	context.Context
	// contains filtered or unexported fields
}

Context implements golang.org/x/net/context.Context and stores values of url parameters

C

func C(c context.Context) *Context

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.

NewContext

func NewContext() *Context

NewContext creates a new context instance

NewContextWithParent

func NewContextWithParent(c context.Context) *Context

NewContextWithParent creates a new context with a parent context specified

Context.AddParam

func (p *Context) AddParam(key, val string)

Context.Param

func (p *Context) Param(key string) string

Param returns the value of a param. If it does not exist it returns an empty string

Context.ParamOk

func (p *Context) ParamOk(key string) (string, bool)

ParamOk returns the value of a param and a boolean that indicates if the param exists.

Context.Remove

func (p *Context) Remove(key string)

Context.Reset

func (p *Context) Reset()

Context.Value

func (p *Context) Value(key interface{}) interface{}

Value returns the value for the passed key. If it is not found in the url params it returns parent's context Value

DeleteResource

type DeleteResource interface {
	Delete(c context.Context, w http.ResponseWriter, r *http.Request)
}

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.

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.

GetResource

type GetResource interface {
	Get(c context.Context, w http.ResponseWriter, r *http.Request)
}

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.

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.

Handler

type Handler interface {
	ServeHTTPC(context.Context, http.ResponseWriter, *http.Request)
}

Handler responds to an HTTP request

Wrap

func Wrap(h http.Handler) Handler

Wrap converts an http.Handler to returns a Handler

WrapFunc

func WrapFunc(fn http.HandlerFunc) Handler

WrapFunc converts an http.HandlerFunc to return a Handler

HandlerFunc

type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)

HandlerFunc is a wrapper for a function to implement the Handler interface

HandlerFunc.ServeHTTP

func (h HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP makes HandlerFunc implement net/http.Handler interface

HandlerFunc.ServeHTTPC

func (h HandlerFunc) ServeHTTPC(c context.Context, w http.ResponseWriter, r *http.Request)

ServeHTTPC makes HandlerFunc implement Handler interface

HeadResource

type HeadResource interface {
	Head(c context.Context, w http.ResponseWriter, r *http.Request)
}

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.

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.

Logger

type Logger struct {
	*log.Logger
}

Logger is a middlewares that logs incoming http requests

NewLogger

func NewLogger() *Logger

NewLogger creates a new Logger

Logger.ServeNext

func (l *Logger) ServeNext(next Handler) Handler

ServeNext implements the Middleware interface for Logger. It wraps the corresponding http.ResponseWriter and saves statistics about the status code returned, the number of bytes written and the time that requests took.

M

type M map[string]string

M is an alias for map[string]string.

Middleware

type Middleware interface {
	ServeNext(Handler) Handler
}

Middleware interface that takes as input a Handler and returns a Handler

MaxAge

func MaxAge(dur time.Duration) Middleware

MaxAge is a middleware that defines the max duration headers

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.

NoCache

func NoCache() Middleware

NoCache middleware sets headers to disable browser caching. Inspired by https://github.com/mytrile/nocache

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

MiddlewareFunc

type MiddlewareFunc func(Handler) Handler

MiddlewareFunc wraps a function that takes as input a Handler and returns a Handler. So that it implements the Middlewares interface

MiddlewareFunc.ServeNext

func (m MiddlewareFunc) ServeNext(next Handler) Handler

ServeNext makes MiddlewareFunc implement Middleware

Middlewares

type Middlewares []Middleware

Middlewares is an array of Middleware

Middlewares.BuildHandler

func (middlewares Middlewares) BuildHandler(handler Handler) Handler

BuildHandler builds a chain of middlewares from a passed Handler and returns a Handler

Module

type Module interface {
	Resource
	Base() string
	Routes(*Router)
}

Module represent an independent router entity. It should be used to group routes and subroutes together.

ModuleRequirements

type ModuleRequirements interface {
	Requires() []string
}

ModuleRequirements specify that the module requires specific named middlewares.

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.

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.

PatchResource

type PatchResource interface {
	Patch(c context.Context, w http.ResponseWriter, r *http.Request)
}

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.

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.

PostResource

type PostResource interface {
	Post(c context.Context, w http.ResponseWriter, r *http.Request)
}

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.

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.

PutResource

type PutResource interface {
	Put(c context.Context, w http.ResponseWriter, r *http.Request)
}

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.

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.

Recovery

type Recovery struct {
	Logger     *log.Logger
	PrintStack bool
	StackAll   bool
	StackSize  int
}

Recovery is a middleware that recovers from panics Taken from https://github.com/codegangsta/negroni/blob/master/recovery.go

NewRecovery

func NewRecovery() *Recovery

NewRecovery creates a new Recovery instance

Recovery.ServeNext

func (rec *Recovery) ServeNext(next Handler) Handler

ServeNext is the method responsible for recovering from a panic

RegisterMatcher

type RegisterMatcher interface {
	Register(method, pattern string, handler Handler)
	Match(*Context, *http.Request) (*Context, Handler)
}

RegisterMatcher registers and matches routes to Handlers

Resource

type Resource interface{}

Resource defines the minimum required methods

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.

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

WrapResponseWriter

func WrapResponseWriter(w http.ResponseWriter) ResponseWriter

WrapResponseWriter wraps an http.ResponseWriter and returns a ResponseWriter

Router

type Router struct {
	// contains filtered or unexported fields
}

Router is the main component of Lion. It is responsible for registering handlers and middlewares

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.

New

func New(mws ...Middleware) *Router

New creates a new router instance

Router.Any

func (r *Router) Any(pattern string, handler Handler)

Any registers the provided Handler for all of the allowed http methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH

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

Router.Connect

func (r *Router) Connect(pattern string, handler Handler)

Connect registers an http CONNECT method receiver with the provided Handler

Router.ConnectFunc

func (r *Router) ConnectFunc(pattern string, fn HandlerFunc)

ConnectFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.ConnectH

func (r *Router) ConnectH(pattern string, handler http.Handler)

ConnectH wraps a http.Handler

Router.Define

func (r *Router) Define(name string, mws ...Middleware)

Define registers some middleware using a name for reuse later using UseNamed method.

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

Router.Delete

func (r *Router) Delete(pattern string, handler Handler)

Delete registers an http DELETE method receiver with the provided Handler

Router.DeleteFunc

func (r *Router) DeleteFunc(pattern string, fn HandlerFunc)

DeleteFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.DeleteH

func (r *Router) DeleteH(pattern string, handler http.Handler)

DeleteH wraps a http.Handler

Router.Get

func (r *Router) Get(pattern string, handler Handler)

Get registers an http GET method receiver with the provided Handler

Router.GetFunc

func (r *Router) GetFunc(pattern string, fn HandlerFunc)

GetFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.GetH

func (r *Router) GetH(pattern string, handler http.Handler)

GetH wraps a http.Handler

Router.Group

func (r *Router) Group(pattern string, mws ...Middleware) *Router

Group creates a subrouter with parent pattern provided.

Router.Handle

func (r *Router) Handle(method, pattern string, handler Handler)

Handle is the underling method responsible for registering a handler for a specific method and pattern.

Router.HandleFunc

func (r *Router) HandleFunc(method, pattern string, fn HandlerFunc)

HandleFunc wraps a HandlerFunc and pass it to Handle method

Router.Head

func (r *Router) Head(pattern string, handler Handler)

Head registers an http HEAD method receiver with the provided Handler

Router.HeadFunc

func (r *Router) HeadFunc(pattern string, fn HandlerFunc)

HeadFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.HeadH

func (r *Router) HeadH(pattern string, handler http.Handler)

HeadH wraps a http.Handler

Router.Host

func (r *Router) Host(hostpattern string) *Router

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.*
*

Router.Module

func (r *Router) Module(modules ...Module)

Module register modules for the current router instance.

Router.Mount

func (r *Router) Mount(pattern string, router *Router, mws ...Middleware)

Mount mounts a subrouter at the provided pattern

Router.NotFoundHandler

func (r *Router) NotFoundHandler(handler Handler)

NotFoundHandler gives the ability to use a specific 404 NOT FOUND handler

Router.Options

func (r *Router) Options(pattern string, handler Handler)

Options registers an http OPTIONS method receiver with the provided Handler

Router.OptionsFunc

func (r *Router) OptionsFunc(pattern string, fn HandlerFunc)

OptionsFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.OptionsH

func (r *Router) OptionsH(pattern string, handler http.Handler)

OptionsH wraps a http.Handler

Router.Patch

func (r *Router) Patch(pattern string, handler Handler)

Patch registers an http PATCH method receiver with the provided Handler

Router.PatchFunc

func (r *Router) PatchFunc(pattern string, fn HandlerFunc)

PatchFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.PatchH

func (r *Router) PatchH(pattern string, handler http.Handler)

PatchH wraps a http.Handler

Router.Post

func (r *Router) Post(pattern string, handler Handler)

Post registers an http POST method receiver with the provided Handler

Router.PostFunc

func (r *Router) PostFunc(pattern string, fn HandlerFunc)

PostFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.PostH

func (r *Router) PostH(pattern string, handler http.Handler)

PostH wraps a http.Handler

Router.Put

func (r *Router) Put(pattern string, handler Handler)

Put registers an http PUT method receiver with the provided Handler

Router.PutFunc

func (r *Router) PutFunc(pattern string, fn HandlerFunc)

PutFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.PutH

func (r *Router) PutH(pattern string, handler http.Handler)

PutH wraps a http.Handler

Router.Resource

func (r *Router) Resource(pattern string, resource Resource)

Resource registers a Resource with the corresponding pattern

Router.Run

func (r *Router) Run(addr ...string)

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")

Router.RunTLS

func (r *Router) RunTLS(addr, certFile, keyFile string)

RunTLS calls http.ListenAndServeTLS for the current router

r := New()
r.RunTLS(":3443", "cert.pem", "key.pem")

Router.ServeFile

func (r *Router) ServeFile(base, path string)

ServeFile serve a specific file located at the passed path

l := New()
l.ServeFile("/robots.txt", "path/to/robots.txt")

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

Router.ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP calls ServeHTTPC with a context.Background()

Router.ServeHTTPC

func (r *Router) ServeHTTPC(c context.Context, w http.ResponseWriter, req *http.Request)

ServeHTTPC finds the handler associated with the request's path. If it is not found it calls the NotFound handler

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.

Router.Trace

func (r *Router) Trace(pattern string, handler Handler)

Trace registers an http TRACE method receiver with the provided Handler

Router.TraceFunc

func (r *Router) TraceFunc(pattern string, fn HandlerFunc)

TraceFunc wraps a HandlerFunc as a Handler and registers it to the router

Router.TraceH

func (r *Router) TraceH(pattern string, handler http.Handler)

TraceH wraps a http.Handler

Router.Use

func (r *Router) Use(middlewares ...Middleware)

Use registers middlewares to be used

Router.UseFunc

func (r *Router) UseFunc(middlewareFuncs ...MiddlewareFunc)

UseFunc wraps a MiddlewareFunc as a Middleware and registers it middlewares to be used

Router.UseHandler

func (r *Router) UseHandler(handler Handler)

UseHandler gives the ability to add and serve a Handler and serve the next handler

Router.UseHandlerFunc

func (r *Router) UseHandlerFunc(fn HandlerFunc)

UseHandlerFunc is a convenience wrapper for UseHandler

Router.UseNamed

func (r *Router) UseNamed(name string)

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.

Router.UseNegroni

func (r *Router) UseNegroni(n negroniHandler)

UseNegroni gives the ability to use Negroni.Handler middlewares as lion.Middlewares

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

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

NewStatic

func NewStatic(directory http.FileSystem) *Static

NewStatic returns a new instance of Static

Static.ServeNext

func (s *Static) ServeNext(next Handler) Handler

ServeNext tries to find a file in the directory

TraceResource

type TraceResource interface {
	Trace(c context.Context, w http.ResponseWriter, r *http.Request)
}

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.

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.

Dependency graph