Skip to content

Swagger/OpenAPI builder and input validation for Go APIs

License

Notifications You must be signed in to change notification settings

matthewtylerwood/crud

 
 

Repository files navigation

crud

GoDoc Go

A Swagger/OpenAPI builder and validation library for building HTTP/REST APIs.

Heavily inspired by hapi and the hapi-swagger projects.

No additional dependencies besides the router you choose.

Status

Version 1.0 is stable, version 2 will support OpenAPI v3.

Why

Swagger is great, but up until now your options to use swagger are:

  1. Write it by hand and then make your server match your spec.
  2. Write it by hand and generate your server.
  3. Generate it 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 nice builders where possible. The swagger is generated from this spec and validation is done before your handler gets called.

This reduces boilerplate that you have to write and gives you nice documentation too!

Examples

Builtin ServeMux not supported

This is disappointing, but the builtin http.ServeMux is not supported because it doesn't support routing by method, and doesn't support path params. This project is NOT a router so it will not try to reinvent these features.

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:

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:

screenshot

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

About

Swagger/OpenAPI builder and input validation for Go APIs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 98.1%
  • HTML 1.9%