MCPcopy Index your code
hub / github.com/a-h/rest

github.com/a-h/rest @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
3,137 symbols 11,753 edges 37 files 65 documented · 2%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

REST

Document a REST API with an OpenAPI 3.0 specification.

  • Code, not configuration.
  • No magic comments, tags, or decorators.
  • Use with or without a Go web framework.
  • Populates schema automatically using reflection.

Why would I want to use this?

  • Add OpenAPI documentation to an API.
  • Create a swagger.json or swagger.yaml file.
  • Serve the Swagger UI to customers.

Examples

See the ./examples directory for complete examples.

Create an OpenAPI 3.0 (swagger) file

// Configure the models.
api := rest.NewAPI("messages")
api.StripPkgPaths = []string{"github.com/a-h/rest/example", "github.com/a-h/respond"}

api.RegisterModel(rest.ModelOf[respond.Error](), rest.WithDescription("Standard JSON error"), func(s *openapi3.Schema) {
  status := s.Properties["statusCode"]
  status.Value.WithMin(100).WithMax(600)
})

api.Get("/topic/{id}").
  HasPathParameter("id", rest.PathParam{
    Description: "id of the topic",
    Regexp:      `\d+`,
  }).
  HasResponseModel(http.StatusOK, rest.ModelOf[models.Topic]()).
  HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]())

// Create the specification.
spec, err := api.Spec()
if err != nil {
  log.Fatalf("failed to create spec: %v", err)
}

// Write to stdout.
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(spec)

Serve API documentation alongside your API

// Create routes.
router := http.NewServeMux()
router.Handle("/topics", &get.Handler{})
router.Handle("/topic", &post.Handler{})

api := rest.NewAPI("messages")
api.StripPkgPaths = []string{"github.com/a-h/rest/example", "github.com/a-h/respond"}

// Register the error type with customisations.
api.RegisterModel(rest.ModelOf[respond.Error](), rest.WithDescription("Standard JSON error"), func(s *openapi3.Schema) {
  status := s.Properties["statusCode"]
  status.Value.WithMin(100).WithMax(600)
})

api.Get("/topics").
  HasResponseModel(http.StatusOK, rest.ModelOf[get.TopicsGetResponse]()).
  HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]())

api.Post("/topic").
  HasRequestModel(rest.ModelOf[post.TopicPostRequest]()).
  HasResponseModel(http.StatusOK, rest.ModelOf[post.TopicPostResponse]()).
  HasResponseModel(http.StatusInternalServerError, rest.ModelOf[respond.Error]())

// Create the spec.
spec, err := api.Spec()
if err != nil {
  log.Fatalf("failed to create spec: %v", err)
}

// Apply any global customisation.
spec.Info.Version = "v1.0.0."
spec.Info.Description = "Messages API"

// Attach the Swagger UI handler to your router.
ui, err := swaggerui.New(spec)
if err != nil {
  log.Fatalf("failed to create swagger UI handler: %v", err)
}
router.Handle("/swagger-ui", ui)
router.Handle("/swagger-ui/", ui)

// And start listening.
fmt.Println("Listening on :8080...")
fmt.Println("Visit http://localhost:8080/swagger-ui to see API definitions")
fmt.Println("Listening on :8080...")
http.ListenAndServe(":8080", router)

Tasks

test

go test ./...

run-example

Dir: ./examples/stdlib

go run main.go

Extension points exported contracts — how you extend this code

CustomSchemaApplier (Interface)
CustomSchemaApplier is a type that customises its OpenAPI schema. [2 implementers]
api.go
ModelOpts (FuncType)
ModelOpts defines options that can be set when registering a model.
schema.go
FuncType (FuncType)
No need to document this.
getcomments/parser/tests/functiontypes/example.go
APIOpts (FuncType)
(no doc)
api.go

Core symbols most depended-on inside this repo

r
called by 2550
swaggerui/swagger-ui/swagger-ui-bundle.js
r
called by 2550
swaggerui/swagger-ui/swagger-ui-es-bundle.js
r
called by 1130
swaggerui/swagger-ui/swagger-ui-standalone-preset.js
q
called by 799
swaggerui/swagger-ui/swagger-ui.js
r
called by 665
swaggerui/swagger-ui/swagger-ui-es-bundle-core.js
get
called by 538
swaggerui/swagger-ui/swagger-ui-bundle.js
get
called by 538
swaggerui/swagger-ui/swagger-ui-es-bundle.js
r
called by 513
swaggerui/swagger-ui/swagger-ui.js

Shape

Function 1,336
Method 1,016
Class 716
Struct 50
TypeAlias 15
FuncType 3
Interface 1

Languages

TypeScript96%
Go4%

Modules by API surface

swaggerui/swagger-ui/swagger-ui-es-bundle.js874 symbols
swaggerui/swagger-ui/swagger-ui-bundle.js874 symbols
swaggerui/swagger-ui/swagger-ui.js495 symbols
swaggerui/swagger-ui/swagger-ui-es-bundle-core.js484 symbols
swaggerui/swagger-ui/swagger-ui-standalone-preset.js269 symbols
api.go39 symbols
schema_test.go25 symbols
schema.go19 symbols
getcomments/parser/tests/functions/example.go7 symbols
examples/chiexample/models/models.go5 symbols
examples/stdlib/handlers/topic/post/handler.go4 symbols
enums/enums_test.go4 symbols

For agents

$ claude mcp add rest \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact