{"id":17912687,"url":"https://github.com/danielgatis/go-simplerouter","last_synced_at":"2025-04-03T07:13:59.854Z","repository":{"id":79824984,"uuid":"340817104","full_name":"danielgatis/go-simplerouter","owner":"danielgatis","description":"A simple request router for Go  in ~100 lines of code.","archived":false,"fork":false,"pushed_at":"2021-02-21T04:37:36.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-05-02T00:48:48.059Z","etag":null,"topics":["go","golang","handler","router"],"latest_commit_sha":null,"homepage":"","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/danielgatis.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-21T04:31:16.000Z","updated_at":"2024-06-19T09:00:44.768Z","dependencies_parsed_at":null,"dependency_job_id":"2eb12148-50d3-4d62-b6f4-c1640ae1cd50","html_url":"https://github.com/danielgatis/go-simplerouter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielgatis%2Fgo-simplerouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielgatis%2Fgo-simplerouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielgatis%2Fgo-simplerouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielgatis%2Fgo-simplerouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielgatis","download_url":"https://codeload.github.com/danielgatis/go-simplerouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246952278,"owners_count":20859812,"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","handler","router"],"created_at":"2024-10-28T19:46:33.985Z","updated_at":"2025-04-03T07:13:59.828Z","avatar_url":"https://github.com/danielgatis.png","language":"Go","funding_links":["https://www.buymeacoffee.com/danielgatis"],"categories":[],"sub_categories":[],"readme":"# go-simplerouter\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/danielgatis/go-simplerouter?style=flat-square)](https://goreportcard.com/report/github.com/danielgatis/go-simplerouter)\n[![License MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/danielgatis/go-simplerouter/master/LICENSE)\n[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/danielgatis/go-simplerouter)\n\nThis package is asimple request router for Go in ~100 lines of code.\n\n### How to use\n\n```bash\ngo get -u github.com/danielgatis/go-simplerouter\n```\n\nAnd then import the package in your code:\n\n```go\nimport \"github.com/danielgatis/go-simplerouter/simplerouter\"\n```\n\n#### CRUD Example\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\t\"time\"\n\n\t\"github.com/danielgatis/go-simplerouter/simplerouter\"\n\t\"github.com/spf13/cast\"\n)\n\nvar db map[int]*book = make(map[int]*book)\n\nfunc genID() int {\n\treturn int(time.Now().UnixNano() / int64(time.Millisecond))\n}\n\ntype book struct {\n\tID     int    `json:\"id\"`\n\tTitle  string `json:\"title\"`\n\tAuthor string `json:\"author\"`\n}\n\nfunc index(w http.ResponseWriter, r *http.Request) {\n\tbooks := make([]*book, 0)\n\n\tfor k := range db {\n\t\tbooks = append(books, db[k])\n\t}\n\n\tjson.NewEncoder(w).Encode(books)\n}\n\nfunc create(w http.ResponseWriter, r *http.Request) {\n\tvar newBook book\n\n\terr := json.NewDecoder(r.Body).Decode(\u0026newBook)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\thttp.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)\n\t\treturn\n\t}\n\n\tnewBook.ID = genID()\n\tdb[newBook.ID] = \u0026newBook\n\n\tw.WriteHeader(http.StatusCreated)\n\tjson.NewEncoder(w).Encode(newBook)\n}\n\nfunc show(w http.ResponseWriter, r *http.Request) {\n\tparamID, ok := simplerouter.GetParam(r, \"id\")\n\tif !ok {\n\t\thttp.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tid := cast.ToInt(paramID)\n\tbook, ok := db[id]\n\tif !ok {\n\t\thttp.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)\n\t\treturn\n\t}\n\n\tjson.NewEncoder(w).Encode(book)\n}\n\nfunc update(w http.ResponseWriter, r *http.Request) {\n\tparamID, ok := simplerouter.GetParam(r, \"id\")\n\tif !ok {\n\t\thttp.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tid := cast.ToInt(paramID)\n\toldBook, ok := db[id]\n\tif !ok {\n\t\thttp.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)\n\t\treturn\n\t}\n\n\tvar newBook book\n\terr := json.NewDecoder(r.Body).Decode(\u0026newBook)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\thttp.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)\n\t\treturn\n\t}\n\n\toldBook.Title = newBook.Title\n\toldBook.Author = newBook.Author\n\n\tjson.NewEncoder(w).Encode(oldBook)\n}\n\nfunc destroy(w http.ResponseWriter, r *http.Request) {\n\tparamID, ok := simplerouter.GetParam(r, \"id\")\n\tif !ok {\n\t\thttp.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)\n\t\treturn\n\t}\n\n\tid := cast.ToInt(paramID)\n\tdelete(db, id)\n\tw.WriteHeader(http.StatusNoContent)\n}\n\nfunc main() {\n\trouter := simplerouter.New()\n\trouter.Get(`/books`, index)\n\trouter.Post(`/books`, create)\n\trouter.Get(`/books/(?P\u003cid\u003e\\d+)`, show)\n\trouter.Put(`/books/(?P\u003cid\u003e\\d+)`, update)\n\trouter.Delete(`/books/(?P\u003cid\u003e\\d+)`, destroy)\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", router))\n}\n\n```\n\n#### Middleware Example\n\n```go\npackage main\n\nimport (\n\t\"io\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/danielgatis/go-simplerouter/simplerouter\"\n\t\"github.com/justinas/alice\"\n)\n\nfunc use(chain alice.Chain, fn http.HandlerFunc) http.HandlerFunc {\n\treturn chain.ThenFunc(fn).ServeHTTP\n}\n\nfunc logMiddleware(h http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tlog.Println(\"log middleware\")\n\t\th.ServeHTTP(w, r)\n\t})\n}\n\nfunc authMiddleware(h http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tlog.Println(\"auth middleware\")\n\t\th.ServeHTTP(w, r)\n\t})\n}\n\nfunc index(w http.ResponseWriter, r *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n\tio.WriteString(w, \"index\")\n}\n\nfunc home(w http.ResponseWriter, r *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n\tio.WriteString(w, \"home\")\n}\n\nfunc main() {\n\tpublic := alice.New(logMiddleware)\n\tprivate := alice.New(logMiddleware, authMiddleware)\n\n\trouter := simplerouter.New()\n\trouter.Get(`/`, use(public, index))\n\trouter.Get(`/home`, use(private, home))\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", router))\n}\n```\n\n#### Subdomain Example\n\n```go\npackage main\n\nimport (\n\t\"io\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/danielgatis/go-simplerouter/simplerouter\"\n)\n\ntype Routres map[string]http.Handler\n\nfunc (routers Routres) ServeHTTP(w http.ResponseWriter, r *http.Request) {\n\tif handler := routers[r.Host]; handler != nil {\n\t\thandler.ServeHTTP(w, r)\n\t} else {\n\t\thttp.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)\n\t}\n}\n\nfunc index1(w http.ResponseWriter, r *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n\tio.WriteString(w, \"index1\")\n}\n\nfunc index2(w http.ResponseWriter, r *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n\tio.WriteString(w, \"index2\")\n}\n\nfunc main() {\n\trouter1 := simplerouter.New()\n\trouter1.Get(`/`, index1)\n\n\trouter2 := simplerouter.New()\n\trouter2.Get(`/`, index2)\n\n\trouters := make(Routres)\n\trouters[\"site1.localhost:8080\"] = router1\n\trouters[\"site2.localhost:8080\"] = router2\n\n\tlog.Fatal(http.ListenAndServe(\":8080\", routers))\n}\n```\n\n### License\n\nCopyright (c) 2021-present [Daniel Gatis](https://github.com/danielgatis)\n\nLicensed under [MIT License](./LICENSE)\n\n### Buy me a coffee\nLiked some of my work? Buy me a coffee (or more likely a beer)\n\n\u003ca href=\"https://www.buymeacoffee.com/danielgatis\" target=\"_blank\"\u003e\u003cimg src=\"https://bmc-cdn.nyc3.digitaloceanspaces.com/BMC-button-images/custom_images/orange_img.png\" alt=\"Buy Me A Coffee\" style=\"height: auto !important;width: auto !important;\"\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielgatis%2Fgo-simplerouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielgatis%2Fgo-simplerouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielgatis%2Fgo-simplerouter/lists"}