https://github.com/jakecoffman/crud
OpenAPI v2 builder and input validation for Go APIs, with Swagger UI
https://github.com/jakecoffman/crud
api go golang openapi rest rest-api server swagger swagger-ui validation
Last synced: 5 days ago
JSON representation
OpenAPI v2 builder and input validation for Go APIs, with Swagger UI
- Host: GitHub
- URL: https://github.com/jakecoffman/crud
- Owner: jakecoffman
- License: apache-2.0
- Created: 2021-02-22T20:25:43.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2024-11-02T01:13:53.000Z (6 months ago)
- Last Synced: 2025-03-31T00:13:50.755Z (about 1 month ago)
- Topics: api, go, golang, openapi, rest, rest-api, server, swagger, swagger-ui, validation
- Language: Go
- Homepage:
- Size: 303 KB
- Stars: 43
- Watchers: 3
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## crud
[](https://godoc.org/github.com/jakecoffman/crud)
[](https://github.com/jakecoffman/crud/actions/workflows/go.yml)An OpenAPI v2 builder and validation library for building HTTP/REST APIs.
No additional dependencies besides the router you choose.
### Status
Version 1.0 is stable, version 2 will support OpenAPI v3.
### Why
OpenAPI is great, but up until now your options to use it are:
1. Write YAML by hand and then make your server match your spec.
2. Write YAML by hand and generate your server.
3. Generate YAML from comments in your code.None of these options seems like a great idea.
This project takes another approach: make a specification in Go code using type-safe builders where possible. The OpenAPI spec is generated from this and validation is done before your handler gets called.
This reduces boilerplate that you have to write and gives you nice documentation too!
### Examples
- [ServeMux Example](_example/main.go)
- [Gin-Gonic Example](adapters/gin-adapter/example)
- [Echo Example](adapters/echo-adapter/example)
- [Gorilla Mux Example](adapters/gorilla-adapter/example)### Getting started
Check the example directory under the adapters for a simple example.
Start by getting the package `go get github.com/jakecoffman/crud`
Then in your `main.go`:
1. Create a router with `NewRouter`, use an adapter from the `adapters` sub-package or write you own.
2. Add routes with `Add`.
3. Then call `Serve`.Routes are specifications that look like this:
```go
crud.Spec{
Method: "PATCH",
Path: "/widgets/{id}",
PreHandlers: Auth,
Handler: CreateHandler,
Description: "Adds a widget",
Tags: []string{"Widgets"},
Validate: crud.Validate{
Path: crud.Object(map[string]crud.Field{
"id": crud.Number().Required().Description("ID of the widget"),
}),
Body: crud.Object(map[string]crud.Field{
"owner": crud.String().Required().Example("Bob").Description("Widget owner's name"),
"quantity": crud.Integer().Min(1).Default(1).Description("The amount requested")
}),
},
}
```This will add a route `/widgets/:id` that responds to the PATCH method. It generates swagger and serves it at the root of the web application. It validates that the ID in the path is a number, so you don't have to. It also validates that the body is an object and has an "owner" property that is a string, again so you won't have to.
It mounts the swagger-ui at `/` and loads up the generated swagger.json:

The `PreHandlers` run before validation, and the `Handler` runs after validation is successful.