{"id":17599375,"url":"https://github.com/jamesog/example-http-api","last_synced_at":"2025-03-29T21:12:32.069Z","repository":{"id":147037735,"uuid":"188593235","full_name":"jamesog/example-http-api","owner":"jamesog","description":"An example HTTP REST API in Go","archived":false,"fork":false,"pushed_at":"2024-03-26T12:09:32.000Z","size":25,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-04T21:47:28.967Z","etag":null,"topics":["example-code","go","http-server","json-api","rest-api"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jamesog.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":"2019-05-25T17:14:34.000Z","updated_at":"2023-03-07T18:02:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"3c7041aa-9a9f-4025-a38e-318d7fb121c9","html_url":"https://github.com/jamesog/example-http-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesog%2Fexample-http-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesog%2Fexample-http-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesog%2Fexample-http-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jamesog%2Fexample-http-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jamesog","download_url":"https://codeload.github.com/jamesog/example-http-api/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246243553,"owners_count":20746311,"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":["example-code","go","http-server","json-api","rest-api"],"created_at":"2024-10-22T10:25:14.259Z","updated_at":"2025-03-29T21:12:32.051Z","avatar_url":"https://github.com/jamesog.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Example HTTP API\n\nThis is a rough framework for how I've ended up writing JSON REST APIs in Go.\nI've put this together as I spent quite a long time trying to figure out how I should structure code, mostly between the frontend and backend packages. I wanted to have an abstract database access interface to allow creating lightweight unit tests for API handlers (meaning I wanted mock data and didn't want to require a full RDBMS) so the backend needed to be easily swappable.\n\nIt's structured as follows:\n\n```\napi/              The package implementing the HTTP endpoints\ncmd/\n    apiserver/    The binary running the HTTP server\ndatabase/         The package with types and database interface. This package could equally be extracted to its own repo\n    mock/         A mock backend returning sample data\n    postgres/     A PostgreSQL backend\n```\n\nI came up with the `Storage` interface in the `database` package\n\n```go\ntype Storage interface {\n    // Database access methods\n}\n```\n\nwhich each of the `mock` and `postgres` packages implement.\n\nThe `api` package creates a mux with its own routes, which are returned by the `Routes()` method. This allows versioning the API by \"mounting\" these routes in the API server at different paths.\n\nThe `apiserver` binary mounts these routes at `/` by default:\n\n```go\napisvc := api.NewService(db)\nr := chi.NewRouter()\nr.Mount(\"/\", apisvc.Routes())\n```\n\nSo the `/users` endpoint defined in the `api` package appears as `/users`. If you created a new version of the API you could do:\n\n```go\nimport (\n    apiv1 \"github.com/jamesog/example-http-api/api/v1\"\n    apiv2 \"github.com/jamesog/example-http-api/api/v2\"\n)\n\napiv1 := apiv1.NewService(db)\napiv2 := apiv2.NewService(db)\nr := chi.NewRouter()\nr.Mount(\"/\", apiv1.Routes())\nr.Mount(\"/v2\", apiv2.Routes())\n```\n\nThis would provide `/users` and `/v2/users`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesog%2Fexample-http-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjamesog%2Fexample-http-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjamesog%2Fexample-http-api/lists"}