{"id":37224567,"url":"https://github.com/samharju/snug","last_synced_at":"2026-01-15T01:42:23.036Z","repository":{"id":61628064,"uuid":"471518803","full_name":"samharju/snug","owner":"samharju","description":"Simple stdlib compatible router with shortcuts to dump json api things","archived":false,"fork":false,"pushed_at":"2022-10-12T19:00:58.000Z","size":13,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-06-20T00:42:22.477Z","etag":null,"topics":["go","golang","json-api","router"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/samharju/snug","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/samharju.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-03-18T21:14:58.000Z","updated_at":"2024-06-20T00:42:22.478Z","dependencies_parsed_at":"2022-10-19T20:15:11.042Z","dependency_job_id":null,"html_url":"https://github.com/samharju/snug","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/samharju/snug","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samharju%2Fsnug","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samharju%2Fsnug/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samharju%2Fsnug/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samharju%2Fsnug/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samharju","download_url":"https://codeload.github.com/samharju/snug/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samharju%2Fsnug/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28441031,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-15T00:55:22.719Z","status":"ssl_error","status_checked_at":"2026-01-15T00:55:20.945Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","json-api","router"],"created_at":"2026-01-15T01:42:22.369Z","updated_at":"2026-01-15T01:42:23.017Z","avatar_url":"https://github.com/samharju.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Snug Minimal HTTP JSON Api Framework\n\n*snug: fitting closely and comfortably: \"a snug coat\"* - [Merriam-Webster](https://www.merriam-webster.com/dictionary/snug)\n\nSnug is a simple router with the goal of having the bare minimum to put together a JSON web API.\n\nWriting small web apis with Go is barely a chore, it's a pleasure.\nRepeating the same few simple steps over and over again, I decided to put together this package\nmostly for quick prototyping. And just to build a router from scratch and do something with\n`reflect`.\n\nProvided features:\n- Router with minimal functionalities mimicking `http.ServeMux`\n- Some default error responses\n- Request body binding with `snug.Fit`\n- Logging\n- `snug.JSON` for dumping simple json responses to responsewriter\n\n\nExample with all the bells and whistles in place:\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/samharju/snug\"\n)\n\ntype server struct {\n\trouter *snug.Router\n\tdb     map[string]int\n}\n\nfunc main() {\n\tmux := snug.New()\n\n\t// init a server with router and a db\n\tsrv := server{\n\t\trouter: mux,\n\t\tdb: map[string]int{\n\t\t\t\"esa\": 25,\n\t\t},\n\t}\n\n\t// add logging\n\tmux.UseMiddleware(snug.Recover)\n\tmux.UseMiddleware(snug.Logging)\n\n\t// register handlers\n\tmux.Get(\"/greet\", srv.greetanon)\n\tmux.Post(\"/greet\", srv.postgreet)\n\tmux.Get(\"/greet/age/\u003cname\u003e\", srv.getAge)\n\n\tlog.Fatalln(http.ListenAndServe(\":8000\", mux))\n}\n\n// handle url parameter\nfunc (s server) getAge(w http.ResponseWriter, r *http.Request) {\n\tname := snug.Param(r, \"name\")\n\tage, ok := s.db[name]\n\n\tif !ok {\n\t\tsnug.JSON{\"error\": name + \" not found\"}.Write(w, 404)\n\t\treturn\n\t}\n\n\tsnug.JSON{\"age\": age}.Write(w, 200)\n\n}\n\n// endpoint with no input\nfunc (server) greetanon(w http.ResponseWriter, r *http.Request) {\n\tsnug.JSON{\"msg\": \"hello anon\"}.Write(w, 200)\n}\n\n// reading post request body\nfunc (server) postgreet(w http.ResponseWriter, r *http.Request) {\n\n\tvar req struct {\n\t\tName *string `json:\"name\" snug:\"required\"`\n\t\tAge  *int    `json:\"age\" snug:\"required\"`\n\t}\n\n\terr := snug.Fit(r.Body, \u0026req)\n\tif err != nil {\n\t\t// return 400 if missing required fields\n\t\tsnug.JSON{\"error\": err.Error()}.Write(w, 400)\n\t\treturn\n\t}\n\n\tsnug.JSON{\n\t\t\"msg\": fmt.Sprintf(\"hello %s %d years\", *req.Name, *req.Age),\n\t}.Write(w, 200)\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamharju%2Fsnug","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamharju%2Fsnug","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamharju%2Fsnug/lists"}