kami: github.com/guregu/kami Index | Files | Directories

package kami

import "github.com/guregu/kami"

Index

Package Files

defaults.go globalmux.go globalmux_17.go handler_19.go handler_new.go kami_17.go middleware.go middleware_19.go middleware_new.go mux_17.go params.go serve.go

Variables

var (
    // Context is the root "god object" from which every request's context will derive.
    Context = context.Background()
    // Cancel will, if true, automatically cancel the context of incoming requests after they finish.
    Cancel bool

    // PanicHandler will, if set, be called on panics.
    // You can use kami.Exception(ctx) within the panic handler to get panic details.
    PanicHandler HandlerType
    // LogHandler will, if set, wrap every request and be called at the very end.
    LogHandler func(context.Context, mutil.WriterProxy, *http.Request)
)

func After Uses

func After(path string, aw AfterwareType)

After registers afterware to run after middleware and the request handler has run. Afterware is like middleware, but everything is in reverse. Afterware will be executed hierarchically, starting with wildcards and then the most specific path, ending with /. Afterware under the same path will be executed in the opposite order of registration.

func Delete Uses

func Delete(path string, handler HandlerType)

Delete registers a DELETE handler under the given path.

func EnableMethodNotAllowed Uses

func EnableMethodNotAllowed(enabled bool)

EnableMethodNotAllowed enables or disables automatic Method Not Allowed handling. Note that this is enabled by default.

func Exception Uses

func Exception(ctx context.Context) interface{}

Exception gets the "v" in panic(v). The panic details. Only PanicHandler will receive a context you can use this with.

func Get Uses

func Get(path string, handler HandlerType)

Get registers a GET handler under the given path.

func Handle Uses

func Handle(method, path string, handler HandlerType)

Handle registers an arbitrary method handler under the given path.

func Handler Uses

func Handler() http.Handler

Handler returns an http.Handler serving registered routes.

func Head(path string, handler HandlerType)

Head registers a HEAD handler under the given path.

func MethodNotAllowed Uses

func MethodNotAllowed(handler HandlerType)

MethodNotAllowed registers a special handler for automatically responding to invalid method requests (405).

func NotFound Uses

func NotFound(handler HandlerType)

NotFound registers a special handler for unregistered (404) paths. If handle is nil, use the default http.NotFound behavior.

func Options Uses

func Options(path string, handler HandlerType)

Head registers a OPTIONS handler under the given path.

func Param Uses

func Param(ctx context.Context, name string) string

Param returns a request path parameter, or a blank string if it doesn't exist. For example, with the path /v2/papers/:page use kami.Param(ctx, "page") to access the :page variable.

func Patch Uses

func Patch(path string, handler HandlerType)

Patch registers a PATCH handler under the given path.

func Post Uses

func Post(path string, handler HandlerType)

Post registers a POST handler under the given path.

func Put Uses

func Put(path string, handler HandlerType)

Put registers a PUT handler under the given path.

func Reset Uses

func Reset()

Reset changes the root Context to context.Background(). It removes every handler and all middleware.

func Serve Uses

func Serve()

Serve starts kami with reasonable defaults. The bind address can be changed by setting the GOJI_BIND environment variable, or by setting the "bind" command line flag. Serve detects einhorn and systemd for you. It works exactly like zenazn/goji.

func ServeListener Uses

func ServeListener(listener net.Listener)

ServeListener is like Serve, but runs kami on top of an arbitrary net.Listener.

func ServeTLS Uses

func ServeTLS(config *tls.Config)

ServeTLS is like Serve, but enables TLS using the given config.

func SetParam Uses

func SetParam(ctx context.Context, name string, value string) context.Context

SetParam will set the value of a path parameter in a given context. This is intended for testing and should not be used otherwise.

func Use Uses

func Use(path string, mw MiddlewareType)

Use registers middleware to run for the given path. Middleware will be executed hierarchically, starting with the least specific path. Middleware under the same path will be executed in order of registration. You may use wildcards in the path. Wildcard middleware will be run last, after all hierarchical middleware has run.

Adding middleware is not threadsafe.

WARNING: kami middleware is run in sequence, but standard middleware is chained; middleware that expects its code to run after the next handler, such as standard loggers and panic handlers, will not work as expected. Use kami.LogHandler and kami.PanicHandler instead. Standard middleware that does not call the next handler to stop the request is supported.

type Afterware Uses

type Afterware func(context.Context, mutil.WriterProxy, *http.Request) context.Context

Afterware is a function that will run after middleware and the request. Afterware takes the request context and returns a new context, but unlike middleware, returning nil won't halt execution of other afterware.

type AfterwareType Uses

type AfterwareType interface{}

Afterware represents types that kami can convert to Afterware. The following concrete types are accepted:

- Afterware
- func(context.Context, mutil.WriterProxy, *http.Request) context.Context
- func(context.Context, http.ResponseWriter, *http.Request) context.Context
- func(context.Context, *http.Request) context.Context
- func(context.Context) context.Context
- Middleware types

The old x/net/context is also supported.

type ContextHandler Uses

type ContextHandler interface {
    ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request)
}

ContextHandler is like http.Handler but supports context.

type HandlerFunc Uses

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

HandlerFunc is like http.HandlerFunc with context.

func (HandlerFunc) ServeHTTPContext Uses

func (h HandlerFunc) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request)

type HandlerType Uses

type HandlerType interface{}

HandlerType is the type of Handlers and types that kami internally converts to ContextHandler. In order to provide an expressive API, this type is an alias for interface{} that is named for the purposes of documentation, however only the following concrete types are accepted:

- types that implement http.Handler
- types that implement ContextHandler
- func(http.ResponseWriter, *http.Request)
- func(context.Context, http.ResponseWriter, *http.Request)

type Middleware Uses

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

Middleware is a function that takes the current request context and returns a new request context. You can use middleware to build your context before your handler handles a request. As a special case, middleware that returns nil will halt middleware and handler execution (LogHandler will still run).

type MiddlewareType Uses

type MiddlewareType interface{}

MiddlewareType represents types that kami can convert to Middleware. kami will try its best to convert standard, non-context middleware. See the Use function for important information about how kami middleware is run. The following concrete types are accepted:

- Middleware
- func(context.Context, http.ResponseWriter, *http.Request) context.Context
- func(http.ResponseWriter, *http.Request) context.Context
- func(http.Handler) http.Handler               [* see Use docs]
- func(http.ContextHandler) http.ContextHandler [* see Use docs]
- http.Handler 									[read only]
- func(http.ResponseWriter, *http.Request)      [read only]

The old x/net/context is also supported.

type Mux Uses

type Mux struct {
    // Context is the root "god object" for this mux,
    // from which every request's context will derive.
    Context context.Context
    // Cancel will, if true, automatically cancel the context of incoming requests after they finish.
    Cancel bool
    // PanicHandler will, if set, be called on panics.
    // You can use kami.Exception(ctx) within the panic handler to get panic details.
    PanicHandler HandlerType
    // LogHandler will, if set, wrap every request and be called at the very end.
    LogHandler func(context.Context, mutil.WriterProxy, *http.Request)
    // contains filtered or unexported fields
}

Mux is an independent kami router and middleware stack. Manipulating it is not threadsafe.

func New Uses

func New() *Mux

New creates a new independent kami router and middleware stack. It is totally separate from the global kami.Context and middleware stack.

func (Mux) After Uses

func (m Mux) After(path string, afterware AfterwareType)

After registers middleware to run for the given path after normal middleware added with Use has run. See the global After function's documents for information on how middleware works.

func (*Mux) Delete Uses

func (m *Mux) Delete(path string, handler HandlerType)

Delete registers a DELETE handler under the given path.

func (*Mux) EnableMethodNotAllowed Uses

func (m *Mux) EnableMethodNotAllowed(enabled bool)

EnableMethodNotAllowed enables or disables automatic Method Not Allowed handling. Note that this is enabled by default.

func (*Mux) Get Uses

func (m *Mux) Get(path string, handler HandlerType)

Get registers a GET handler under the given path.

func (*Mux) Handle Uses

func (m *Mux) Handle(method, path string, handler HandlerType)

Handle registers an arbitrary method handler under the given path.

func (*Mux) Head Uses

func (m *Mux) Head(path string, handler HandlerType)

Head registers a HEAD handler under the given path.

func (*Mux) MethodNotAllowed Uses

func (m *Mux) MethodNotAllowed(handler HandlerType)

MethodNotAllowed registers a special handler for automatically responding to invalid method requests (405).

func (*Mux) NotFound Uses

func (m *Mux) NotFound(handler HandlerType)

NotFound registers a special handler for unregistered (404) paths. If handle is nil, use the default http.NotFound behavior.

func (*Mux) Options Uses

func (m *Mux) Options(path string, handler HandlerType)

Options registers a OPTIONS handler under the given path.

func (*Mux) Patch Uses

func (m *Mux) Patch(path string, handler HandlerType)

Patch registers a PATCH handler under the given path.

func (*Mux) Post Uses

func (m *Mux) Post(path string, handler HandlerType)

Post registers a POST handler under the given path.

func (*Mux) Put Uses

func (m *Mux) Put(path string, handler HandlerType)

Put registers a PUT handler under the given path.

func (*Mux) Serve Uses

func (m *Mux) Serve()

Serve starts serving this mux with reasonable defaults. The bind address can be changed by setting the GOJI_BIND environment variable, or by setting the "--bind" command line flag. Serve detects einhorn and systemd for you. It works exactly like zenazn/goji. Only one mux may be served at a time.

func (*Mux) ServeHTTP Uses

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles an HTTP request, running middleware and forwarding the request to the appropriate handler. Implements the http.Handler interface for easy composition with other frameworks.

func (*Mux) ServeListener Uses

func (m *Mux) ServeListener(listener net.Listener)

ServeListener is like Serve, but runs kami on top of an arbitrary net.Listener.

func (*Mux) ServeTLS Uses

func (m *Mux) ServeTLS(config *tls.Config)

ServeTLS is like Serve, but enables TLS using the given config.

func (Mux) Use Uses

func (m Mux) Use(path string, mw MiddlewareType)

Use registers middleware to run for the given path. See the global Use function's documents for information on how middleware works.

Directories

PathSynopsis
treemuxPackage treemux is a generic treemux ripped from httptreemux.

Package kami imports 16 packages (graph) and is imported by 34 packages. Updated 2017-11-18. Refresh now. Tools for package owners.