openapi

package module
v0.0.0-...-cf24adf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2021 License: MIT Imports: 12 Imported by: 0

README

Go OpenAPI

This project extends the go-chi router to support OpenAPI 3, bringing to you a simple interface to build a router conforming your API contract.

Examples

See _examples/ for more examples.

As easy as:

package main

import (
	_ "embed"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"github.com/go-chi/render"

	"github.com/angelokurtis/go-openapi"
)

//go:embed openapi.yaml
var contract []byte

func main() {
	r := openapi.NewRouter(contract)
	r.Use(middleware.Logger)
	r.HandleOperation("getInvites", func(w http.ResponseWriter, r *http.Request) {
		render.Status(r, http.StatusOK)
		render.JSON(w, r, map[string]string{"operation": "getInvites"})
	})
	r.HandleOperation("createInvite", func(w http.ResponseWriter, r *http.Request) {
		render.Status(r, http.StatusCreated)
	})
	r.HandleOperation("deleteInvite", func(w http.ResponseWriter, r *http.Request) {
		_ = chi.URLParam(r, "inviteId")
		render.Status(r, http.StatusNoContent)
	})
	http.ListenAndServe(":3000", r)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Router

type Router struct {
	*chi.Mux
	// contains filtered or unexported fields
}

Router extends `chi.Mux` to enable it to deal with OpenAPI 3 contract.

func NewRouter

func NewRouter(openapi []byte) *Router

NewRouter returns a new Router object.

func (*Router) HandleOperation

func (r *Router) HandleOperation(operationID string, handlerFn http.HandlerFunc)

HandleOperation adds the route `pattern` that matches the `method` HTTP method described by `operationID` of OpenAPI 3 contract to execute the `handlerFn` http.HandlerFunc.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL