bon

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2019 License: MIT Imports: 6 Imported by: 1

README

BON

Bon is fast http router of Go designed by Patricia tree

GoDoc Widget

Features

  • Lightweight
  • Middleware framework
  • Not use third party package
  • Standard http request handler
  • Flexible routing

Match Patterns

Priority high order
  1. static is exact match
    • /users/taro
  2. param is directorys range match
    • /users/:name
  3. any is all range match
    • /*
package main

import (
	"net/http"

	"github.com/nissy/bon"
)

func main() {
	r := bon.NewRouter()

	r.Get("/users/taro", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("static"))
	})
	r.Get("/users/:name", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("param name is " + bon.URLParam(r, "name")))
	})
	r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("any"))
	})

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

Example

  • Group is inherits middleware and grants a prefix
  • Route is does not inherit middleware
package main

import (
	"net/http"

	"github.com/nissy/bon"
	"github.com/nissy/bon/middleware"
)

func main() {
	r := bon.NewRouter()

	v := r.Group("/v1")
	v.Use(
		middleware.CORS(middleware.AccessControlConfig{
			AllowOrigin:      "*",
			AllowCredentials: false,
			AllowMethods: []string{
				http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions,
			},
			AllowHeaders: []string{
				"Authorization",
			},
			ExposeHeaders: []string{
				"link",
			},
			MaxAge: 86400,
		}),
	)
	v.Options("*", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNoContent)
	})
	v.Get("/users/:name", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hallo," + bon.URLParam(r, "name")))
	})

	admin := r.Group("/admin")
	admin.Use(
		middleware.BasicAuth([]middleware.BasicAuthUser{
			{
				Name:     "name",
				Password: "password",
			},
		}),
	)
	admin.Get("/users/:name", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hallo, admin " + bon.URLParam(r, "name")))
	})

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

Benchmarks

GitHub

The GitHub API is rather large, consisting of 203 routes. The tasks are basically the same as in the benchmarks before.

Bon            10000     105265 ns/op     42753 B/op     167 allocs/op

Other http routers

Beego           3000     464848 ns/op     74707 B/op     812 allocs/op
Chi            10000     152969 ns/op     61714 B/op     406 allocs/op
Denco          20000      62366 ns/op     20224 B/op     167 allocs/op
GorillaMux       300    4686063 ns/op    215088 B/op    2272 allocs/op
Gin           100000      22283 ns/op         0 B/op       0 allocs/op
HttpRouter     30000      41143 ns/op     13792 B/op     167 allocs/op
LARS           50000      22996 ns/op         0 B/op       0 allocs/op
Possum         10000     212328 ns/op     84451 B/op     609 allocs/op
Rivet          20000      72324 ns/op     16272 B/op     167 allocs/op
Tango           5000     285607 ns/op     63834 B/op    1618 allocs/op
Vulcan         10000     177044 ns/op     19894 B/op     609 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func URLParam

func URLParam(r *http.Request, key string) string

Types

type Context

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

func (*Context) GetParam added in v1.2.0

func (ctx *Context) GetParam(key string) string

func (*Context) PutParam added in v1.2.0

func (ctx *Context) PutParam(key, value string)

func (*Context) WithContext

func (ctx *Context) WithContext(r *http.Request) *http.Request

allocate

type Group

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

func (*Group) Connect

func (g *Group) Connect(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Delete

func (g *Group) Delete(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) FileServer added in v1.3.0

func (g *Group) FileServer(pattern, root string, middlewares ...Middleware)

func (*Group) Get

func (g *Group) Get(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Group

func (g *Group) Group(pattern string, middlewares ...Middleware) *Group

func (*Group) Handle

func (g *Group) Handle(method, pattern string, handler http.Handler, middlewares ...Middleware)

func (*Group) Head

func (g *Group) Head(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Options

func (g *Group) Options(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Patch

func (g *Group) Patch(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Post

func (g *Group) Post(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Put

func (g *Group) Put(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Route added in v1.2.0

func (g *Group) Route(middlewares ...Middleware) *Route

func (*Group) Trace

func (g *Group) Trace(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Group) Use

func (g *Group) Use(middlewares ...Middleware)

type Middleware

type Middleware func(http.Handler) http.Handler

type Mux

type Mux struct {
	NotFound http.HandlerFunc
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Mux

func (*Mux) Connect

func (m *Mux) Connect(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) Delete

func (m *Mux) Delete(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) FileServer

func (m *Mux) FileServer(pattern, root string, middlewares ...Middleware)

func (*Mux) Get

func (m *Mux) Get(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) Group

func (m *Mux) Group(pattern string, middlewares ...Middleware) *Group

func (*Mux) Handle

func (m *Mux) Handle(method, pattern string, handler http.Handler, middlewares ...Middleware)

func (*Mux) Head

func (m *Mux) Head(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) NewContext added in v1.2.0

func (m *Mux) NewContext() *Context

func (*Mux) Options

func (m *Mux) Options(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) Patch

func (m *Mux) Patch(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) Post

func (m *Mux) Post(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) Put

func (m *Mux) Put(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) Route added in v1.2.0

func (m *Mux) Route(middlewares ...Middleware) *Route

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Mux) Trace

func (m *Mux) Trace(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Mux) Use

func (m *Mux) Use(middlewares ...Middleware)

type Route added in v1.2.0

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

func (*Route) Connect added in v1.2.0

func (r *Route) Connect(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Delete added in v1.2.0

func (r *Route) Delete(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) FileServer added in v1.3.0

func (r *Route) FileServer(pattern, root string, middlewares ...Middleware)

func (*Route) Get added in v1.2.0

func (r *Route) Get(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Group added in v1.2.0

func (r *Route) Group(pattern string, middlewares ...Middleware) *Group

func (*Route) Handle added in v1.2.0

func (r *Route) Handle(method, pattern string, handler http.Handler, middlewares ...Middleware)

func (*Route) Head added in v1.2.0

func (r *Route) Head(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Options added in v1.2.0

func (r *Route) Options(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Patch added in v1.2.0

func (r *Route) Patch(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Post added in v1.2.0

func (r *Route) Post(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Put added in v1.2.0

func (r *Route) Put(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Route added in v1.2.0

func (r *Route) Route(middlewares ...Middleware) *Route

func (*Route) Trace added in v1.2.0

func (r *Route) Trace(pattern string, handlerFunc http.HandlerFunc, middlewares ...Middleware)

func (*Route) Use added in v1.2.0

func (r *Route) Use(middlewares ...Middleware)

type Router added in v1.3.0

type Router interface {
	Handle(method, pattern string, handler http.Handler, middlewares ...Middleware)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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