{"id":15693102,"url":"https://github.com/code-hex/go-router-simple","last_synced_at":"2025-03-30T13:10:26.232Z","repository":{"id":57547667,"uuid":"302804400","full_name":"Code-Hex/go-router-simple","owner":"Code-Hex","description":"go-router-simple is Inspired by Router::Simple (Perl5)","archived":false,"fork":false,"pushed_at":"2020-10-11T16:07:08.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-05T14:42:14.731Z","etag":null,"topics":["flexibility","flexible","go","golang","hacktoberfest","hacktoberfest2020","http","router","simple"],"latest_commit_sha":null,"homepage":"https://github.com/tokuhirom/p5-router-simple","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/Code-Hex.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":"2020-10-10T03:14:34.000Z","updated_at":"2020-10-11T16:06:41.000Z","dependencies_parsed_at":"2022-09-26T18:41:00.019Z","dependency_job_id":null,"html_url":"https://github.com/Code-Hex/go-router-simple","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-router-simple","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-router-simple/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-router-simple/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Code-Hex%2Fgo-router-simple/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Code-Hex","download_url":"https://codeload.github.com/Code-Hex/go-router-simple/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246320192,"owners_count":20758410,"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":["flexibility","flexible","go","golang","hacktoberfest","hacktoberfest2020","http","router","simple"],"created_at":"2024-10-03T18:41:33.265Z","updated_at":"2025-03-30T13:10:26.192Z","avatar_url":"https://github.com/Code-Hex.png","language":"Go","readme":"go-router-simple\n=====\n![test](https://github.com/Code-Hex/go-router-simple/workflows/test/badge.svg) [![PkgGoDev](https://pkg.go.dev/badge/github.com/Code-Hex/go-router-simple)](https://pkg.go.dev/github.com/Code-Hex/go-router-simple) [![Go Report Card](https://goreportcard.com/badge/github.com/Code-Hex/go-router-simple)](https://goreportcard.com/report/github.com/Code-Hex/go-router-simple)\n\ngo-router-simple is a simple HTTP request router for [Go](https://golang.org/). This package is ported from [Router::Simple](https://metacpan.org/pod/Router::Simple) which is a great module in Perl.\n\n**Motivation:** Most request routing third-party modules in Go are implemented using the trie (radix) tree algorithm. Hence, they are fast but it hard to provide flexible URL parameters. (e.g. URL paths containing `/`)\n\n⚠️ **Supports Go1.12 and above.**\n\n## Synopsis\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\t\"strings\"\n\n\t\"github.com/Code-Hex/go-router-simple\"\n)\n\nfunc main() {\n\tr := router.New()\n\n\tr.GET(\"/\", http.HandlerFunc(Index))\n\tr.GET(\"/hi/:name\", http.HandlerFunc(Hi))\n\tr.GET(\"/hi/{name}\", http.HandlerFunc(Hi))\n\tr.GET(\"/download/*.*\", http.HandlerFunc(GetFilename))\n\tr.GET(`/blog/{year:\\d{4}}/{month:(?:\\d{2})}`, Blog())\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", r))\n}\n\nfunc Index(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprint(w, \"Index!\\n\")\n}\n\nfunc Hi(w http.ResponseWriter, r *http.Request) {\n\tname := router.ParamFromContext(r.Context(), \"name\")\n\tfmt.Fprintf(w, \"Welcome! %q\\n\", name)\n}\n\nfunc GetFilename(w http.ResponseWriter, r *http.Request) {\n\twildcards := router.WildcardsFromContext(r.Context())\n\tfmt.Fprintf(w, \"File: %q\\n\", strings.Join(wildcards, \".\"))\n}\n\nfunc Blog() http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tyear := router.ParamFromContext(r.Context(), \"year\")\n\t\tmonth := router.ParamFromContext(r.Context(), \"month\")\n\t\tfmt.Fprintf(w, \"Render: %q\\n\", year+\"/\"+month)\n\t})\n}\n```\n\n## Advanced usage\n\ngo-router-simple is used regular expressions for traversal but it's a little slow. So you would better to use other third-party routing packages for traversing most request paths whenever possible.\n\nHere's example. Basically, use httprouter, but use go-router-simple for complex URL parameters.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/Code-Hex/go-router-simple\"\n\t\"github.com/julienschmidt/httprouter\"\n)\n\nfunc main() {\n    // Setup go-router-simple.\n\tr := router.New()\n\tr.GET(`/blog/{year:(?:199\\d|20\\d{2})}/{month:(?:0?[1-9]|1[0-2])}`, Blog())\n\n\t// Setup httprouter.\n\thr := httprouter.New()\n\thr.GET(\"/hi/:name\", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {\n\t\tname := ps.ByName(\"name\")\n\t\tfmt.Fprintf(w, \"Welcome! %q\\n\", name)\n\t})\n\thr.GET(\"/bye/:name\", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {\n\t\tname := ps.ByName(\"name\")\n\t\tfmt.Fprintf(w, \"Bye! %q\\n\", name)\n\t})\n\n\t// If not found on httprouter, it will traverse on go-router-simple.\n\thr.NotFound = r\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", hr))\n}\n\nfunc Blog() http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tyear := router.ParamFromContext(r.Context(), \"year\")\n\t\tmonth := router.ParamFromContext(r.Context(), \"month\")\n\t\tfmt.Fprintf(w, \"Render: %q\\n\", year+\"/\"+month)\n\t})\n}\n```\n\n\u003cdetails\u003e\n\u003csummary\u003eResult\u003c/summary\u003e\n\n```\n$ curl localhost:8080/hi/codehex\nWelcome! \"codehex\"\n$ curl localhost:8080/bye/codehex\nBye! \"codehex\"\n$ curl localhost:8080/blog/2020/11\nRender: \"2020/11\"\n```\n\n\u003c/details\u003e","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-hex%2Fgo-router-simple","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcode-hex%2Fgo-router-simple","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcode-hex%2Fgo-router-simple/lists"}