{"id":13549025,"url":"https://github.com/uptrace/bunrouter","last_synced_at":"2025-04-02T22:31:14.676Z","repository":{"id":37379414,"uuid":"254652602","full_name":"uptrace/bunrouter","owner":"uptrace","description":"Golang HTTP router","archived":false,"fork":false,"pushed_at":"2025-03-19T11:29:19.000Z","size":335,"stargazers_count":733,"open_issues_count":17,"forks_count":38,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-19T12:26:58.222Z","etag":null,"topics":["go","golang","http-router","router"],"latest_commit_sha":null,"homepage":"https://bunrouter.uptrace.dev/guide/golang-router.html","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uptrace.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://uptrace.dev/sponsor"]}},"created_at":"2020-04-10T14:18:46.000Z","updated_at":"2025-03-19T11:28:54.000Z","dependencies_parsed_at":"2025-03-19T13:30:22.359Z","dependency_job_id":null,"html_url":"https://github.com/uptrace/bunrouter","commit_stats":{"total_commits":105,"total_committers":3,"mean_commits":35.0,"dds":0.01904761904761909,"last_synced_commit":"1709e1de85ddd98bc324a99652424c616dfd846c"},"previous_names":["vmihailenco/treemux"],"tags_count":92,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptrace%2Fbunrouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptrace%2Fbunrouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptrace%2Fbunrouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uptrace%2Fbunrouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uptrace","download_url":"https://codeload.github.com/uptrace/bunrouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246905101,"owners_count":20852812,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","http-router","router"],"created_at":"2024-08-01T12:01:17.392Z","updated_at":"2025-04-02T22:31:14.268Z","avatar_url":"https://github.com/uptrace.png","language":"Go","funding_links":["https://uptrace.dev/sponsor"],"categories":["开源类库","Go"],"sub_categories":["路由"],"readme":"# Fast and flexible HTTP router for Go\n\n[![build workflow](https://github.com/uptrace/bunrouter/actions/workflows/build.yml/badge.svg)](https://github.com/uptrace/bunrouter/actions)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/uptrace/bunrouter)](https://pkg.go.dev/github.com/uptrace/bunrouter)\n[![Documentation](https://img.shields.io/badge/bunrouter-documentation-informational)](https://bunrouter.uptrace.dev/)\n[![Chat](https://discordapp.com/api/guilds/752070105847955518/widget.png)](https://discord.gg/rWtp5Aj)\n\n\u003e BunRouter is brought to you by :star: [**uptrace/uptrace**](https://github.com/uptrace/uptrace).\n\u003e Uptrace is an open-source APM tool that supports distributed tracing, metrics, and logs. You can\n\u003e use it to monitor applications and set up automatic alerts to receive notifications via email,\n\u003e Slack, Telegram, and others. Star it as well!\n\n**TLDR** BunRouter is as fast as httprouter, but supports middlewares, routing rules priority, and\nerror handling.\n\nBunRouter is an extremely fast HTTP router for Go with unique combination of features:\n\n- [Middlewares](https://bunrouter.uptrace.dev/guide/golang-http-middlewares.html) allow to extract\n  common operations from HTTP handlers into reusable functions.\n- [Error handling](https://bunrouter.uptrace.dev/guide/golang-http-error-handling.html) allows to\n  further reduce the size of HTTP handlers by handling errors in middlewares.\n- [Routes priority](https://bunrouter.uptrace.dev/guide/golang-router.html#routes-priority) enables\n  meaningful matching priority for routing rules: first static nodes, then named nodes, lastly\n  wildcard nodes.\n- net/http compatible API which means using minimal API without constructing huge wrappers that try\n  to do everything: from serving static files to XML generation (for example, `gin.Context` or\n  `echo.Context`).\n\n| Router          | Middlewares        | Error handling     | Routes priority    | net/http API       |\n| --------------- | ------------------ | ------------------ | ------------------ | ------------------ |\n| BunRouter       | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |\n| [httprouter][1] | :x:                | :x:                | :x:                | :heavy_check_mark: |\n| [Chi][2]        | :heavy_check_mark: | :x:                | :heavy_check_mark: | :heavy_check_mark: |\n| [Echo][3]       | :heavy_check_mark: | :heavy_check_mark: | :x:                | :x:                |\n| [Gin][4]        | :heavy_check_mark: | :heavy_check_mark: | :x:                | :x:                |\n\n[1]: https://github.com/julienschmidt/httprouter\n[2]: https://github.com/go-chi/chi\n[3]: https://github.com/labstack/echo\n[4]: https://github.com/go-gin/gin\n\nLearn:\n\n- [Documentation](https://bunrouter.uptrace.dev/)\n- [Reference](https://pkg.go.dev/github.com/uptrace/bunrouter)\n\nExamples:\n\n- [Basic example](/example/basic/)\n- [http.HandlerFunc example](/example/basic-compat/)\n- [httprouter.Handle example](/example/basic-verbose/)\n- [CORS example](/example/cors/)\n- [Basic auth example](/example/basicauth/)\n\nProjects using BunRouter:\n\n- [Distributed tracing tool](https://github.com/uptrace/uptrace)\n- [input-output-hk/cicero](https://github.com/input-output-hk/cicero)\n- [RealWorld example application](https://github.com/go-bun/bun-realworld-app)\n\nBenchmarks:\n\n- [web-frameworks-benchmark](https://web-frameworks-benchmark.netlify.app/result?l=go)\n- [go-http-routing-benchmark](https://github.com/go-bun/go-http-routing-benchmark)\n\n\u003cdetails\u003e\n  \u003csummary\u003eBenchmark results\u003c/summary\u003e\n\n```\nBenchmarkGin_Param               \t16019718\t        74.16 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_Param        \t12560001\t        95.04 ns/op\t      32 B/op\t       1 allocs/op\nBenchmarkBunrouter_Param         \t50015306\t        23.81 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_Param5              \t 8997234\t       131.5 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_Param5       \t 4809441\t       261.3 ns/op\t     160 B/op\t       1 allocs/op\nBenchmarkBunrouter_Param5        \t10789635\t       114.0 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_Param20             \t 3953041\t       302.4 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_Param20      \t 1661373\t       743.3 ns/op\t     640 B/op\t       1 allocs/op\nBenchmarkBunrouter_Param20       \t 2462354\t       482.8 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_ParamWrite          \t 9258986\t       128.0 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_ParamWrite   \t 9908178\t       123.0 ns/op\t      32 B/op\t       1 allocs/op\nBenchmarkBunrouter_ParamWrite    \t15511226\t        70.62 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_GithubStatic        \t12781513\t        94.17 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_GithubStatic \t30077443\t        37.36 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkBunrouter_GithubStatic  \t37160334\t        32.41 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_GithubParam         \t 6971791\t       169.2 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_GithubParam  \t 5464755\t       217.4 ns/op\t      96 B/op\t       1 allocs/op\nBenchmarkBunrouter_GithubParam   \t12047902\t       101.2 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_GithubAll           \t   32758\t     37382 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_GithubAll    \t   27324\t     43932 ns/op\t   13792 B/op\t     167 allocs/op\nBenchmarkBunrouter_GithubAll     \t   57910\t     20914 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_GPlusStatic         \t17788194\t        69.13 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_GPlusStatic  \t60191341\t        19.84 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkBunrouter_GPlusStatic   \t87114368\t        14.06 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_GPlusParam          \t10075399\t       119.5 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_GPlusParam   \t 8272046\t       149.2 ns/op\t      64 B/op\t       1 allocs/op\nBenchmarkBunrouter_GPlusParam    \t37359979\t        32.43 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_GPlus2Params        \t 7375279\t       162.9 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_GPlus2Params \t 6538942\t       186.7 ns/op\t      64 B/op\t       1 allocs/op\nBenchmarkBunrouter_GPlus2Params  \t19681939\t        61.51 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_GPlusAll            \t  647716\t      1752 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_GPlusAll     \t  590356\t      2085 ns/op\t     640 B/op\t      11 allocs/op\nBenchmarkBunrouter_GPlusAll      \t 1685287\t       712.8 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_ParseStatic         \t14566458\t        76.58 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_ParseStatic  \t52994076\t        21.02 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkBunrouter_ParseStatic   \t50583933\t        23.83 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_ParseParam          \t13443874\t        90.66 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_ParseParam   \t 8825664\t       135.6 ns/op\t      64 B/op\t       1 allocs/op\nBenchmarkBunrouter_ParseParam    \t38058278\t        31.33 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_Parse2Params        \t10179813\t       118.1 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_Parse2Params \t 7801735\t       152.9 ns/op\t      64 B/op\t       1 allocs/op\nBenchmarkBunrouter_Parse2Params  \t23704574\t        50.78 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_ParseAll            \t  394884\t      3073 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_ParseAll     \t  410238\t      3011 ns/op\t     640 B/op\t      16 allocs/op\nBenchmarkBunrouter_ParseAll      \t  810908\t      1487 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkGin_StaticAll           \t   50658\t     23699 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkHttpRouter_StaticAll    \t  105313\t     11518 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkBunrouter_StaticAll     \t   99674\t     12188 ns/op\t       0 B/op\t       0 allocs/op\n```\n\n\u003c/details\u003e\n\n## Quickstart\n\nInstall:\n\n```shell\ngo get github.com/uptrace/bunrouter\n```\n\nRun the [example](/example/basic/):\n\n```go\npackage main\n\nimport (\n\t\"html/template\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/uptrace/bunrouter\"\n\t\"github.com/uptrace/bunrouter/extra/reqlog\"\n)\n\nfunc main() {\n\trouter := bunrouter.New(\n\t\tbunrouter.Use(reqlog.NewMiddleware()),\n\t)\n\n\trouter.GET(\"/\", indexHandler)\n\n\trouter.WithGroup(\"/api\", func(g *bunrouter.Group) {\n\t\tg.GET(\"/users/:id\", debugHandler)\n\t\tg.GET(\"/users/current\", debugHandler)\n\t\tg.GET(\"/users/*path\", debugHandler)\n\t})\n\n\tlog.Println(\"listening on http://localhost:9999\")\n\tlog.Println(http.ListenAndServe(\":9999\", router))\n}\n\nfunc indexHandler(w http.ResponseWriter, req bunrouter.Request) error {\n\treturn indexTemplate().Execute(w, nil)\n}\n\nfunc debugHandler(w http.ResponseWriter, req bunrouter.Request) error {\n\treturn bunrouter.JSON(w, bunrouter.H{\n\t\t\"route\":  req.Route(),\n\t\t\"params\": req.Params().Map(),\n\t})\n}\n\nvar indexTmpl = `\n\u003chtml\u003e\n  \u003ch1\u003eWelcome\u003c/h1\u003e\n  \u003cul\u003e\n    \u003cli\u003e\u003ca href=\"/api/users/123\"\u003e/api/users/123\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/api/users/current\"\u003e/api/users/current\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"/api/users/foo/bar\"\u003e/api/users/foo/bar\u003c/a\u003e\u003c/li\u003e\n  \u003c/ul\u003e\n\u003c/html\u003e\n`\n\nfunc indexTemplate() *template.Template {\n\treturn template.Must(template.New(\"index\").Parse(indexTmpl))\n}\n```\n\nSee the [Golang Router documentation](https://bunrouter.uptrace.dev/) for details.\n\n## See also\n\n- [Golang ORM](https://bun.uptrace.dev/)\n- [Golang msgpack](https://msgpack.uptrace.dev/)\n- [Golang message task queue](https://github.com/vmihailenco/taskq)\n- [Distributed tracing tools ](https://get.uptrace.dev/compare/distributed-tracing-tools.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptrace%2Fbunrouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuptrace%2Fbunrouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuptrace%2Fbunrouter/lists"}