router

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2020 License: MIT Imports: 19 Imported by: 0

README

router

Wrapper around Julien Schmidt's httprouter.

This package supports SPAs out of the box.

Install

go get -u "peterdekok.nl/gotools/router"
import "peterdekok.nl/gotools/router"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CorsConfig added in v0.0.8

type CorsConfig struct {
	AllowedOrigin  []string
	AllowedHeaders []string
}

TODO Docs

type HttpRouter added in v0.0.5

type HttpRouter struct {
	*httprouter.Router
}

TODO Docs

func (*HttpRouter) AuthMiddleware added in v0.0.8

func (hr *HttpRouter) AuthMiddleware() *PrefixRouter

TODO Docs

func (*HttpRouter) EnableAuth added in v0.0.8

func (hr *HttpRouter) EnableAuth(ur auth.UserRepository) (*PrefixRouter, *auth.Auth, error)

TODO Docs

func (*HttpRouter) Middleware added in v0.0.6

func (hr *HttpRouter) Middleware(middleware Middleware) *PrefixRouter

Middleware Wraps the current router and adds middleware for all handlers registered through this wrapper.

func (*HttpRouter) Prefix added in v0.0.5

func (hr *HttpRouter) Prefix(prefix string) *PrefixRouter

Prefix wraps the current router and adds middleware for all handlers registered through this wrapper.

func (*HttpRouter) PrefixFunc added in v0.0.5

func (hr *HttpRouter) PrefixFunc(prefix string, fn func(rr Routable)) *PrefixRouter

PrefixFunc wraps the current router and adds middleware for all handlers registered through this wrapper. The wrapper is passed to a callback, to create a nice grouped structure to the code.

func (*HttpRouter) WS added in v0.0.8

func (hr *HttpRouter) WS(path string, wh WebsocketHandler)

TODO Docs

type Middleware added in v0.0.6

type Middleware func(wrapped httprouter.Handle) httprouter.Handle

TODO Docs

func BasicAuth

func BasicAuth() Middleware

TODO Docs

type PrefixRouter added in v0.0.5

type PrefixRouter struct {
	Routable
	// contains filtered or unexported fields
}

TODO Docs

func NewPrefixRouter added in v0.0.5

func NewPrefixRouter(r Routable, prefix string, middleware Middleware) *PrefixRouter

TODO Docs

func (*PrefixRouter) AuthMiddleware added in v0.0.8

func (pr *PrefixRouter) AuthMiddleware() *PrefixRouter

TODO Docs

func (*PrefixRouter) DELETE added in v0.0.5

func (pr *PrefixRouter) DELETE(path string, handle httprouter.Handle)

DELETE is a shortcut for router.Handle(http.MethodDelete, path, handle)

func (*PrefixRouter) EnableAuth added in v0.0.8

func (pr *PrefixRouter) EnableAuth(ur auth.UserRepository) (*PrefixRouter, *auth.Auth, error)

TODO Docs

func (*PrefixRouter) GET added in v0.0.5

func (pr *PrefixRouter) GET(path string, handle httprouter.Handle)

GET is a shortcut for router.Handle(http.MethodGet, path, handle)

func (*PrefixRouter) Group added in v0.0.5

func (pr *PrefixRouter) Group(fn func(rr Routable)) *PrefixRouter

The wrapper (prefix router) is passed to a callback, to create a nice grouped structure to the code.

func (*PrefixRouter) HEAD added in v0.0.5

func (pr *PrefixRouter) HEAD(path string, handle httprouter.Handle)

HEAD is a shortcut for router.Handle(http.MethodHead, path, handle)

func (*PrefixRouter) Handle added in v0.0.5

func (pr *PrefixRouter) Handle(method, path string, handle httprouter.Handle)

Handle registers a new request handle with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*PrefixRouter) Handler added in v0.0.5

func (pr *PrefixRouter) Handler(method, path string, handler http.Handler)

Handler is an adapter which allows the usage of an http.Handler as a request handle. The Params are available in the request context under ParamsKey.

func (*PrefixRouter) HandlerFunc added in v0.0.5

func (pr *PrefixRouter) HandlerFunc(method, path string, handler http.HandlerFunc)

HandlerFunc is an adapter which allows the usage of an http.HandlerFunc as a request handle.

func (*PrefixRouter) Lookup added in v0.0.5

func (pr *PrefixRouter) Lookup(method, path string) (httprouter.Handle, httprouter.Params, bool)

Lookup allows the manual lookup of a method + path combo. This is e.g. useful to build a framework around this router. If the path was found, it returns the handle function and the path parameter values. Otherwise the third return value indicates whether a redirection to the same path with an extra / without the trailing slash should be performed.

func (*PrefixRouter) Middleware added in v0.0.6

func (pr *PrefixRouter) Middleware(middleware Middleware) *PrefixRouter

Wraps the current prefix router and adds middleware for all handlers registered through this wrapper.

func (*PrefixRouter) OPTIONS added in v0.0.5

func (pr *PrefixRouter) OPTIONS(path string, handle httprouter.Handle)

OPTIONS is a shortcut for router.Handle(http.MethodOptions, path, handle)

func (*PrefixRouter) PATCH added in v0.0.5

func (pr *PrefixRouter) PATCH(path string, handle httprouter.Handle)

PATCH is a shortcut for router.Handle(http.MethodPatch, path, handle)

func (*PrefixRouter) POST added in v0.0.5

func (pr *PrefixRouter) POST(path string, handle httprouter.Handle)

POST is a shortcut for router.Handle(http.MethodPost, path, handle)

func (*PrefixRouter) PUT added in v0.0.5

func (pr *PrefixRouter) PUT(path string, handle httprouter.Handle)

PUT is a shortcut for router.Handle(http.MethodPut, path, handle)

func (*PrefixRouter) Prefix added in v0.0.5

func (pr *PrefixRouter) Prefix(prefix string) *PrefixRouter

Wraps the current prefix router and adds a prefix to the path for all handlers registered through this wrapper.

func (*PrefixRouter) PrefixFunc added in v0.0.5

func (pr *PrefixRouter) PrefixFunc(prefix string, fn func(rr Routable)) *PrefixRouter

Wraps the current prefix router and adds a prefix to the path for all handlers registered through this wrapper. The wrapper is passed to a callback, to create a nice grouped structure to the code.

func (*PrefixRouter) ServeFiles added in v0.0.5

func (pr *PrefixRouter) ServeFiles(path string, root http.FileSystem)

ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use http.Dir:

router.ServeFiles("/src/*filepath", http.Dir("/var/www"))

func (*PrefixRouter) ServeHTTP added in v0.0.5

func (pr *PrefixRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

TODO Docs

func (*PrefixRouter) WS added in v0.0.8

func (pr *PrefixRouter) WS(path string, wh WebsocketHandler)

TODO Docs

type RestConfig

type RestConfig struct {
	Listen string
	Port   int

	RequestLogLevel string

	SSL struct {
		Cert string
		Priv string
	}

	Auth struct {
		Basic struct {
			User string
			Pass string
		}

		Bearer struct {
			AccessTokenSecret  string
			RefreshTokenSecret string
		}
	}
}

TODO Docs

type Routable added in v0.0.5

type Routable interface {
	// ServeHTTP will propagate to the nearest actual router
	ServeHTTP(w http.ResponseWriter, r *http.Request)

	// GET is a shortcut for router.Handle(http.MethodGet, path, handle)
	GET(path string, handle httprouter.Handle)

	// HEAD is a shortcut for router.Handle(http.MethodHead, path, handle)
	HEAD(path string, handle httprouter.Handle)

	// OPTIONS is a shortcut for router.Handle(http.MethodOptions, path, handle)
	OPTIONS(path string, handle httprouter.Handle)

	// POST is a shortcut for router.Handle(http.MethodPost, path, handle)
	POST(path string, handle httprouter.Handle)

	// PUT is a shortcut for router.Handle(http.MethodPut, path, handle)
	PUT(path string, handle httprouter.Handle)

	// PATCH is a shortcut for router.Handle(http.MethodPatch, path, handle)
	PATCH(path string, handle httprouter.Handle)

	// DELETE is a shortcut for router.Handle(http.MethodDelete, path, handle)
	DELETE(path string, handle httprouter.Handle)

	// Handle registers a new request handle with the given path and method.
	//
	// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
	// functions can be used.
	//
	// This function is intended for bulk loading and to allow the usage of less
	// frequently used, non-standardized or custom methods (e.g. for internal
	// communication with a proxy).
	Handle(method, path string, handle httprouter.Handle)

	// Handler is an adapter which allows the usage of an http.Handler as a
	// request handle.
	// The Params are available in the request context under ParamsKey.
	Handler(method, path string, handler http.Handler)

	// HandlerFunc is an adapter which allows the usage of an http.HandlerFunc as a
	// request handle.
	HandlerFunc(method, path string, handler http.HandlerFunc)

	// ServeFiles serves files from the given file system root.
	// The path must end with "/*filepath", files are then served from the local
	// path /defined/root/dir/*filepath.
	// For example if root is "/etc" and *filepath is "passwd", the local file
	// "/etc/passwd" would be served.
	// Internally a http.FileServer is used, therefore http.NotFound is used instead
	// of the Router's NotFound handler.
	// To use the operating system's file system implementation,
	// use http.Dir:
	//     router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
	ServeFiles(path string, root http.FileSystem)

	// Lookup allows the manual lookup of a method + path combo.
	// This is e.g. useful to build a framework around this router.
	// If the path was found, it returns the handle function and the path parameter
	// values. Otherwise the third return value indicates whether a redirection to
	// the same path with an extra / without the trailing slash should be performed.
	Lookup(method, path string) (httprouter.Handle, httprouter.Params, bool)

	// Prefix wraps the current router and adds middleware
	// for all handlers registered through this wrapper.
	Prefix(prefix string) *PrefixRouter

	// PrefixFunc wraps the current router and adds middleware
	// for all handlers registered through this wrapper.
	// The wrapper is passed to a callback, to create a nice grouped
	// structure to the code.
	PrefixFunc(prefix string, fn func(rr Routable)) *PrefixRouter

	// Middleware Wraps the current router and adds middleware
	// for all handlers registered through this wrapper.
	Middleware(middleware Middleware) *PrefixRouter

	// TODO Docs
	WS(path string, wh WebsocketHandler)

	// TODO Docs
	EnableAuth(ur auth.UserRepository) (*PrefixRouter, *auth.Auth, error)

	// TODO Docs
	AuthMiddleware() *PrefixRouter
}

TODO Docs

type Router

type Router struct {
	Routable

	// Catchall for SPA (e.g.: index.html)
	SpaHandler http.Handler

	// Dist folder (for static files like js, css, images, etc.)
	DistHandler http.Handler
	DistPrefix  string

	// Prefix for router (API, websocket, custom go handlers, etc.)
	RouterPrefix string

	// Handlers for specific handlers that should be accessible outside dist/router (e.g.: favicon.ico)
	StaticRootHandlers map[string]http.Handler

	// TODO Docs
	CorsConfig *CorsConfig
}

TODO Docs

func New

func New(corsConfig *CorsConfig) *Router

TODO Docs

func NewSPA added in v0.0.2

func NewSPA(spaConfig SPAConfig) *Router

TODO Docs

func (*Router) AuthMiddleware added in v0.0.8

func (rtr *Router) AuthMiddleware() *PrefixRouter

TODO Docs

func (*Router) EnableAuth added in v0.0.8

func (rtr *Router) EnableAuth(ur auth.UserRepository) (*PrefixRouter, *auth.Auth, error)

TODO Docs

func (*Router) Middleware added in v0.0.6

func (rtr *Router) Middleware(middleware Middleware) *PrefixRouter

Middleware Wraps the current router and adds middleware for all handlers registered through this wrapper.

func (*Router) Prefix added in v0.0.5

func (rtr *Router) Prefix(prefix string) *PrefixRouter

Prefix wraps the current router and adds middleware for all handlers registered through this wrapper.

func (*Router) PrefixFunc added in v0.0.5

func (rtr *Router) PrefixFunc(prefix string, fn func(rr Routable)) *PrefixRouter

PrefixFunc wraps the current router and adds middleware for all handlers registered through this wrapper. The wrapper is passed to a callback, to create a nice grouped structure to the code.

func (*Router) Serve

func (rtr *Router) Serve()

TODO Docs

type SPAConfig added in v0.0.5

type SPAConfig struct {
	SpaHandler         http.Handler
	DistHandler        http.Handler
	StaticRootHandlers map[string]http.Handler

	DistPrefix   string
	RouterPrefix string

	RootURIDist []string

	CorsConfig *CorsConfig
}

TODO Docs

type TokenSource added in v0.0.6

type TokenSource func() string

TODO Docs

type WebsocketHandler added in v0.0.8

type WebsocketHandler func(ctx context.Context, conn *websocket.Conn, r *http.Request) error

TODO Docs

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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