{"id":17261326,"url":"https://github.com/takama/router","last_synced_at":"2025-04-14T06:50:54.614Z","repository":{"id":19819764,"uuid":"23080554","full_name":"takama/router","owner":"takama","description":"A simple, compact and fast router package to process HTTP requests","archived":false,"fork":false,"pushed_at":"2021-05-05T18:59:34.000Z","size":75,"stargazers_count":104,"open_issues_count":1,"forks_count":10,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-27T20:39:41.469Z","etag":null,"topics":["go","golang","http","json","router"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/takama.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-18T17:45:31.000Z","updated_at":"2024-08-07T16:43:50.000Z","dependencies_parsed_at":"2022-08-25T20:00:22.500Z","dependency_job_id":null,"html_url":"https://github.com/takama/router","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takama%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/takama","download_url":"https://codeload.github.com/takama/router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248837274,"owners_count":21169373,"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","json","router"],"created_at":"2024-10-15T07:50:47.182Z","updated_at":"2025-04-14T06:50:54.592Z","avatar_url":"https://github.com/takama.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Go Router\n=========\n\n[![Build Status](https://travis-ci.org/takama/router.svg?branch=master)](https://travis-ci.org/takama/router)\n[![GoDoc](https://godoc.org/github.com/takama/router?status.svg)](https://godoc.org/github.com/takama/router)\n[![Contributions Welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/takama/router/issues)\n[![Go Report Card](https://goreportcard.com/badge/github.com/takama/router)](https://goreportcard.com/report/github.com/takama/router)\n[![codecov.io](https://codecov.io/github/takama/router/coverage.svg?branch=master)](https://codecov.io/github/takama/router?branch=master)\n\nA simple, compact and fast router package to process HTTP requests.\nIt has some sugar from framework however still lightweight. The router package is useful to prepare a RESTful API for Go services. It has JSON output, which bind automatically for relevant type of data. The router has timer feature to display duration of request handling in the header  \n\n### Examples\n\n- Simplest example (serve static route): \n```go\npackage main\n\nimport (\n\t\"github.com/takama/router\"\n)\n\nfunc Hello(c *router.Control) {\n\tc.Body(\"Hello world\")\n}\n\nfunc main() {\n\tr := router.New()\n\tr.GET(\"/hello\", Hello)\n\n\t// Listen and serve on 0.0.0.0:8888\n\tr.Listen(\":8888\")\n}\n```\n\n- Check it:\n```sh\ncurl -i http://localhost:8888/hello/\n\nHTTP/1.1 200 OK\nContent-Type: text/plain\nDate: Sun, 17 Aug 2014 13:25:50 GMT\nContent-Length: 11\n\nHello world\n```\n\n- Serve dynamic route with parameter:\n```go\npackage main\n\nimport (\n\t\"github.com/takama/router\"\n)\n\nfunc main() {\n\tr := router.New()\n\tr.GET(\"/hello/:name\", func(c *router.Control) {\n\t\tc.Body(\"Hello \" + c.Get(\":name\"))\n\t})\n\n\t// Listen and serve on 0.0.0.0:8888\n\tr.Listen(\":8888\")\n}\n```\n\n- Check it:\n```sh\ncurl -i http://localhost:8888/hello/John\n\nHTTP/1.1 200 OK\nContent-Type: text/plain\nDate: Sun, 17 Aug 2014 13:25:55 GMT\nContent-Length: 10\n\nHello John\n```\n\n- Checks JSON Content-Type automatically:\n```go\npackage main\n\nimport (\n\t\"github.com/takama/router\"\n)\n\n// Data is helper to construct JSON\ntype Data map[string]interface{}\n\nfunc main() {\n\tr := router.New()\n\tr.GET(\"/api/v1/settings/database/:db\", func(c *router.Control) {\n\t\tdata := Data{\n\t\t\t\"Database settings\": Data{\n\t\t\t\t\"database\": c.Get(\":db\"),\n\t\t\t\t\"host\":     \"localhost\",\n\t\t\t\t\"port\":     \"3306\",\n\t\t\t},\n\t\t}\n\t\tc.Code(200).Body(data)\n\t})\n\t// Listen and serve on 0.0.0.0:8888\n\tr.Listen(\":8888\")\n}\n```\n\n- Check it:\n```sh\ncurl -i http://localhost:8888/api/v1/settings/database/testdb\n\nHTTP/1.1 200 OK\nContent-Type: application/json\nDate: Sun, 17 Aug 2014 13:25:58 GMT\nContent-Length: 102\n\n{\n  \"Database settings\": {\n    \"database\": \"testdb\",\n    \"host\": \"localhost\",\n    \"port\": \"3306\"\n  }\n}\n```\n\n- Use timer to calculate duration of request handling:\n```go\npackage main\n\nimport (\n\t\"github.com/takama/router\"\n)\n\n// Data is helper to construct JSON\ntype Data map[string]interface{}\n\nfunc main() {\n\tr := router.New()\n\tr.GET(\"/api/v1/settings/database/:db\", func(c *router.Control) {\n\t\tc.UseTimer()\n\n\t\t// Do something\n\n\t\tdata := Data{\n\t\t\t\"Database settings\": Data{\n\t\t\t\t\"database\": c.Get(\":db\"),\n\t\t\t\t\"host\":     \"localhost\",\n\t\t\t\t\"port\":     \"3306\",\n\t\t\t},\n\t\t}\n\t\tc.Code(200).Body(data)\n\t})\n\t// Listen and serve on 0.0.0.0:8888\n\tr.Listen(\":8888\")\n}\n```\n\n- Check it:\n```sh\ncurl -i http://localhost:8888/api/v1/settings/database/testdb\n\nHTTP/1.1 200 OK\nContent-Type: application/json\nDate: Sun, 17 Aug 2014 13:26:05 GMT\nContent-Length: 143\n\n{\n  \"duration\": 5356123\n  \"took\": \"5.356ms\",\n  \"data\": {\n    \"Database settings\": {\n      \"database\": \"testdb\",\n      \"host\": \"localhost\",\n      \"port\": \"3306\"\n    }\n  }\n}\n```\n\n- Custom handler with \"Access-Control-Allow\" options and compact JSON:\n```go\npackage main\n\nimport (\n\t\"github.com/takama/router\"\n)\n\n// Data is helper to construct JSON\ntype Data map[string]interface{}\n\nfunc baseHandler(handle router.Handle) router.Handle {\n\treturn func(c *router.Control) {\n\t\tc.CompactJSON(true)\n\t\tif origin := c.Request.Header.Get(\"Origin\"); origin != \"\" {\n\t\t\tc.Writer.Header().Set(\"Access-Control-Allow-Origin\", origin)\n\t\t\tc.Writer.Header().Set(\"Access-Control-Allow-Credentials\", \"true\")\n\t\t}\n\t\thandle(c)\n\t}\n}\n\nfunc Info(c *router.Control) {\n\tdata := Data{\n\t\t\"debug\": true,\n\t\t\"error\": false,\n\t}\n\tc.Body(data)\n}\n\nfunc main() {\n\tr := router.New()\n\tr.CustomHandler = baseHandler\n\tr.GET(\"/info\", Info)\n\n\t// Listen and serve on 0.0.0.0:8888\n\tr.Listen(\":8888\")\n}\n```\n\n- Check it:\n```sh\ncurl -i -H 'Origin: http://foo.com' http://localhost:8888/info/\n\nHTTP/1.1 200 OK\nAccess-Control-Allow-Credentials: true\nAccess-Control-Allow-Origin: http://foo.com\nContent-Type: text/plain\nDate: Sun, 17 Aug 2014 13:27:10 GMT\nContent-Length: 28\n\n{\"debug\":true,\"error\":false}\n```\n\n- Use google json style `https://google.github.io/styleguide/jsoncstyleguide.xml`:\n```go\npackage main\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/takama/router\"\n)\n\nfunc main() {\n\tr := router.New()\n\tr.GET(\"/api/v1/people/:action/:id\", func(c *router.Control) {\n\n\t\t// Do something\n\n\t\tc.Method(\"people.\" + c.Get(\":action\"))\n\t\tc.SetParams(map[string]string{\"userId\": c.Get(\":id\")})\n\t\tc.SetError(http.StatusNotFound, \"UserId not found\")\n\t\tc.AddError(router.Error{Message: \"Group or User not found\"})\n\t\tc.Code(http.StatusNotFound).Body(nil)\n\t})\n\t// Listen and serve on 0.0.0.0:8888\n\tr.Listen(\":8888\")\n}\n```\n\n- Check it:\n```sh\ncurl -i http://localhost:8888/api/v1/people/get/@me\n\nHTTP/1.1 404 Not Found\nContent-Type: application/json\nDate: Sat, 22 Oct 2016 14:50:00 GMT\nContent-Length: 220\n\n{\n  \"method\": \"people.get\",\n  \"params\": {\n    \"userId\": \"@me\"\n  },\n  \"error\": {\n    \"code\": 404,\n    \"message\": \"UserId not found\",\n    \"errors\": [\n      {\n        \"message\": \"Group or User not found\"\n      }\n    ]\n  }\n}\n```\n\n## Contributors (unsorted)\n\n- [Igor Dolzhikov](https://github.com/takama)\n- [Yaroslav Lukyanov](https://github.com/CSharpRU)\n\nAll the contributors are welcome. If you would like to be the contributor please accept some rules.\n- The pull requests will be accepted only in \"develop\" branch\n- All modifications or additions should be tested\n- Sorry, I'll not accept code with any dependency, only standard library\n\nThank you for your understanding!\n\n## License\n\n[BSD License](https://github.com/takama/router/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakama%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftakama%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakama%2Frouter/lists"}