All Projects → nissy → bon

nissy / bon

Licence: MIT license
Go http router

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to bon

bunrouter
Golang HTTP router
Stars: ✭ 533 (+3453.33%)
Mutual labels:  http-router
up10.me
up10.me file storage site which only store data given data one day
Stars: ✭ 22 (+46.67%)
Mutual labels:  appengine-go
goblin
A golang http router based on trie tree.
Stars: ✭ 52 (+246.67%)
Mutual labels:  http-router
tinyrouter
An ~O(2k) time complexity http request router in Go
Stars: ✭ 38 (+153.33%)
Mutual labels:  http-router
fastify-gateway
A Node.js API gateway that just works!
Stars: ✭ 88 (+486.67%)
Mutual labels:  http-router
Lightweight-PHP-Framework-For-Web-and-APIs
Simple PHP framework that helps you quickly understand and write simple APIs.
Stars: ✭ 24 (+60%)
Mutual labels:  http-router
rux
⚡ Rux is an simple and fast web framework. support route group, param route binding, middleware, compatible http.Handler interface. 简单且快速的 Go api/web 框架,支持路由分组,路由参数绑定,中间件,兼容 http.Handler 接口
Stars: ✭ 81 (+440%)
Mutual labels:  http-router
aelog
App Engine Logger
Stars: ✭ 24 (+60%)
Mutual labels:  appengine-go
gae-postgres
Connect to Cloud SQL for PostgreSQL from Google App Engine
Stars: ✭ 23 (+53.33%)
Mutual labels:  appengine-go
url2epub
Create ePub files from URLs
Stars: ✭ 35 (+133.33%)
Mutual labels:  appengine-go

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, " + 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
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].