minirouter

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

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

Go to latest
Published: Apr 28, 2021 License: MIT Imports: 3 Imported by: 0

README

Minirouter

go get github.com/ofux/minirouter

Minirouter just adds support for middlewares on top of httprouter. It was inspired by hitch, but minirouter also supports clean definition of sub-paths so that routes can be grouped together.

It is designed to use only standard interfaces such as http.Handler and http.HandlerFunc to define handlers and middlewares, and exclusively uses context to retrieve query parameters. It does not pollute your code with weird custom stuff. Only standard interfaces.

Usage

package main

import (
	"fmt"
	"net/http"
	"log"

	"github.com/ofux/minirouter"
	"github.com/rs/cors"
)

func Index(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello, %s!\n", minirouter.Params(r).ByName("name"))
}

func GetUser(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "hello, user with ID %s!\n", minirouter.Params(r).ByName("id"))
}

func CreateUser(w http.ResponseWriter, r *http.Request) {
	//TODO: do your thing
}

func checkIsAdmin(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		//TODO: check logged user is admin
		next.ServeHTTP(w, r)
	})
}

func main() {
	mr := minirouter.New()
	mr = mr.WithMiddleware(cors.Default().Handler)

	mr.GET("/", Index)
	mr.GET("/hello/:name", Hello)

	mrAdmin := mr.WithBasePath("/admin").WithMiddleware(checkIsAdmin)
	mrAdmin.GET("/users/:id", GetUser)
	mrAdmin.POST("/users", CreateUser)

	log.Fatal(http.ListenAndServe(":8080", mr))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Params

func Params(req *http.Request) httprouter.Params

Params returns the httprouter.Params for request. This is just a pass-through to httprouter.ParamsFromContext.

Types

type Middleware

type Middleware func(next http.Handler) http.Handler

Middleware wraps an http.Handler, returning a new http.Handler.

type Mini

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

Mini adds middlewares on top of httprouter.Router

func New

func New() *Mini

New initializes a new Mini.

func (*Mini) DELETE

func (m *Mini) DELETE(path string, handler http.HandlerFunc, middleware ...Middleware)

DELETE registers a DELETE func handler for the given path.

func (*Mini) GET

func (m *Mini) GET(path string, handler http.HandlerFunc, middleware ...Middleware)

GET registers a GET func handler for the given path.

func (*Mini) Handle

func (m *Mini) Handle(method, path string, handler http.Handler, middleware ...Middleware)

Handle registers a handler for the given method and path.

func (*Mini) HandleFunc

func (m *Mini) HandleFunc(method, path string, handler http.HandlerFunc, middleware ...Middleware)

HandleFunc registers a func handler for the given method and path.

func (*Mini) OPTIONS

func (m *Mini) OPTIONS(path string, handler http.HandlerFunc, middleware ...Middleware)

OPTIONS registers a OPTIONS func handler for the given path.

func (*Mini) PATCH

func (m *Mini) PATCH(path string, handler http.HandlerFunc, middleware ...Middleware)

PATCH registers a PATCH func handler for the given path.

func (*Mini) POST

func (m *Mini) POST(path string, handler http.HandlerFunc, middleware ...Middleware)

POST registers a POST func handler for the given path.

func (*Mini) PUT

func (m *Mini) PUT(path string, handler http.HandlerFunc, middleware ...Middleware)

PUT registers a PUT func handler for the given path.

func (*Mini) Router

func (m *Mini) Router() *httprouter.Router

Router returns the internal httprouter.Router

func (*Mini) ServeHTTP

func (m *Mini) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP makes Mini implement the http.Handler interface.

func (*Mini) WithBasePath

func (m *Mini) WithBasePath(path string) *Mini

WithBasePath returns a a copy of parent with an augmented base-path, in which a set of sub-routes can be defined. It can be used for inner routes that share a common base-path. The new base-path is the concatenation of the parent's base-path and the given path (eg. <parent's base-path>/<path>).

func (*Mini) WithHandlerMiddleware

func (m *Mini) WithHandlerMiddleware(handler http.Handler) *Mini

WithHandlerMiddleware returns a copy of parent with an http.Handler as a new middleware.

func (*Mini) WithMiddleware

func (m *Mini) WithMiddleware(middleware ...Middleware) *Mini

WithMiddleware returns a copy of parent with one or more new middlewares. It can be used for routes that share common middlewares.

Jump to

Keyboard shortcuts

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