top
(README.md)
bone [![GoDoc](https://godoc.org/github.com/squiidz/bone?status.png)](http://godoc.org/github.com/go-zoo/bone) [![Build Status](https://travis-ci.org/go-zoo/bone.svg)](https://travis-ci.org/go-zoo/bone) [![Go Report Card](https://goreportcard.com/badge/go-zoo/bone)](https://goreportcard.com/report/go-zoo/bone)
=======

## What is bone ?

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

- URL Parameters
- REGEX Parameters
- Wildcard routes
- Router Prefix
- 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](https://c2.staticflickr.com/2/1070/540747396_5542b42cca_z.jpg)

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

``` go

package main

import(
  "net/http"

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

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

  // mux.Get, Post, etc ... takes http.Handler
  mux.Get("/home/:id", http.HandlerFunc(HomeHandler))
  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.HandleFunc(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))
}

```
## Changelog

#### Update 25 September 2015

- Add support for Sub router

Example :
``` go
func main() {
    mux := bone.New()
    sub := mux.NewRouter()

    sub.GetFunc("/test/example", func(rw http.ResponseWriter, req *http.Request) {
        rw.Write([]byte("From sub router !"))
    })

    mux.SubRoute("/api", sub)

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

```


#### Update 26 April 2015

- Add Support for REGEX parameters, using ` # ` instead of ` : `.
- Add Mux method ` mux.GetFunc(), mux.PostFunc(), etc ... `, takes ` http.HandlerFunc ` instead of ` http.Handler `.

Example :
``` go
func main() {
    mux.GetFunc("/route/#var^[a-z]$", handler)
}

func handler(rw http.ResponseWriter, req *http.Request) {
    bone.GetValue(req, "var")
}
```

#### Update 29 january 2015

- Speed improvement for url Parameters, from ```~ 1500 ns/op ``` to ```~ 1000 ns/op ```.

#### Update 25 december 2014

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(req, 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.  

## TODO

- DOC
- More Testing
- Debugging
- Optimisation

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

## Links
- Blog post talking about bone : http://www.peterbe.com/plog/my-favorite-go-multiplexer

## Libs
- Errors dump for Go : [Trash](https://github.com/go-zoo/trash)
- Middleware Chaining module : [Claw](https://github.com/go-zoo/claw)

Imported by 104 package(s)

  1. github.com/1851616111/go-http-routing-benchmark
  2. github.com/AchievementNetwork/go-util/boneful
  3. github.com/AchievementNetwork/static
  4. github.com/CloudyKit/go-http-routing-benchmark
  5. github.com/JessonChan/go-http-routing-benchmark
  6. github.com/JulienSchmidt/go-http-routing-benchmark
  7. github.com/MatejLach/hoverfly
  8. github.com/ObjectIsAdvantag/hoverfly
  9. github.com/SpectoLabs/hoverfly/core
  10. github.com/SpectoLabs/hoverfly/core/handlers
  11. github.com/SpectoLabs/hoverfly/core/handlers/v1
  12. github.com/SpectoLabs/hoverfly/core/handlers/v2
  13. github.com/SpectoLabs/twitter-app
  14. github.com/ThatsMrTalbot/example
  15. github.com/TheHippo/go-http-routing-benchmark
  16. github.com/ValleyCamp/WeatherMoss
  17. github.com/Xuyuanp/go-http-routing-benchmark
  18. github.com/achiku/sample-golang-bone-context
  19. github.com/achiku/sample-golang-logging
  20. github.com/achiku/sample-golang-newrelic
  21. github.com/achiku/sample-golang-xhandler
  22. github.com/ant0ine/go-http-routing-benchmark
  23. github.com/beauhoyt/go-http-routing-benchmark
  24. github.com/besser/go-http-routing-benchmark
  25. github.com/betacraft/deferclient/deferstats
  26. github.com/betacraft/lemonade
  27. github.com/betacraft/lemonade/controllers
  28. github.com/betacraft/lemonade/interceptors
  29. github.com/bfitzsimmons/go-http-routing-benchmark
  30. github.com/bspaans/hoverfly
  31. github.com/chubais/go-http-routing-benchmark
  32. github.com/citysir/go-http-routing-benchmark
  33. github.com/coderhaoxin/chai
  34. github.com/cvvs/badb
  35. github.com/dalent/go-http-routing-benchmark
  36. github.com/daniel-bryant-uk/hoverfly
  37. github.com/dimiro1/experiments/bone
  38. github.com/dinever/go-http-routing-benchmark
  39. github.com/djinn/go-http-routing-benchmark
  40. github.com/generationtux/brizo/app/handlers/api
  41. github.com/generationtux/brizo/app/routes
  42. github.com/gerryyang/go-http-routing-benchmark
  43. github.com/gin-gonic/go-http-routing-benchmark
  44. github.com/go-gas/go-web-framework-benchmark
  45. github.com/go-zoo/bone/example/001
  46. github.com/go-zoo/bone/example/002
  47. github.com/go-zoo/bone/example/003
  48. github.com/go-zoo/bone/example/004
  49. github.com/gofury/go-web-framework-benchmark
  50. github.com/gotokatsuya/go-http-routing-benchmark
  51. github.com/guerrero/blog
  52. github.com/guerrero/blog/controllers
  53. github.com/jbuberel/go-http-routing-benchmark
  54. github.com/joeybloggs/go-http-routing-benchmark
  55. github.com/julienschmidt/go-http-routing-benchmark
  56. github.com/l3pp4rd/go-http-routing-benchmark
  57. github.com/liu-rui/go-http-routing-benchmark
  58. github.com/liu-rui/go-web-framework-benchmark
  59. github.com/lunny/go-http-routing-benchmark
  60. github.com/manifoldco/torus-cli/daemon/routes
  61. github.com/manifoldco/torus-cli/daemon/socket
  62. github.com/matthewdu/powerplug
  63. github.com/matthewdu/rule110-go
  64. github.com/mikespook/go-http-routing-benchmark
  65. github.com/mozey/hoverfly
  66. github.com/najeira/go-frameworks-benchmark
  67. github.com/pegasuskim/go-http-routing-benchmark
  68. github.com/pkg4go/chai
  69. github.com/pkieltyka/go-http-routing-benchmark
  70. github.com/qiangxue/go-http-routing-benchmark
  71. github.com/ricorx7/go-vault
  72. github.com/rusenask/bikend
  73. github.com/rusenask/genproxy
  74. github.com/rusenask/hoverfly
  75. github.com/rusenask/lgc
  76. github.com/rusenask/overseer
  77. github.com/safeie/go-http-routing-benchmark
  78. github.com/shivakar/go-http-routing-benchmark
  79. github.com/smallnest/go-web-framework-benchmark
  80. github.com/solher/auth-nginx-proxy-companion/app
  81. github.com/solher/auth-nginx-proxy-companion/infrastructure
  82. github.com/solher/kit-gateway
  83. github.com/solher/kit-gateway/library
  84. github.com/solher/zest
  85. github.com/squiidz/bench
  86. github.com/squiidz/fishMe
  87. github.com/squiidz/mtlsoft
  88. github.com/squiidz/spammer
  89. github.com/squiidz/spoon
  90. github.com/squiidz/spoon/api
  91. github.com/suifengRock/go-http-routing-benchmark
  92. github.com/sunisdown/go-http-routing-benchmark
  93. github.com/taka-wang/mb-web/route
  94. github.com/theckman/go-http-routing-benchmark
  95. github.com/twanies/go-http-routing-benchmark
  96. github.com/ursiform/go-http-routing-benchmark
  97. github.com/valleycamp/WeatherMoss
  98. github.com/vanng822/go-http-routing-benchmark
  99. github.com/viniciusfeitosa/GoInRIO
  100. github.com/viniciusfeitosa/GoInRIO/handlers
  101. github.com/viniciusfeitosa/GoInRio/handlers
  102. github.com/wenkesj/go-server
  103. github.com/wenkesj/go-server/httpserver
  104. github.com/yudppp/go-frameworks-benchmark

Imported only in test by 3 package(s)

  1. github.com/taka-wang/mb-socket/worker
  2. github.com/taka-wang/mb-web/test
  3. github.com/taka-wang/mb-web/worker