bone

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2019 License: MIT Imports: 5 Imported by: 295

README

bone GoDoc Build Status Go Report Card

What is bone ?

Bone is a lightweight and lightning fast HTTP Multiplexer for Golang. It support :

  • URL Parameters
  • REGEX Parameters
  • Wildcard routes
  • Router Prefix
  • Route params validators
  • Sub Router, mux.SubRoute(), support most standard router (bone, gorilla/mux, httpRouter etc...)
  • Http method declaration
  • Support for http.Handler and http.HandlerFunc
  • Custom NotFound handler
  • Respect the Go standard http.Handler interface

alt tag

Speed

- BenchmarkBoneMux        10000000               118 ns/op
- BenchmarkZeusMux          100000               144 ns/op
- BenchmarkHttpRouterMux  10000000               134 ns/op
- BenchmarkNetHttpMux      3000000               580 ns/op
- BenchmarkGorillaMux       300000              3333 ns/op
- BenchmarkGorillaPatMux   1000000              1889 ns/op

These tests are just for fun, all these routers are great and efficient. Bone isn't the fastest router for every job.

Example


package main

import(
  "net/http"

  "github.com/go-zoo/bone"
)

func main () {
  mux := bone.New()

  mux.RegisterValidatorFunc("isNum", func(s string) bool {
    if _, err := strconv.Atoi(s); err == nil {
      return true
    }
    return false
  })

  // mux.Get, Post, etc ... takes http.Handler
  // validator for route parameter
  mux.Get("/home/:id|isNum", http.HandlerFunc(HomeHandler))
  // multiple parameter
  mux.Get("/profil/:id/:var", http.HandlerFunc(ProfilHandler))
  mux.Post("/data", http.HandlerFunc(DataHandler))

  // Support REGEX Route params
  mux.Get("/index/#id^[0-9]$", http.HandlerFunc(IndexHandler))

  // Handle take http.Handler
  mux.Handle("/", http.HandlerFunc(RootHandler))

  // GetFunc, PostFunc etc ... takes http.HandlerFunc
  mux.GetFunc("/test", Handler)

  http.ListenAndServe(":8080", mux)
}

func Handler(rw http.ResponseWriter, req *http.Request) {
  // Get the value of the "id" parameters.
  val := bone.GetValue(req, "id")

  rw.Write([]byte(val))
}

Blog Posts

Libs

  • Errors dump for Go : Trash
  • Middleware Chaining module : Claw

Documentation

Index

Constants

View Source
const (
	//PARAM value store in Atts if the route have parameters
	PARAM = 2
	//SUB value store in Atts if the route is a sub router
	SUB = 4
	//WC value store in Atts if the route have wildcard
	WC = 8
	//REGEX value store in Atts if the route contains regex
	REGEX = 16
)

Variables

This section is empty.

Functions

func GetAllQueries

func GetAllQueries(req *http.Request) map[string][]string

GetAllQueries return all queries of the current *http.Request

func GetAllValues

func GetAllValues(req *http.Request) map[string]string

GetAllValues return the req PARAMs

func GetQuery

func GetQuery(req *http.Request, key string) []string

GetQuery return the key value, of the current *http.Request query

func GetValue

func GetValue(req *http.Request, key string) string

GetValue return the key value, of the current *http.Request

Types

type Mux

type Mux struct {
	Routes map[string][]*Route

	Validators    map[string]Validator
	Serve         func(rw http.ResponseWriter, req *http.Request)
	CaseSensitive bool
	// contains filtered or unexported fields
}

Mux have routes and a notFound handler Route: all the registred route notFound: 404 handler, default http.NotFound if not provided

func New

func New(adapters ...adapter) *Mux

New create a pointer to a Mux instance

func (*Mux) DefaultServe

func (m *Mux) DefaultServe(rw http.ResponseWriter, req *http.Request)

DefaultServe is the default http request handler

func (*Mux) Delete

func (m *Mux) Delete(path string, handler http.Handler) *Route

Delete add a new route to the Mux with the Delete method

func (*Mux) DeleteFunc

func (m *Mux) DeleteFunc(path string, handler http.HandlerFunc) *Route

DeleteFunc add a new route to the Mux with the Delete method

func (*Mux) Get

func (m *Mux) Get(path string, handler http.Handler) *Route

Get add a new route to the Mux with the Get method

func (*Mux) GetFunc

func (m *Mux) GetFunc(path string, handler http.HandlerFunc) *Route

GetFunc add a new route to the Mux with the Get method

func (*Mux) GetRequestRoute

func (m *Mux) GetRequestRoute(req *http.Request) string

GetRequestRoute returns the route of given Request

func (*Mux) Handle

func (m *Mux) Handle(path string, handler http.Handler)

Handle add a new route to the Mux without a HTTP method

func (*Mux) HandleFunc

func (m *Mux) HandleFunc(path string, handler http.HandlerFunc)

HandleFunc is use to pass a func(http.ResponseWriter, *Http.Request) instead of http.Handler

func (*Mux) HandleNotFound

func (m *Mux) HandleNotFound(rw http.ResponseWriter, req *http.Request)

HandleNotFound handle when a request does not match a registered handler.

func (*Mux) Head

func (m *Mux) Head(path string, handler http.Handler) *Route

Head add a new route to the Mux with the Head method

func (*Mux) HeadFunc

func (m *Mux) HeadFunc(path string, handler http.HandlerFunc) *Route

HeadFunc add a new route to the Mux with the Head method

func (*Mux) ListenAndServe

func (m *Mux) ListenAndServe(port string) error

ListenAndServe wrapper

func (*Mux) NotFound

func (m *Mux) NotFound(handler http.Handler)

NotFound the mux custom 404 handler

func (*Mux) NotFoundFunc

func (m *Mux) NotFoundFunc(handler http.HandlerFunc)

NotFoundFunc the mux custom 404 handler

func (*Mux) Options

func (m *Mux) Options(path string, handler http.Handler) *Route

Options add a new route to the Mux with the Options method

func (*Mux) OptionsFunc

func (m *Mux) OptionsFunc(path string, handler http.HandlerFunc) *Route

OptionsFunc add a new route to the Mux with the Options method

func (*Mux) Patch

func (m *Mux) Patch(path string, handler http.Handler) *Route

Patch add a new route to the Mux with the Patch method

func (*Mux) PatchFunc

func (m *Mux) PatchFunc(path string, handler http.HandlerFunc) *Route

PatchFunc add a new route to the Mux with the Patch method

func (*Mux) Post

func (m *Mux) Post(path string, handler http.Handler) *Route

Post add a new route to the Mux with the Post method

func (*Mux) PostFunc

func (m *Mux) PostFunc(path string, handler http.HandlerFunc) *Route

PostFunc add a new route to the Mux with the Post method

func (*Mux) Prefix

func (m *Mux) Prefix(p string) *Mux

Prefix set a default prefix for all routes registred on the router

func (*Mux) Put

func (m *Mux) Put(path string, handler http.Handler) *Route

Put add a new route to the Mux with the Put method

func (*Mux) PutFunc

func (m *Mux) PutFunc(path string, handler http.HandlerFunc) *Route

PutFunc add a new route to the Mux with the Put method

func (*Mux) Register

func (m *Mux) Register(method string, path string, handler http.Handler) *Route

Register the route in the router

func (*Mux) RegisterValidator

func (m *Mux) RegisterValidator(name string, validator Validator)

RegisterValidator makes the provided validator available to the routes register on that mux

func (*Mux) RegisterValidatorFunc

func (m *Mux) RegisterValidatorFunc(name string, validator func(string) bool)

RegisterValidatorFunc makes the provided function available to the routes register on that mux as a validator

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP pass the request to the serve method of Mux

func (*Mux) SubRoute

func (m *Mux) SubRoute(path string, router Router) *Route

SubRoute register a router as a SubRouter of bone

type Route

type Route struct {
	Path   string
	Method string
	Size   int
	Atts   int

	Token   Token
	Pattern map[int]string
	Compile map[int]*regexp.Regexp
	Tag     map[int]string
	Handler http.Handler
	// contains filtered or unexported fields
}

Route content the required information for a valid route Path: is the Route URL Size: is the length of the path Token: is the value of each part of the path, split by / Pattern: is content information about the route, if it's have a route variable handler: is the handler who handle this route Method: define HTTP method on the route

func NewRoute

func NewRoute(mux *Mux, url string, h http.Handler) *Route

NewRoute return a pointer to a Route instance and call save() on it

func (*Route) Delete

func (r *Route) Delete() *Route

Delete set the route method to Delete

func (*Route) Get

func (r *Route) Get() *Route

Get set the route method to Get

func (*Route) Head

func (r *Route) Head() *Route

Head set the route method to Head

func (*Route) Match

func (r *Route) Match(req *http.Request) bool

Match check if the request match the route Pattern

func (*Route) Options

func (r *Route) Options() *Route

Options set the route method to Options

func (*Route) Patch

func (r *Route) Patch() *Route

Patch set the route method to Patch

func (*Route) Post

func (r *Route) Post() *Route

Post set the route method to Post

func (*Route) Put

func (r *Route) Put() *Route

Put set the route method to Put

func (*Route) ServeHTTP

func (r *Route) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type Router

type Router interface {
	ServeHTTP(http.ResponseWriter, *http.Request)
}

Router is the same as a http.Handler

type Token

type Token struct {
	Tokens []string
	Size   int
	// contains filtered or unexported fields
}

Token content all value of a spliting route path Tokens: string value of each token size: number of token

type Validator

type Validator interface {
	Validate(string) bool
}

Validator can be passed to a route to validate the params

Directories

Path Synopsis
example
001
002
004
003 Module

Jump to

Keyboard shortcuts

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