{"id":16812542,"url":"https://github.com/deluan/rest","last_synced_at":"2025-03-22T03:31:08.709Z","repository":{"id":137882367,"uuid":"128538675","full_name":"deluan/rest","owner":"deluan","description":"Simple Generic REST controller","archived":false,"fork":false,"pushed_at":"2023-12-18T00:10:52.000Z","size":53,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T07:23:31.150Z","etag":null,"topics":["admin-on-rest","json-server","react-admin","rest-api","restful-api","simple-restapi"],"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/deluan.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":"2018-04-07T14:56:10.000Z","updated_at":"2023-05-10T08:20:05.000Z","dependencies_parsed_at":"2024-06-19T03:55:55.669Z","dependency_job_id":null,"html_url":"https://github.com/deluan/rest","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/deluan%2Frest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deluan%2Frest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deluan%2Frest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deluan%2Frest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deluan","download_url":"https://codeload.github.com/deluan/rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244902929,"owners_count":20529114,"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":["admin-on-rest","json-server","react-admin","rest-api","restful-api","simple-restapi"],"created_at":"2024-10-13T10:22:17.240Z","updated_at":"2025-03-22T03:31:08.128Z","avatar_url":"https://github.com/deluan.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Simple Generic REST controller\n\n[![Build Status](https://github.com/deluan/rest/workflows/CI/badge.svg)](https://github.com/deluan/rest/actions)\n[![Go Report Card](https://goreportcard.com/badge/github.com/deluan/rest)](https://goreportcard.com/report/github.com/deluan/rest)\n[![GoDoc](https://godoc.org/github.com/deluan/rest?status.svg)](https://godoc.org/github.com/deluan/rest)\n[![Maintainability](https://api.codeclimate.com/v1/badges/6837751f9b4305e56843/maintainability)](https://codeclimate.com/github/deluan/rest/maintainability)\n[![Coverage](http://gocover.io/_badge/github.com/deluan/rest)](http://gocover.io/github.com/deluan/rest)\n\nThis package provides a simple REST controller compatible with the [JSON Server API](https://github.com/typicode/json-server)\n\"dialect\". This package enables the creation of backends for the great [React-admin](https://marmelab.com/react-admin/)\nframework using pure Go, but can be used in other scenarios where you need a simple REST server for your data.\n\nTo use it, you will need to provide an implementation of the Repository interface and a function to create\nsuch repository (the constructor). For a simple implementation of an in-memory repository, see\n[`/examples/sample_repository.go`](https://github.com/deluan/rest/blob/master/examples/sample_repository.go).\n\nThe controller was created to be used with [Gorilla Pat](https://github.com/gorilla/pat), as it requires URL params to\nbe parsed and set as query params. You can easily adapt it to work with other routers and frameworks using a custom middleware.\n\nThe functionality is provided by a set of handlers named after the REST verbs they handle: `Get()`, `GetAll()`, `Put()`,\n`Post()` and `Delete()`. Each of these functions receive a constructor for your repository, and an optional\nimplementation of the Logger interface (compatible with [Logrus](https://github.com/sirupsen/logrus)). If no Logger is\nspecified, the functions falls back to the default Go log package.\n\nExample using [Gorilla Pat](https://github.com/gorilla/pat):\n\n```go\n\tfunc NewThingsRepository(ctx context) rest.Repository {\n\t\treturn \u0026ThingsRepository{ctx: ctx}\n\t}\n\n\tfunc main() {\n\t\trouter := pat.New()\n\n\t\trouter.Get(\"/thing/{id}\", rest.Get(NewThingsRepository))\n\t\trouter.Get(\"/thing\", rest.GetAll(NewThingsRepository))\n\t\trouter.Post(\"/thing\", rest.Post(NewThingsRepository))\n\t\trouter.Put(\"/thing/{id}\", rest.Put(NewThingsRepository))\n\t\trouter.Delete(\"/thing/{id}\", rest.Delete(NewThingsRepository))\n\n\t\thttp.Handle(\"/\", router)\n\n\t\tlog.Print(\"Listening on 127.0.0.1:8000...\")\n\t\tlog.Fatal(http.ListenAndServe(\":8000\", nil))\n\t}\n```\n\nExample using [chi router](https://github.com/go-chi/chi):\n\n```go\n\tfunc main() {\n\t\trouter := chi.NewRouter()\n\n\t\trouter.Route(\"/thing\", func(r chi.Router) {\n\t\t\tr.Get(\"/\", rest.GetAll(NewThingsRepository))\n\t\t\tr.Post(\"/\", rest.Post(NewThingsRepository))\n\t\t\tr.Route(\"/{id:[0-9]+}\", func(r chi.Router) {\n\t\t\t\tr.With(urlParams).Get(\"/\", rest.Get(NewThingsRepository))\n\t\t\t\tr.With(urlParams).Put(\"/\", rest.Put(NewThingsRepository))\n\t\t\t\tr.With(urlParams).Delete(\"/\", rest.Delete(NewThingsRepository))\n\t\t\t})\n\t\t})\n\n\t\thttp.Handle(\"/\", router)\n\n\t\tlog.Print(\"Listening on 127.0.0.1:8000...\")\n\t\tlog.Fatal(http.ListenAndServe(\":8000\", nil))\n\t}\n\n\t// Middleware to convert Chi URL params (from Context) to query params, as expected by our REST package\n\tfunc urlParams(next http.Handler) http.Handler {\n\t\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\t\tctx := chi.RouteContext(r.Context())\n\t\t\tparts := make([]string, 0)\n\t\t\tfor i, key := range ctx.URLParams.Keys {\n\t\t\t\tvalue := ctx.URLParams.Values[i]\n\t\t\t\tif key == \"*\" {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tparts = append(parts, url.QueryEscape(\":\"+key)+\"=\"+url.QueryEscape(value))\n\t\t\t}\n\t\t\tq := strings.Join(parts, \"\u0026\")\n\t\t\tif r.URL.RawQuery == \"\" {\n\t\t\t\tr.URL.RawQuery = q\n\t\t\t} else {\n\t\t\t\tr.URL.RawQuery += \"\u0026\" + q\n\t\t\t}\n\n\t\t\tnext.ServeHTTP(w, r)\n\t\t})\n\t}\n```\n\nAdd an [issue](https://github.com/deluan/rest/issues) if you need examples for other routers/frameworks\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeluan%2Frest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeluan%2Frest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeluan%2Frest/lists"}