go>> bon>> 返回
项目作者: nissy

项目描述 :
Go http router
高级语言: Go
项目地址: git://github.com/nissy/bon.git
创建时间: 2017-02-23T13:52:08Z
项目社区:https://github.com/nissy/bon

开源协议:MIT License

下载


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
    • /*
  1. package main
  2. import (
  3. "net/http"
  4. "github.com/nissy/bon"
  5. )
  6. func main() {
  7. r := bon.NewRouter()
  8. r.Get("/users/taro", func(w http.ResponseWriter, r *http.Request) {
  9. w.Write([]byte("static"))
  10. })
  11. r.Get("/users/:name", func(w http.ResponseWriter, r *http.Request) {
  12. w.Write([]byte("param name is " + bon.URLParam(r, "name")))
  13. })
  14. r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
  15. w.Write([]byte("any"))
  16. })
  17. http.ListenAndServe(":8080", r)
  18. }

Example

  • Group is inherits middleware and grants a prefix
  • Route is does not inherit middleware
  1. package main
  2. import (
  3. "net/http"
  4. "github.com/nissy/bon"
  5. "github.com/nissy/bon/middleware"
  6. )
  7. func main() {
  8. r := bon.NewRouter()
  9. v := r.Group("/v1")
  10. v.Use(
  11. middleware.CORS(middleware.AccessControlConfig{
  12. AllowOrigin: "*",
  13. AllowCredentials: false,
  14. AllowMethods: []string{
  15. http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions,
  16. },
  17. AllowHeaders: []string{
  18. "Authorization",
  19. },
  20. ExposeHeaders: []string{
  21. "link",
  22. },
  23. MaxAge: 86400,
  24. }),
  25. )
  26. v.Options("*", func(w http.ResponseWriter, r *http.Request) {
  27. w.WriteHeader(http.StatusNoContent)
  28. })
  29. v.Get("/users/:name", func(w http.ResponseWriter, r *http.Request) {
  30. w.Write([]byte("Hallo," + bon.URLParam(r, "name")))
  31. })
  32. admin := r.Group("/admin")
  33. admin.Use(
  34. middleware.BasicAuth([]middleware.BasicAuthUser{
  35. {
  36. Name: "name",
  37. Password: "password",
  38. },
  39. }),
  40. )
  41. admin.Get("/users/:name", func(w http.ResponseWriter, r *http.Request) {
  42. w.Write([]byte("Hallo, " + bon.URLParam(r, "name")))
  43. })
  44. http.ListenAndServe(":8080", r)
  45. }

Benchmarks

GitHub

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

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

Other http routers

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