{"id":23381134,"url":"https://github.com/dasfmi/garson","last_synced_at":"2025-04-10T23:05:36.528Z","repository":{"id":262617706,"uuid":"54530114","full_name":"dasfmi/garson","owner":"dasfmi","description":"a simple (mux) router for Go lang on top of net/http","archived":false,"fork":false,"pushed_at":"2019-07-24T15:45:48.000Z","size":30,"stargazers_count":8,"open_issues_count":9,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T23:05:29.163Z","etag":null,"topics":["go","golang","http","router","server"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dasfmi.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-23T04:10:34.000Z","updated_at":"2019-08-20T02:16:51.000Z","dependencies_parsed_at":"2024-11-13T11:40:52.165Z","dependency_job_id":"00b8161b-d2c1-44fe-a0c1-33a43f1f58f7","html_url":"https://github.com/dasfmi/garson","commit_stats":null,"previous_names":["dasfmi/garson"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasfmi%2Fgarson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasfmi%2Fgarson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasfmi%2Fgarson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dasfmi%2Fgarson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dasfmi","download_url":"https://codeload.github.com/dasfmi/garson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248312157,"owners_count":21082638,"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","server"],"created_at":"2024-12-21T20:39:51.049Z","updated_at":"2025-04-10T23:05:36.510Z","avatar_url":"https://github.com/dasfmi.png","language":"Go","readme":"# Garson\n\nSimple Go router lies on top of net/http package. created for learning purposes.\n\n[![GoDoc](https://godoc.org/github.com/schehata/garson?status.svg)](https://godoc.org/github.com/schehata/garson)\n\n[![Build Status](https://travis-ci.org/schehata/garson.svg?branch=master)](https://travis-ci.org/schehata/garson)\n\n#### Installation\n\nfrom your shell use \"go get\" command to install the package\n\n```bash\n go get github.com/schehata/garson\n```\n\n#### Usage\n\nGarson supports 4 http methods, \nfirst import garson and then initialize the router inside the main func,\n\n```go\nimport (\n    \"net/http\"\n    g \"github.com/schehata/garson\"\n)\n\n\nfunc main() {\n    router := g.New()\n    router.Get(\"/posts\", func(w http.ResponseWriter, r *http.Request){})\n    router.Post(\"/posts\", func(w http.ResponseWriter, r *http.Request){})\n    router.Put(\"/posts/:id\", func(w http.ResponseWriter, r *http.Request){})\n    router.Delete(\"/:posts/:id\", func(w http.ResponseWriter, r *http.Request){})\n\n    http.ListenAndServe(\":8080\", router)\n}\n```\n\n#### Example\n\n```go\nimport (\n    \"net/http\"\n    g \"github.com/schehata/garson\"\n)\n\n\nfunc main() {\n    router := g.New()\n\n    router.Get(\"/hello\", func(w http.ResponseWriter, r *http.Request){}\n        w.Write([]byte(\"Hello World\"))\n    })\n\n\n    http.ListenAndServe(\":8080\", router)\n}\n```\n\n#### Route Params\n\nyou can easily define params in route by appending a colon \":\" then a name,\nfor example :\n\n```go\nrouter.Get(\"/api/articles/:id\", handler)\n```\nGarson using go context package to store request parameters.\nthe requested route parameters are stored with a key named \"route_params\"\n\nThere is a function called \"GetParam()\" that makes it easier to get those parameters\n\n```go\nfunc someHandler(w Http.ResponseWriter, r *http.Request) {\n    id, ok := garson.GetParam(r, \"id\")\n    if ok != false {\n\t\tfmt.Println(id)\n    }\n    ...\n}\n```\n\nIf you prefer to use the context directly, you can access it through the value\nof \"route_params\" key.\n\n```go\nfunc someHandler(w Http.ResponseWriter, r *http.Request) {\n    ctx := r.Context()\n    if val := ctx.Value(\"route_params\"); val != nil {\n        params := val.(garson.Params)\n        fmt.Println(params[\"id\"])\n    }\n    ...\n}\n```\n\n\n## ViewSets\n\nGarson allows you to create ViewSets and register it to the router directly,\ne.g:\n\nPrepare your ViewSet, it's a struct that implements garson.ViewSet interface.\nThe ViewSet interface itself is built from another interfaces:\n - Indexer\n - Poster\n - Getter\n - Putter\n - Deleter\n\n You can implement all of them, or some of them, based on your needs.\n Garson will automatically created urls for the methods you have implemented.\n\n```go\n\ntype UserViewSet struct {}\n\nfunc (vs *UserViewSet) Index(w http.ResponseWriter, r *http.Request) {\n\tw.Write([]byte(\"Users\"))\n\n}\nfunc (vs *UserViewSet) Get(w http.ResponseWriter, r *http.Request) {\n\tw.Write([]byte(\"Just One User\"))\n}\nfunc (vs *UserViewSet) Post(w http.ResponseWriter, r *http.Request)         {}\n```\n\n```go\nfunc main() {\n    ...\n    vs := \u0026UserViewSet{}\n    router.ViewSet(\"/api/users\", vs)\n    ....\n}\n```\n\nThe Router will automatically register routes for this ViewSet as following:\n\n```\n    GET     /api/users          =\u003e vs.Index\n    POST    /api/users          =\u003e vs.Post\n    GET     /api/users/:id      =\u003e vs.Get\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdasfmi%2Fgarson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdasfmi%2Fgarson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdasfmi%2Fgarson/lists"}