goatkeeper

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2020 License: Apache-2.0 Imports: 10 Imported by: 1

README

Goatkeeper

Build status codecov Codacy Badge Latest release Godoc License

GoatKeeper is an HTTP middleware for Golang that validates HTTP requests and their responses according to types defined in OpenAPI Specification.

Goals

  • Single source of truth for APIs using OpenAPI specifications
  • Ensuring request and response conformity to the specification

Installation

$ go get -u github.com/ashutoshgngwr/goatkeeper

Usage

spec, err := ioutil.ReadFile("./openapi-spec.yaml")
if err != nil {
  panic("unable to read openapi spec file")
}

middleware, err := goatkeeper.NewMiddlware(
  &goatkeeper.MiddlewareOptions{
    OpenAPISpec:      spec,
    ValidateResponse: true,
  })

if err != nil {
  panic("unable to initialize goatkeeper middleware")
}
With Gorilla mux
r := mux.NewRouter()
r.Use(middleware)
// r.Handle ...
With vanilla Golang
s := &http.Server{
  Addr:    ":8080",
  Handler: middlware(myAPIHandler),
}

log.Fatal(s.ListenAndServe())

Credits

Built using kin-openapi implementation!

License

Copyright 2019 Ashutosh Gangwar

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package goatkeeper is an HTTP middleware for Golang that validates HTTP requests and their responses according to types defined in OpenAPI Specification.

Built using kin-openapi implementation! https://github.com/getkin/kin-openapi

Usage:

spec, err := ioutil.ReadFile("./openapi-spec.yaml")
if err != nil {
  panic("unable to read openapi spec file")
}

middleware, err := goatkeeper.NewMiddlware(
  &goatkeeper.MiddlewareOptions{
    OpenAPISpec:      spec,
    ValidateResponse: true,
  })

if err != nil {
  panic("unable to initialize goatkeeper middleware")
}

With Gorilla mux

r := mux.NewRouter()
r.Use(middleware)
// r.Handle ...

With vanilla Golang

s := &http.Server{
  Addr:    ":8080",
  Handler: middlware(myAPIHandler),
}

log.Fatal(s.ListenAndServe())

Index

Constants

This section is empty.

Variables

View Source
var DefaultMiddlewareOptions = MiddlewareOptions{
	Logger:           logrtesting.NullLogger{},
	ValidateResponse: false,
	InvalidRequestResponse: &MiddlewareResponse{
		Headers: http.Header{},
		Status:  http.StatusBadRequest,
		Body:    []byte{},
	},
	InvalidResponseResponse: &MiddlewareResponse{
		Status:  http.StatusInternalServerError,
		Headers: http.Header{},
		Body:    []byte{},
	},
}

DefaultMiddlewareOptions defines default options used by the middleware. These options are only used if a option isn't specified when initializing the middleware.

Functions

func NewMiddleware

func NewMiddleware(opts *MiddlewareOptions) (func(http.Handler) http.Handler, error)

NewMiddleware creates a new HTTP middleware that will use the given OpenAPI Spec to validate all requests and their responses.

Types

type MiddlewareOptions

type MiddlewareOptions struct {
	Logger                  logr.Logger
	OpenAPISpec             []byte
	ValidateResponse        bool                // should goatkeeper also validate http responses
	InvalidRequestResponse  *MiddlewareResponse // response to write if request is invalid
	InvalidResponseResponse *MiddlewareResponse // response to write if response is invalid
}

MiddlewareOptions defines options for configuring the GoatKeeper middleware.

type MiddlewareResponse

type MiddlewareResponse struct {
	Status  int
	Body    []byte
	Headers http.Header
}

MiddlewareResponse defines a response format that middleware will write to http requests if their request body or response do not adhere to defined OpenAPI specification.

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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