{"id":15012956,"url":"https://github.com/swaggest/rest","last_synced_at":"2025-05-15T11:06:04.477Z","repository":{"id":37092306,"uuid":"306110879","full_name":"swaggest/rest","owner":"swaggest","description":"Web services with OpenAPI and JSON Schema done quick in Go","archived":false,"fork":false,"pushed_at":"2025-04-22T15:30:54.000Z","size":6430,"stargazers_count":432,"open_issues_count":10,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-22T16:44:14.820Z","etag":null,"topics":["go","golang","hacktoberfest","json-schema","openapi","openapi3","rest-api","swagger"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/swaggest/rest","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/swaggest.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,"zenodo":null}},"created_at":"2020-10-21T18:16:16.000Z","updated_at":"2025-04-22T15:30:37.000Z","dependencies_parsed_at":"2023-02-16T02:00:44.392Z","dependency_job_id":"e1c22779-2237-4479-989e-ed2ba2c6f043","html_url":"https://github.com/swaggest/rest","commit_stats":{"total_commits":143,"total_committers":8,"mean_commits":17.875,"dds":"0.39160839160839156","last_synced_commit":"31256ddb5f464941f2e9c11bac580caacfb81e01"},"previous_names":[],"tags_count":97,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Frest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Frest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Frest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/swaggest%2Frest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/swaggest","download_url":"https://codeload.github.com/swaggest/rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254328385,"owners_count":22052632,"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","hacktoberfest","json-schema","openapi","openapi3","rest-api","swagger"],"created_at":"2024-09-24T19:43:30.927Z","updated_at":"2025-05-15T11:06:04.453Z","avatar_url":"https://github.com/swaggest.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# REST with Clean Architecture for Go\n\n[![Build Status](https://github.com/swaggest/rest/workflows/test-unit/badge.svg)](https://github.com/swaggest/rest/actions?query=branch%3Amaster+workflow%3Atest-unit)\n[![Coverage Status](https://codecov.io/gh/swaggest/rest/branch/master/graph/badge.svg)](https://codecov.io/gh/swaggest/rest)\n[![GoDevDoc](https://img.shields.io/badge/dev-doc-00ADD8?logo=go)](https://pkg.go.dev/github.com/swaggest/rest)\n[![Time Tracker](https://wakatime.com/badge/github/swaggest/rest.svg)](https://wakatime.com/badge/github/swaggest/rest)\n![Code lines](https://sloc.xyz/github/swaggest/rest/?category=code)\n![Comments](https://sloc.xyz/github/swaggest/rest/?category=comments)\n\nThis module implements HTTP transport level for [`github.com/swaggest/usecase`](https://github.com/swaggest/usecase) \nto build REST services.\n\n## Goals\n\n* Maintain single source of truth for documentation, validation and input/output of HTTP API.\n* Avoid dependency on compile time code generation.\n* Improve productivity and reliability by abstracting HTTP details with simple API for common case.\n* Allow low-level customizations for advanced cases.\n* Maintain reasonable performance with low GC impact.\n\n## Non-Goals\n\n* Support for legacy documentation schemas like Swagger 2.0 or RAML.\n* Zero allocations.\n* Explicit support for XML in request or response bodies.\n\n## Features\n\n* Compatible with `net/http`.\n* Built with [`github.com/go-chi/chi`](https://github.com/go-chi/chi) router.\n* Modular flexible structure.\n* HTTP [request mapping](#request-decoder) into Go value based on field tags.\n* Decoupled business logic with Clean Architecture use cases.\n* Automatic type-safe OpenAPI 3.0/3.1 documentation with [`github.com/swaggest/openapi-go`](https://github.com/swaggest/openapi-go).\n* Single source of truth for the documentation and endpoint interface.\n* Automatic request/response JSON schema validation with [`github.com/santhosh-tekuri/jsonschema`](https://github.com/santhosh-tekuri/jsonschema).\n* Dynamic gzip compression and fast pass through mode.\n* Optimized performance.\n* Embedded [Swagger UI](https://swagger.io/tools/swagger-ui/).\n* Generic interface for [use case interactors](https://pkg.go.dev/github.com/swaggest/usecase#NewInteractor). \n\n## Usage\n\nPlease check this [tutorial](https://dev.to/vearutop/tutorial-developing-a-restful-api-with-go-json-schema-validation-and-openapi-docs-2490) for end-to-end usage example.\n\n### Request Decoder\n\nGo struct with field tags defines input port. \nRequest decoder populates field values from `http.Request` data before use case interactor is invoked. \n\n```go\n// Declare input port type.\ntype helloInput struct {\n    Locale string `query:\"locale\" default:\"en-US\" pattern:\"^[a-z]{2}-[A-Z]{2}$\" enum:\"ru-RU,en-US\"`\n    Name   string `path:\"name\" minLength:\"3\"` // Field tags define parameter location and JSON schema constraints.\n\n    // Field tags of unnamed fields are applied to parent schema, \n\t// they are optional and can be used to disallow unknown parameters.\n    // For non-body params, name tag must be provided explicitly.\n    // E.g. here no unknown `query` and `cookie` parameters allowed,\n    // unknown `header` params are ok.\n    _ struct{} `query:\"_\" cookie:\"_\" additionalProperties:\"false\"`\n}\n```\n\nInput data can be located in:\n* `path` parameter in request URI, e.g. `/users/{name}`,\n* `query` parameter in request URI, e.g. `/users?locale=en-US`,\n* `formData` parameter in request body with `application/x-www-form-urlencoded` or `multipart/form-data` content,\n* `form` parameter acts as `formData` or `query`,\n* `json` parameter in request body with `application/json` content,\n* `cookie` parameter in request cookie,\n* `header` parameter in request header,\n* `contentType` of matching raw request body.\n\nFor more explicit separation of concerns between use case and transport it is possible to provide request mapping \nseparately when initializing handler (please note, such mapping is [not applied](https://github.com/swaggest/rest/issues/61#issuecomment-1059851553) to `json` body).\n\n```go\n// Declare input port type.\ntype helloInput struct {\n    Locale string `default:\"en-US\" pattern:\"^[a-z]{2}-[A-Z]{2}$\"`\n    Name   string `minLength:\"3\"` // Field tags define parameter location and JSON schema constraints.\n}\n```\n\n```go\n// Add use case handler with custom input mapping to router.\nr.Method(http.MethodGet, \"/hello/{name}\", nethttp.NewHandler(u,\n    nethttp.RequestMapping(new(struct {\n       Locale string `query:\"locale\"`\n       Name   string `path:\"name\"` // Field tags define parameter location and JSON schema constraints.\n    })),\n))\n```\n\nAdditional field tags describe JSON schema constraints, please check \n[documentation](https://pkg.go.dev/github.com/swaggest/jsonschema-go#Reflector.Reflect).\n\nMore schema customizations are possible with [`github.com/swaggest/jsonschema-go interfaces`](https://github.com/swaggest/jsonschema-go#implementing-interfaces-on-a-type).\n\nBy default `default` tags are only contributing to documentation, \nif [`request.DecoderFactory.ApplyDefaults`](https://pkg.go.dev/github.com/swaggest/rest/request#DecoderFactory) is \nset to `true`, fields of request structure that don't have explicit value but have `default` will be populated with \ndefault value.\n\nIf input structure implements [`request.Loader`](https://pkg.go.dev/github.com/swaggest/rest/request#Loader),  \nthen `LoadFromHTTPRequest(r *http.Request) error` method will be invoked to populate input structure instead \nof automatic decoding. This allows low level control for cases that need it.\n\n\u003cdetails\u003e\n\u003csummary\u003eRequest decoder can be used standalone, in already existing `ServeHTTP`.\u003c/summary\u003e\n\n```go\ntype MyRequest struct {\n    Foo int    `header:\"X-Foo\"`\n    Bar string `formData:\"bar\"`\n    Baz bool   `query:\"baz\"`\n}\n\n// A decoder for particular structure, can be reused for multiple HTTP requests.\nmyDecoder := request.NewDecoderFactory().MakeDecoder(http.MethodPost, new(MyRequest), nil)\n\n// Request and response writer from ServeHTTP.\nvar (\n    rw  http.ResponseWriter\n    req *http.Request\n)\n\n// This code would presumably live in ServeHTTP.\nvar myReq MyRequest\n\nif err := myDecoder.Decode(req, \u0026myReq, nil); err != nil {\n    http.Error(rw, err.Error(), http.StatusBadRequest)\n}\n```\n\u003c/details\u003e\n\n### Response Encoder\n\nGo struct with field tags defines output port.\nResponse encoder writes data from output to `http.ResponseWriter` after use case interactor invocation finishes.\n\n```go\n// Declare output port type.\ntype helloOutput struct {\n    Now     time.Time `header:\"X-Now\" json:\"-\"`\n    Message string    `json:\"message\"`\n    Sess    string    `cookie:\"sess,httponly,secure,max-age=86400,samesite=lax\"`\n}\n```\n\nOutput data can be located in:\n* `json` for response body with `application/json` content,\n* `header` for values in response header,\n* `cookie` for cookie values, cookie fields can have configuration in field tag (same as in actual cookie, but with comma separation),\n* `contentType` for a non-empty string value that should be used as response body with given content type.\n\nFor more explicit separation of concerns between use case and transport it is possible to provide response header mapping \nseparately when initializing handler.\n\n```go\n// Declare output port type.\ntype helloOutput struct {\n    Now     time.Time `json:\"-\"`\n    Message string    `json:\"message\"`\n}\n```\n\n```go\n// Add use case handler with custom output headers mapping to router.\nr.Method(http.MethodGet, \"/hello/{name}\", nethttp.NewHandler(u,\n    nethttp.ResponseHeaderMapping(new(struct {\n        Now     time.Time `header:\"X-Now\"`\n    })),\n))\n```\n\nAdditional field tags describe JSON schema constraints, please check \n[documentation](https://pkg.go.dev/github.com/swaggest/jsonschema-go#Reflector.Reflect).\n\n### Creating Use Case Interactor\n\nHTTP transport is decoupled from business logic by adapting\n[use case interactors](https://pkg.go.dev/github.com/swaggest/usecase#Interactor).\n\nUse case interactor can define input and output ports that are used to map data between Go values and transport.\nIt can provide information about itself that will be exposed in generated documentation.\n\n```go\n// Create use case interactor with references to input/output types and interaction function.\nu := usecase.NewInteractor(func(ctx context.Context, input helloInput, output *helloOutput) error {\n    msg, available := messages[input.Locale]\n    if !available {\n        return status.Wrap(errors.New(\"unknown locale\"), status.InvalidArgument)\n    }\n\n    output.Message = fmt.Sprintf(msg, input.Name)\n    output.Now = time.Now()\n\n    return nil\n})\n```\n\n### Initializing Web Service\n\n[Web Service](https://pkg.go.dev/github.com/swaggest/rest/web#DefaultService) is an instrumented facade in front of \nrouter, it simplifies configuration and provides more compact API to add use cases.\n\n```go\n// Service initializes router with required middlewares.\nservice := web.NewService(openapi31.NewReflector())\n\n// It allows OpenAPI configuration.\nservice.OpenAPISchema().SetTitle(\"Albums API\")\nservice.OpenAPISchema().SetDescription(\"This service provides API to manage albums.\")\nservice.OpenAPISchema().SetVersion(\"v1.0.0\")\n\n// Additional middlewares can be added.\nservice.Use(\n    middleware.StripSlashes,\n\n    // cors.AllowAll().Handler, // \"github.com/rs/cors\", 3rd-party CORS middleware can also be configured here.\n)\n\n// Use cases can be mounted using short syntax .\u003cMethod\u003e(...).\nservice.Post(\"/albums\", postAlbums(), nethttp.SuccessStatus(http.StatusCreated))\n\nlog.Println(\"Starting service at http://localhost:8080\")\n\nif err := http.ListenAndServe(\"localhost:8080\", service); err != nil {\n    log.Fatal(err)\n}\n\n```\n\nUsually, `web.Service` API is sufficient, but if it is not, router can be configured manually, please check \nthe documentation below.\n\n\n## Security Setup\n\nExample with HTTP Basic Auth.\n\n```go\n// Prepare middleware with suitable security schema.\n// It will perform actual security check for every relevant request.\nadminAuth := middleware.BasicAuth(\"Admin Access\", map[string]string{\"admin\": \"admin\"})\n\n// Prepare API schema updater middleware.\n// It will annotate handler documentation with security schema.\nadminSecuritySchema := nethttp.HTTPBasicSecurityMiddleware(apiSchema, \"Admin\", \"Admin access\")\n\n// Endpoints with admin access.\nr.Route(\"/admin\", func(r chi.Router) {\n    r.Group(func(r chi.Router) {\n        r.Use(adminAuth, adminSecuritySchema) // Add both middlewares to routing group to enforce and document security.\n        r.Method(http.MethodPut, \"/hello/{name}\", nethttp.NewHandler(u))\n    })\n})\n```\n\nExample with cookie.\n\n```go\n// Security middlewares.\n//  - sessMW is the actual request-level processor,\n//  - sessDoc is a handler-level wrapper to expose docs.\nsessMW := func(handler http.Handler) http.Handler {\n    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n        if c, err := r.Cookie(\"sessid\"); err == nil {\n            r = r.WithContext(context.WithValue(r.Context(), \"sessionID\", c.Value))\n        }\n\n        handler.ServeHTTP(w, r)\n    })\n}\n\nsessDoc := nethttp.APIKeySecurityMiddleware(s.OpenAPICollector, \"User\",\n    \"sessid\", oapi.InCookie, \"Session cookie.\")\n\n// Security schema is configured for a single top-level route.\ns.With(sessMW, sessDoc).Method(http.MethodGet, \"/root-with-session\", nethttp.NewHandler(dummy()))\n\n// Security schema is configured on a sub-router.\ns.Route(\"/deeper-with-session\", func(r chi.Router) {\n    r.Group(func(r chi.Router) {\n        r.Use(sessMW, sessDoc)\n\n        r.Method(http.MethodGet, \"/one\", nethttp.NewHandler(dummy()))\n        r.Method(http.MethodGet, \"/two\", nethttp.NewHandler(dummy()))\n    })\n})\n\n```\n\nSee [example](./_examples/task-api/internal/infra/nethttp/router.go).\n\n## Handler Setup\n\nHandler is a generalized adapter for use case interactor, so usually setup is trivial.\n\n```go\n// Add use case handler to router.\nr.Method(http.MethodGet, \"/hello/{name}\", nethttp.NewHandler(u))\n```\n\n## Example\n\nFor non-generic use case, see another [example](./_examples/basic/main.go).\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"net/http\"\n\t\"time\"\n\n\t\"github.com/swaggest/openapi-go/openapi31\"\n\t\"github.com/swaggest/rest/response/gzip\"\n\t\"github.com/swaggest/rest/web\"\n\tswgui \"github.com/swaggest/swgui/v5emb\"\n\t\"github.com/swaggest/usecase\"\n\t\"github.com/swaggest/usecase/status\"\n)\n\nfunc main() {\n\ts := web.NewService(openapi31.NewReflector())\n\n\t// Init API documentation schema.\n\ts.OpenAPISchema().SetTitle(\"Basic Example\")\n\ts.OpenAPISchema().SetDescription(\"This app showcases a trivial REST API.\")\n\ts.OpenAPISchema().SetVersion(\"v1.2.3\")\n\n\t// Setup middlewares.\n\ts.Wrap(\n\t\tgzip.Middleware, // Response compression with support for direct gzip pass through.\n\t)\n\n\t// Declare input port type.\n\ttype helloInput struct {\n\t\tLocale string `query:\"locale\" default:\"en-US\" pattern:\"^[a-z]{2}-[A-Z]{2}$\" enum:\"ru-RU,en-US\"`\n\t\tName   string `path:\"name\" minLength:\"3\"` // Field tags define parameter location and JSON schema constraints.\n\n\t\t// Field tags of unnamed fields are applied to parent schema.\n\t\t// they are optional and can be used to disallow unknown parameters.\n\t\t// For non-body params, name tag must be provided explicitly.\n\t\t// E.g. here no unknown `query` and `cookie` parameters allowed,\n\t\t// unknown `header` params are ok.\n\t\t_ struct{} `query:\"_\" cookie:\"_\" additionalProperties:\"false\"`\n\t}\n\n\t// Declare output port type.\n\ttype helloOutput struct {\n\t\tNow     time.Time `header:\"X-Now\" json:\"-\"`\n\t\tMessage string    `json:\"message\"`\n\t}\n\n\tmessages := map[string]string{\n\t\t\"en-US\": \"Hello, %s!\",\n\t\t\"ru-RU\": \"Привет, %s!\",\n\t}\n\n\t// Create use case interactor with references to input/output types and interaction function.\n\tu := usecase.NewInteractor(func(ctx context.Context, input helloInput, output *helloOutput) error {\n\t\tmsg, available := messages[input.Locale]\n\t\tif !available {\n\t\t\treturn status.Wrap(errors.New(\"unknown locale\"), status.InvalidArgument)\n\t\t}\n\n\t\toutput.Message = fmt.Sprintf(msg, input.Name)\n\t\toutput.Now = time.Now()\n\n\t\treturn nil\n\t})\n\n\t// Describe use case interactor.\n\tu.SetTitle(\"Greeter\")\n\tu.SetDescription(\"Greeter greets you.\")\n\n\tu.SetExpectedErrors(status.InvalidArgument)\n\n\t// Add use case handler to router.\n\ts.Get(\"/hello/{name}\", u)\n\n\t// Swagger UI endpoint at /docs.\n\ts.Docs(\"/docs\", swgui.New)\n\n\t// Start server.\n\tlog.Println(\"http://localhost:8011/docs\")\n\tif err := http.ListenAndServe(\"localhost:8011\", s); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\n```\n\n![Documentation Page](./_examples/basic/screen.png)\n\n## Additional Integrations\n\n* [`github.com/gorilla/mux`](https://github.com/gorilla/mux), see [example](./gorillamux/example_openapi_collector_test.go).\n\n## Performance Optimization\n\nIf top performance is critical for the service or particular endpoints, you can trade \nsimplicity for performance by implementing manual request loader on input type.\n\n```go\nfunc (i *myInput) LoadFromHTTPRequest(r *http.Request) (err error) {\n\ti.Header = r.Header.Get(\"X-Header\")\n\n\treturn nil\n}\n```\n\nIf `request.Loader` is implemented, it will be called instead of both automatic decoding and validation.\n\nCheck advanced [example](https://github.com/swaggest/rest/blob/v0.2.29/_examples/advanced-generic/json_body_manual.go#L58).\n\nTo further improve performance you may try to use `fasthttp` instead of `net/http` with \n[`rest-fasthttp`](https://github.com/swaggest/rest-fasthttp) fork.\n\n## Versioning\n\nThis project adheres to [Semantic Versioning](https://semver.org/#semantic-versioning-200).\n\nBefore version `1.0.0`, breaking changes are tagged with `MINOR` bump, features and fixes are tagged with `PATCH` bump.\nAfter version `1.0.0`, breaking changes are tagged with `MAJOR` bump.\n\nBreaking changes are described in [UPGRADE.md](./UPGRADE.md).\n\n## Advanced Usage\n\n[Advanced Usage](./ADVANCED.md)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswaggest%2Frest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswaggest%2Frest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswaggest%2Frest/lists"}