lionrouter

package module
v0.0.0-...-f4429b4 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2020 License: MIT Imports: 5 Imported by: 0

README

LionRouter (Golang path-trie router)

This is a lightweight minimalistic implementation of a go router based on a path trie approach. Don't expect a fully featured router neither now nor in future. Only the most necessary features will be included, based on our needs.

If you need some features which are not included, feel free to add them in fork.

Supported functionality

  • implements the default go http.Handler interface
  • routes
    • static routing
    • parameter routes
      • Named keys
      • Named paths
  • Sub-Routing using the go http.Handler interface
  • Middleware stack using the default wrapper technique through func(next http.Handler) http.Handler

How to use

The router implements the http.Handler interface, so it can be used alongside with other frameworks. Each route handler is using the http.Handler interface itself to maximize compatibility with other packages.

New router instance

New() *Router

router := lionrouter.New()

Define a route

Routes can easily be assigned by using one of the following functions.

Get(path string, handler http.Handler)

Post(path string, handler http.Handler)

Put(path string, handler http.Handler)

Patch(path string, handler http.Handler)

Delete(path string, handler http.Handler)

Head(path string, handler http.Handler)

Options(path string, handler http.Handler)

Static route
router.Get("/contact", contactHandler())

Each request to http://.../contact will be passed through the contactHandler() method.

Parameter routes
Named key
router.Get("/download/:file", downloadHandler())

Each request to http://.../download/someFileName will be passed through the downloadHandler() method where you can get the corresponding value passed by as file by extracing it from the context.

params, ok := lionrouter.Params(r.Context())

if ok {
    w.Write([]byte(params["file"]))
}

or

w.Write([]byte(lionrouter.Param(r.Context(), "file"))

This will generate the output: someFileName. A request like http://.../download/profile_picture.png would get: profile_picture.png.

Multiple named keys per route are possible:

router.Handler(http.MethodGet, "/download/:param1/:param2", downloadHandler())
Named path

Named path is corresponding to the named key functionality except a whole path is read from request.

router.Get("/download/*file", downloadHandler())

Each request to http://.../download/some/path/here/test.png will be passed through the downloadHandler() method where you can get the corresponding value passed by as file by extracing it from the context.

params, ok := lionrouter.Params(r.GetContext())

if ok {
    w.Write([]byte(params["file"]))
}

This will generate the output: /some/path/here/test.png.

A named path can also be combined with named keys:

router.Get("/download/:param1/:param2/*param3", downloadHandler())

Named paths are only possible at the end and cannot be used alongside with the subrouting funciotnality.

Sub-Routing

You can register any http.Handler to a given path. The request will then be passed through the registered Handler. Note that the http.StripPrefix() middleware will be chained before passing through the request to the sub-router handler.

Router(path string, handler http.Handler)

mainRouter := lionrouter.New()
staticRouter := lionrouter.New()

staticRouter.Get("/*file", staticHandler())
mainRouter.Route("/static", staticRouter)

Path to sub-routers doesn't support named paths or keys at this moment

Middleware

To assign any given middleware of the type func(http.Handler) http.Handler, just use the Middleware(...func(http.Handler) Handler) method.

router.Use(someMiddleware)
router.Use(otherMiddleware)

this adds someMiddleware and otherMiddleware. You can also define the middleware at once.

router.Use(someMiddleware, otherMiddleware)

License

MIT licensed 2017-2019 Cedrik Kaufmann. See the LICENSE file for further details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownHTTPMethod = errors.New("unknown or unsupported http method")
	ErrNilHandler        = errors.New("nil handler cannot be assigned")
	ErrAlreadyAssigned   = errors.New("cannot reassign handler")
	ErrAssignment        = errors.New("cannot assign child/leaf to node")
	ErrNoHandler         = errors.New("no handler for http method assigned")
)

Functions

func Param

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

Param extracts the single param value from the given context and key It returns the param as string or an empty string

Types

type Router

type Router struct {

	// 404 and 500 handler which called if no handler for route assigned or error occurs
	NotFoundHandler http.Handler
	// contains filtered or unexported fields
}

Router struct represents router instance which implements the http.Handler interface

func New

func New() *Router

New creates a new router instance returns router instance

func (*Router) Delete

func (r *Router) Delete(path string, handler http.Handler)

Delete assigns the handler as HTTP-DELETE Route for given path

func (*Router) Get

func (r *Router) Get(path string, handler http.Handler)

Get assigns the handler as HTTP-GET Route for given path

func (*Router) Head

func (r *Router) Head(path string, handler http.Handler)

Head assigns the handler as HTTP-HEAD Route for given path

func (*Router) Options

func (r *Router) Options(path string, handler http.Handler)

Options assigns the handler as HTTP-OPTIONS Route for given path

func (*Router) Patch

func (r *Router) Patch(path string, handler http.Handler)

Patch assigns the handler as HTTP-PATCH Route for given path

func (*Router) Post

func (r *Router) Post(path string, handler http.Handler)

Post assigns the handler as HTTP-POST Route for given path

func (*Router) Put

func (r *Router) Put(path string, handler http.Handler)

Put assigns the handler as HTTP-PUT Route for given path

func (*Router) Route

func (r *Router) Route(path string, handler http.Handler)

Route assigns the given handler for given path, if route is called the request is passed through this handler Used for sub-routing, path will be stripped through http.StripPrefix middleware before request passed to handler

func (*Router) ServeHTTP

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

ServeHTTP handles a given request

func (*Router) Use

func (r *Router) Use(middleware ...func(http.Handler) http.Handler)

Use assigns a middleware stack to the whole router instance

type RouterParam

type RouterParam map[string]string

RouterParam map[string]string holds the parameter from each route if set

func Params

func Params(ctx context.Context) (RouterParam, bool)

Params extracts the params map from the given context It returns the params string map or nil

Jump to

Keyboard shortcuts

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