Documentation ¶
Index ¶
- Variables
- func After(path string, aw AfterwareType)
- func Delete(path string, handler HandlerType)
- func EnableMethodNotAllowed(enabled bool)
- func Exception(ctx context.Context) interface{}
- func Get(path string, handler HandlerType)
- func Handle(method, path string, handler HandlerType)
- func Handler() http.Handler
- func Head(path string, handler HandlerType)
- func MethodNotAllowed(handler HandlerType)
- func NotFound(handler HandlerType)
- func Options(path string, handler HandlerType)
- func Param(ctx context.Context, name string) string
- func Patch(path string, handler HandlerType)
- func Post(path string, handler HandlerType)
- func Put(path string, handler HandlerType)
- func Reset()
- func Serve()
- func ServeListener(listener net.Listener)
- func ServeTLS(config *tls.Config)
- func SetParam(ctx context.Context, name string, value string) context.Context
- func Use(path string, mw MiddlewareType)
- type Afterware
- type AfterwareType
- type ContextHandler
- type HandlerFunc
- type HandlerType
- type Middleware
- type MiddlewareType
- type Mux
- func (m Mux) After(path string, afterware AfterwareType)
- func (m *Mux) Delete(path string, handler HandlerType)
- func (m *Mux) EnableMethodNotAllowed(enabled bool)
- func (m *Mux) Get(path string, handler HandlerType)
- func (m *Mux) Handle(method, path string, handler HandlerType)
- func (m *Mux) Head(path string, handler HandlerType)
- func (m *Mux) MethodNotAllowed(handler HandlerType)
- func (m *Mux) NotFound(handler HandlerType)
- func (m *Mux) Options(path string, handler HandlerType)
- func (m *Mux) Patch(path string, handler HandlerType)
- func (m *Mux) Post(path string, handler HandlerType)
- func (m *Mux) Put(path string, handler HandlerType)
- func (m *Mux) Serve()
- func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *Mux) ServeListener(listener net.Listener)
- func (m *Mux) ServeTLS(config *tls.Config)
- func (m Mux) Use(path string, mw MiddlewareType)
- type OldContextHandler
Constants ¶
This section is empty.
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) )
Functions ¶
func After ¶ added in v1.3.1
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 ¶
func Delete(path string, handler HandlerType)
Delete registers a DELETE handler under the given path.
func EnableMethodNotAllowed ¶ added in v1.3.1
func EnableMethodNotAllowed(enabled bool)
EnableMethodNotAllowed enables or disables automatic Method Not Allowed handling. Note that this is enabled by default.
func Exception ¶
Exception gets the "v" in panic(v). The panic details. Only PanicHandler will receive a context you can use this with.
func Get ¶
func Get(path string, handler HandlerType)
Get registers a GET handler under the given path.
func Handle ¶
func Handle(method, path string, handler HandlerType)
Handle registers an arbitrary method handler under the given path.
func Head ¶
func Head(path string, handler HandlerType)
Head registers a HEAD handler under the given path.
func MethodNotAllowed ¶ added in v1.3.1
func MethodNotAllowed(handler HandlerType)
MethodNotAllowed registers a special handler for automatically responding to invalid method requests (405).
func NotFound ¶
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 ¶
func Options(path string, handler HandlerType)
Head registers a OPTIONS handler under the given path.
func Param ¶
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 ¶
func Patch(path string, handler HandlerType)
Patch registers a PATCH handler under the given path.
func Post ¶
func Post(path string, handler HandlerType)
Post registers a POST handler under the given path.
func Put ¶
func Put(path string, handler HandlerType)
Put registers a PUT handler under the given path.
func Reset ¶
func Reset()
Reset changes the root Context to context.Background(). It removes every handler and all middleware.
func Serve ¶
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 ¶ added in v1.3.1
ServeListener is like Serve, but runs kami on top of an arbitrary net.Listener.
func SetParam ¶
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 ¶
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.
Types ¶
type Afterware ¶ added in v1.3.1
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 ¶ added in v1.3.1
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 ¶
type ContextHandler interface {
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request)
}
ContextHandler is like http.Handler but supports context.
type HandlerFunc ¶
HandlerFunc is like http.HandlerFunc with context.
func (HandlerFunc) ServeHTTPContext ¶
func (h HandlerFunc) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request)
type HandlerType ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶ added in v1.3.1
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 ¶
func (m *Mux) Delete(path string, handler HandlerType)
Delete registers a DELETE handler under the given path.
func (*Mux) EnableMethodNotAllowed ¶ added in v1.3.1
EnableMethodNotAllowed enables or disables automatic Method Not Allowed handling. Note that this is enabled by default.
func (*Mux) Get ¶
func (m *Mux) Get(path string, handler HandlerType)
Get registers a GET handler under the given path.
func (*Mux) Handle ¶
func (m *Mux) Handle(method, path string, handler HandlerType)
Handle registers an arbitrary method handler under the given path.
func (*Mux) Head ¶
func (m *Mux) Head(path string, handler HandlerType)
Head registers a HEAD handler under the given path.
func (*Mux) MethodNotAllowed ¶ added in v1.3.1
func (m *Mux) MethodNotAllowed(handler HandlerType)
MethodNotAllowed registers a special handler for automatically responding to invalid method requests (405).
func (*Mux) NotFound ¶
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 ¶
func (m *Mux) Options(path string, handler HandlerType)
Options registers a OPTIONS handler under the given path.
func (*Mux) Patch ¶
func (m *Mux) Patch(path string, handler HandlerType)
Patch registers a PATCH handler under the given path.
func (*Mux) Post ¶
func (m *Mux) Post(path string, handler HandlerType)
Post registers a POST handler under the given path.
func (*Mux) Put ¶
func (m *Mux) Put(path string, handler HandlerType)
Put registers a PUT handler under the given path.
func (*Mux) Serve ¶
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 ¶
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 ¶
ServeListener is like Serve, but runs kami on top of an arbitrary net.Listener.
func (Mux) Use ¶
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.
type OldContextHandler ¶
type OldContextHandler interface {
ServeHTTPContext(netcontext.Context, http.ResponseWriter, *http.Request)
}
OldContextHandler is like ContextHandler but uses the old x/net/context.