{"id":16092353,"url":"https://github.com/alecthomas/rapid","last_synced_at":"2025-03-18T06:30:58.099Z","repository":{"id":18094682,"uuid":"21161197","full_name":"alecthomas/rapid","owner":"alecthomas","description":"RESTful API Daemons (and Clients) for Go","archived":false,"fork":false,"pushed_at":"2024-01-01T14:36:24.000Z","size":107,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-28T07:51:06.591Z","etag":null,"topics":[],"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/alecthomas.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":"2014-06-24T10:46:42.000Z","updated_at":"2019-06-28T05:38:05.000Z","dependencies_parsed_at":"2024-06-19T09:59:02.457Z","dependency_job_id":"7b8bb2b6-e877-48ec-b5d0-bfb83685c16c","html_url":"https://github.com/alecthomas/rapid","commit_stats":{"total_commits":57,"total_committers":1,"mean_commits":57.0,"dds":0.0,"last_synced_commit":"5fafe537aeaba68b6bd35b06dcd401e0bb244a18"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Frapid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Frapid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Frapid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Frapid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alecthomas","download_url":"https://codeload.github.com/alecthomas/rapid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243909879,"owners_count":20367535,"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":[],"created_at":"2024-10-09T16:07:19.844Z","updated_at":"2025-03-18T06:30:57.806Z","avatar_url":"https://github.com/alecthomas.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RESTful API Daemons (and Clients) for Go\n\nThis Go package provides facilities for building server-side RESTful APIs. An\nAPI is defined via a DSL in Go. This definition can be used to generate\n[RAML](http://raml.org) schemas and nicely idiomatic Go client code.\n\n## Example\n\nHere's an example of defining a basic user service:\n\n```go\ntype User struct {\n  ID int\n  Name string\n}\n\ntype IDPath struct {\n  ID string `schema:\"id\"`\n}\n\nusers := rapid.Define(\"Users\")\nusers.Route(\"ListUsers\").Get(\"/users\").Response(http.StatusOK, []*User{})\nusers.Route(\"GetUser\").Get(\"/users/{id}\").Path(\u0026IDPath{}).Response(http.StatusOK, \u0026User{})\nusers.Route(\"CreateUser\").Post(\"/users\").Request(http.StatusCreated, \u0026User{})\n```\n\nOnce your schema is defined you can create a service implementation. Each\nroute maps to a method on a service struct:\n\n```go\ntype UserService struct {\n  // ...\n}\n\nfunc (u *UserService) ListUsers() ([]*User, error) {\n  // Retrieve users\n  users := []*User{\u0026User{ID: 1, Name: \"Bob\"}, \u0026User{ID: 2, Name: \"Larry\"}}\n  return users, nil\n}\n\nfunc (u *UserService) CreateUser(user *User) error {\n  return rapid.ErrorForStatus(403)\n}\n\nfunc (u *UserService) GetUser(path *IDPath) (*User, error) {\n  return nil, rapid.ErrorForStatus(403)\n}\n```\n\nFinally, bind the service definition to the implementation:\n\n```go\nservice := \u0026UserService{}\nserver, err := rapid.NewServer(users, service)\nhttp.ListenAndServe(\":8080\", server)\n```\n\n## Encoding\n\nThe encoding, headers, etc. that different REST protocols use differs\nconsiderably. To cater for this, RAPID supports four interfaces for\nencoding/decoding requests and  responses:\n\n```go\n// Encoding and decoding requests on the client and server, respectively.\ntype RequestCodec interface {\n  // Encode request on client.\n  EncodeRequest() (headers http.Header, body io.ReadCloser, err error)\n  // Decode request.\n  DecodeRequest(r *http.Request) error\n}\n\n// Encoding and decoding responses on the server and client, respectively.\ntype ResponseCodec interface {\n  // Encode response into w. http.Request is included to allow Accept-based\n  // responses.\n  EncodeResponse(r *http.Request, w http.ResponseWriter, status int, err error) error\n  // Decode response from r.\n  DecodeResponse(r *http.Response) error\n}\n\ntype Codec interface {\n  RequestCodec\n  ResponseCodec\n}\n\ntype CodecFactory func(v interface{}) Codec\n```\n\nThe included implementation of `Codec` and\n`CodecFactory` supports a JSON-based API. This can be\ncompletely replaced by your own implementation (eg. encoding using Protocol\nBuffers, Avro, Thrift, etc.).\n\nAdditionally, individual types used in the definition of responses and\nrequests can implement these interfaces to override the default codec. This\ncan be seen in the included `rapid.FileDownload`, `rapid.Upload` and\n`rapid.RawBytes` types. `rapid.FileDownload`, for example, sets the\nappropriate `Content-Type` and `Content-Disposition: attachment` headers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Frapid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falecthomas%2Frapid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Frapid/lists"}