bone

package module
v0.0.0-...-58d232d Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2015 License: MIT Imports: 5 Imported by: 11

README

bone GoDoc Build Status

NOTICE

The Original repo have move to https://github.com/go-zoo/bone

What is bone ?

Bone is a lightweight and lightning fast HTTP Multiplexer for Golang. It support URL variables, http method declaration and custom NotFound handler.

alt tag

Update

After trying to find a way of using the default url.Query() for route parameters, i decide to change the way bone is dealing with this. url.Query() is too slow for good router performance. So now to get the parameters value in your handler, you need to use bone.GetValue(request, key) instead of req.Url.Query().Get(key). This change give a big speed improvement for every kind of application using route parameters, like ~80x faster ... Really sorry for breaking things, but i think it's worth it.

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 test are just for fun, all these router are great and really efficient. Bone do not pretend to be the fastest router for every job.

Example


package main

import(
  "net/http"

  "github.com/squiidz/bone"
)

func main () {
  mux := bone.New()
  
  // Method takes http.HandlerFunc
  mux.Get("/home/:id", HomeHandler)
  mux.Post("/data", DataHandler)

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

  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))
}

TODO

  • More Testing
  • Debugging
  • Optimisation
  • Refactoring

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Write Tests!
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create new Pull Request

License

MIT

Middleware Chaining module : Claw

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	METHOD = []string{"GET", "POST", "PUT", "DELETE", "HEAD", "PATCH", "OPTIONS"}
	VARS   = make(map[*http.Request]*Route)
)

Functions

func GetValue

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

Return the key value, of the current *http.Request

Types

type ByLength

type ByLength []*Route

func (ByLength) Len

func (b ByLength) Len() int

func (ByLength) Less

func (b ByLength) Less(i int, j int) bool

func (ByLength) Swap

func (b ByLength) Swap(i int, j int)

type Mux

type Mux struct {
	Routes map[string][]*Route
	Static []*Route
	// 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() *Mux

New create a pointer to a Mux instance

func (*Mux) BadRequest

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

If no pattern are set in the route. Handle the bad request

func (*Mux) Delete

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

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

func (*Mux) Get

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

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

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) Head

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

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

func (*Mux) NotFound

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

Set the mux custom 404 handler

func (*Mux) Options

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

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

func (*Mux) Patch

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

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

func (*Mux) Post

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

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

func (*Mux) Put

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

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

func (*Mux) ServeHTTP

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

Serve http request

type Pattern

type Pattern struct {
	Exist bool
	Id    string
	Pos   int
	Value map[string]string
}

Pattern content the required information for the route Pattern Exist: check if a variable was declare on the route Id: the name of the variable Pos: postition of var in the route path Value: is the value of the request parameters

type Route

type Route struct {
	Path    string
	Size    int
	Token   Token
	Pattern Pattern
	Handler http.Handler
	Method  string
}

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(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

Set the route method to Delete

func (*Route) Get

func (r *Route) Get() *Route

Set the route method to Get

func (*Route) Head

func (r *Route) Head() *Route

Set the route method to Head

func (*Route) Info

func (r *Route) Info()

Info is only used for debugging

func (*Route) Match

func (r *Route) Match(path string) (url.Values, bool)

Check if the request match the route Pattern

func (*Route) MethCheck

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

Check if the request respect the route method if provided.

func (*Route) Options

func (r *Route) Options() *Route

Set the route method to Options

func (*Route) Patch

func (r *Route) Patch() *Route

Set the route method to Patch

func (*Route) Post

func (r *Route) Post() *Route

Set the route method to Post

func (*Route) Put

func (r *Route) Put() *Route

Set the route method to Put

func (Route) ServeHTTP

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

Only using this in squiidz/fur package

type Token

type Token struct {
	Tokens []string
	Size   int
}

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

Directories

Path Synopsis
example
001
002

Jump to

Keyboard shortcuts

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