Documentation
¶
Overview ¶
Example ¶
package main import ( "net/http" "github.com/go-http-utils/compose" ) var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) const ( middleware1 = iota middleware2 middleware3 middleware4 middleware5 arg1 arg2 arg3 arg4 arg5 ) func main() { mux := http.NewServeMux() mux.Handle("/example1", compose.New(handler). Use(middleware1, arg1, arg2). Use(middleware2, arg3). Use(middleware3). Handler()) mux.Handle("/example2", compose.New(handler). UseMiddlewares([]compose.Middleware{ compose.Middleware{Fun: middleware3, Opts: []interface{}{arg4, arg5}}, compose.Middleware{Fun: middleware4}, }). Handler()) http.ListenAndServe(":8080", compose.New(mux).Use(middleware5).Handler()) }
Index ¶
Examples ¶
Constants ¶
View Source
const Version = "0.1.0"
Version is this package's version
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Composer ¶
type Composer struct {
// contains filtered or unexported fields
}
Composer wraps the inner http.Handler and can be used to append middlewares to it.
func (*Composer) Use ¶
Use appends the middleware to the inner handler. Accept the middleware function as the first argument and the first argument of the middleware function should be a http.Handler and the return value should also be http.Handler ( middlewareFunc(h http.Handler, ...) http.Handler ).
Example ¶
package main import ( "net/http" "github.com/go-http-utils/compose" ) var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) const ( middleware1 = iota middleware2 middleware3 middleware4 middleware5 arg1 arg2 arg3 arg4 arg5 ) func main() { mux := http.NewServeMux() mux.Handle("/example1", compose.New(handler). Use(middleware1, arg1, arg2). Use(middleware2, arg3). Use(middleware3). Handler()) http.ListenAndServe(":8080", compose.New(mux).Use(middleware5).Handler()) }
func (*Composer) UseMiddlewares ¶
func (c *Composer) UseMiddlewares(ms []Middleware) *Composer
UseMiddlewares appends the specfied middlewares to the inner handler.
Example ¶
package main import ( "net/http" "github.com/go-http-utils/compose" ) var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) const ( middleware1 = iota middleware2 middleware3 middleware4 middleware5 arg1 arg2 arg3 arg4 arg5 ) func main() { mux := http.NewServeMux() mux.Handle("/example2", compose.New(handler). UseMiddlewares([]compose.Middleware{ compose.Middleware{Fun: middleware3, Opts: []interface{}{arg4, arg5}}, compose.Middleware{Fun: middleware4}, }). Handler()) http.ListenAndServe(":8080", compose.New(mux).Use(middleware5).Handler()) }
type Middleware ¶
type Middleware struct { // Func is the middleware function. The first argument of the // function should be a http.Handler and the return value should also be a // http.Handler ( middlewareFunc(h http.Handler, ...) http.Handler ). Fun interface{} // Opts are the arguments to pass to the middleware function. Opts []interface{} }
Middleware includes the middleware function and the arguments to be passed.
Click to show internal directories.
Click to hide internal directories.