All Projects → mnf-group → openapimux

mnf-group / openapimux

Licence: MIT license
Open API router in go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to openapimux

openapi-eller
Generate OpenAPI v3 clients and servers from the command line
Stars: ✭ 19 (-9.52%)
Mutual labels:  openapi, openapi3
whook
Build strong and efficient REST web services.
Stars: ✭ 18 (-14.29%)
Mutual labels:  openapi, openapi3
ogen
OpenAPI v3 code generator for go
Stars: ✭ 436 (+1976.19%)
Mutual labels:  openapi, openapi3
openapi-filter
Filter internal paths, operations, parameters, schemas etc from OpenAPI/Swagger/AsyncAPI definitions
Stars: ✭ 112 (+433.33%)
Mutual labels:  openapi, openapi3
Unchase.OpenAPI.Connectedservice
📜 Visual Studio extension to generate OpenAPI (Swagger) web service reference.
Stars: ✭ 69 (+228.57%)
Mutual labels:  openapi, openapi3
intellij-openapi-generator
Intellij Plugin for openapi-generator
Stars: ✭ 73 (+247.62%)
Mutual labels:  openapi, openapi3
openapi
OpenAPI 3 Specification for golang
Stars: ✭ 18 (-14.29%)
Mutual labels:  openapi, openapi3
oaie-sketch
OpenAPI Visual Editor
Stars: ✭ 54 (+157.14%)
Mutual labels:  openapi, openapi3
HibiAPI
一个实现了多种常用站点的易用化API的程序 / A program that implements easy-to-use APIs for a variety of commonly used sites.
Stars: ✭ 427 (+1933.33%)
Mutual labels:  openapi, openapi3
oas
OpenAPI Spec builder in go
Stars: ✭ 15 (-28.57%)
Mutual labels:  openapi, openapi3
openapi-viewer
Browse and test a REST API described with the OpenAPI 3.0 Specification
Stars: ✭ 85 (+304.76%)
Mutual labels:  openapi, openapi3
n26-api
Unofficial N26 Bank API documentation
Stars: ✭ 41 (+95.24%)
Mutual labels:  openapi, openapi3
aws2openapi
Amazon Web Services API description to OpenAPI 3.0 definition
Stars: ✭ 45 (+114.29%)
Mutual labels:  openapi, openapi3
openapi4j
OpenAPI 3 parser, JSON schema and request validator.
Stars: ✭ 92 (+338.1%)
Mutual labels:  openapi, openapi3
specifications-ITS-REST
openEHR REST API Specifications
Stars: ✭ 20 (-4.76%)
Mutual labels:  openapi, openapi3
swagger-converter
OpenAPI/Swagger 2.0 to OpenAPI 3.0 Converter WebService
Stars: ✭ 58 (+176.19%)
Mutual labels:  openapi, openapi3
openapi-boilerplate
📘 Multi-file boilerplate for Open API Specification
Stars: ✭ 280 (+1233.33%)
Mutual labels:  openapi, openapi3
OpenAPI
A pharo implementation of OpenAPI 3.0.1
Stars: ✭ 20 (-4.76%)
Mutual labels:  openapi, openapi3
openapi-generator-go
An opinionated OpenAPI v3 code generator for Go. Use this to generate API models and router scaffolding.
Stars: ✭ 42 (+100%)
Mutual labels:  openapi, openapi3
OpenAlchemy
Define SQLAlchemy models using the OpenAPI specification.
Stars: ✭ 39 (+85.71%)
Mutual labels:  openapi, openapi3

OpenAPIMux

OpenAPIMux is a "schema-first" HTTP router. It takes one or multiple OpenAPI (Swagger) schema files as an input and then matches, validates and handles all incoming HTTP based on these schemas. Under the hood, it uses kin-openapi for OpenAPI schema parsing and validation.

Motivation

OpenAPI offers a great way of documenting API. However, none of existing go routers offers "schema first" approach. In each router, you need to initialize a list of available routes and then do the request validation manually. OpenAPIMux fills this gap by allowing to initialize a router directly from the OpenAPI schema definition file.

Features

  • Works with both OpenAPI 3.0 and OpenAPI 2.0 (aka Swagger). As well as both json and yaml schemas
  • Multiple OpenAPI schema files can be used at the same router to support API versioning
  • Implements http.Handler interface, so it is compatible with the standard http.ServeMux
  • Supports global level http.Handler middlewares, so it is compatible with third-party middlewares
  • Supports custom error handler for more control

Routing

  • operationId attribute of an OpenAPI path is used to resolve it to an appropriate handler
  • OpenAPIMux can encapsulate one or more swagger routers. Each router could be created from an OpenAPI schema file or directly as a swagger object
  • To handle multiple versions, use the servers.url attribute in OpenAPI schema. Eg
servers:
 - url: "/v1.2"
  • When finding a matching route, routers with servers attribute set take priority

Install

go get -u github.com/MNFGroup/openapimux

Full Example

Assuming openapi.yaml has the following schema

openapi: 3.0.0

paths:
  /foo:
    get:
      operationId: getFoo

It will create and start a server on 8080

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/go-chi/chi/middleware"
)

type fooHandler struct{}

func (f fooHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hello")
}

func main() {
	r, err := NewRouter("./openapi.yaml")
	if err != nil {
		panic(err)
	}

	r.UseHandlers(map[string]http.Handler{
		"getFoo": fooHandler{},
	})

	r.UseMiddleware(
		middleware.Recoverer,
		middleware.RequestID,
		middleware.DefaultCompress,
	)

	r.ErrorHandler = func(w http.ResponseWriter, r *http.Request, data string, code int) {
		w.WriteHeader(code)
		if code == http.StatusInternalServerError {
			fmt.Println("Fatal:", data)
			w.Write([]byte("Oops"))
		} else {
			w.Write([]byte(data))
		}
	}

	log.Fatal(http.ListenAndServe(":8080", r))
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].