muxinator

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2020 License: MIT Imports: 5 Imported by: 5

README

Muxinator

Muxinator is a simple wrapper around the gorilla/mux and urfave/negroni packages to make it easier to build an HTTP router with middleware.

Example

router := muxinator.NewRouter()
router.AddMiddleware(globalMiddleware)
router.Get("/path", middleware1, middleware2)
router.Patch("/path", middleware2, middleware3)
http.ListenAndServe(":80", router.BuildHandler())

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Middleware replicates the negroni.HandlerFunc type but decouples the code from the library

type Router

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

Router is a wrapper around the gorilla mux router and the negroni middleware library. It has some convenience functions to make it easier to do per-route middleware

func NewRouter

func NewRouter() Router

NewRouter returns a new Router instance with some defaults

func (*Router) AddMiddleware

func (router *Router) AddMiddleware(middlewares ...Middleware)

AddMiddleware adds middleware that will be applied to every request. Middleware handlers are executed in the order defined.

func (*Router) BuildHandler

func (router *Router) BuildHandler() http.Handler

BuildHandler returns an http.Handler that can be used as the argument to http.ListenAndServe.

func (*Router) Delete

func (router *Router) Delete(path string, handler http.Handler, middlewares ...Middleware)

Delete is a helper function to add a DELETE route

func (*Router) Get

func (router *Router) Get(path string, handler http.Handler, middlewares ...Middleware)

Get is a helper function to add a GET route

func (*Router) Handle

func (router *Router) Handle(method string, path string, handler http.Handler, middlewares ...Middleware)

Handle registers a route with the router. Internally, gorilla mux is used. See https://github.com/gorilla/mux for options available for the path, including variables. End a path with an asterisk "*" to create a wildcard route.

func (*Router) ListenAndServe

func (router *Router) ListenAndServe(addr string) error

ListenAndServe builds the final handler and passes it to http.ListenAndServe

func (*Router) Match

func (router *Router) Match(req *http.Request) bool

Match returns whether a route exists for the given request

func (*Router) Patch

func (router *Router) Patch(path string, handler http.Handler, middlewares ...Middleware)

Patch is a helper function to add a PATCH route

func (*Router) Post

func (router *Router) Post(path string, handler http.Handler, middlewares ...Middleware)

Post is a helper function to add a POST route

func (*Router) Put

func (router *Router) Put(path string, handler http.Handler, middlewares ...Middleware)

Put is a helper function to add a PUT route

func (*Router) Shutdown

func (router *Router) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL