proxy

package module
v0.0.0-...-4f4aa38 Latest Latest
Warning

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

Go to latest
Published: May 30, 2017 License: MIT Imports: 13 Imported by: 2

README

swagger-proxy Build Status

Swagger Proxy ensure HTTP Responses correctness based on swagger specs.

Usage

SwaggerProxy is designed to assist the development process, it can be used as a reverse proxy or as a middleware.

Reverse Proxy

SwaggerProxy sits between the client/integration suite and the server as follows:

[Client] ---> [SwaggerProxy] ---> [Server]

SwaggerProxy will rely the request and analize the server response.

Install
go get github.com/gchaincl/swagger-proxy/cmd/swagger-proxy`
Run
$ swagger-proxy -h
Usage of swagger-proxy:
  -bind string
        Bind Address (default ":1234")
  -spec string
        Swagger Spec (default "swagger.yml")
  -target string
        Target (default "http://localhost:4321")
  -verbose
        Verbose

Middleware

If your server is built in Golang, you can use it as a middleware:

package main

import (
	"log"
	"net/http"

	proxy "github.com/gchaincl/swagger-proxy"
	"github.com/go-openapi/loads"
)

func main() {
	doc, err := loads.Spec("swagger.json")
	if err != nil {
		log.Fatal(err)
	}

	p, err := proxy.New(doc.Spec(), &proxy.LogReporter{}, proxy.WithVerbose(true))
	if err != nil {
		log.Fatal(err)
	}

	app := func(w http.ResponseWriter, req *http.Request) {
		if req.Method == "POST" {
			w.WriteHeader(201)
		}
	}
	log.Printf("Server Running")
	http.ListenAndServe(":8989", p.Handler(http.HandlerFunc(app)))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WalkOps

func WalkOps(spec *spec.Swagger, fn WalkOpsFunc)

Types

type LogReporter

type LogReporter struct {
}

func (*LogReporter) Error

func (r *LogReporter) Error(req *http.Request, err error)

func (*LogReporter) Report

func (r *LogReporter) Report()

func (*LogReporter) Success

func (r *LogReporter) Success(req *http.Request)

func (*LogReporter) Warning

func (r *LogReporter) Warning(req *http.Request, msg string)

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

func New

func New(s *spec.Swagger, reporter Reporter, opts ...ProxyOpt) (*Proxy, error)

func (*Proxy) Handler

func (proxy *Proxy) Handler(next http.Handler) http.Handler

func (*Proxy) PendingOperations

func (proxy *Proxy) PendingOperations() []*spec.Operation

func (*Proxy) Router

func (proxy *Proxy) Router() http.Handler

func (*Proxy) SetSpec

func (proxy *Proxy) SetSpec(spec *spec.Swagger) error

func (*Proxy) Target

func (proxy *Proxy) Target() string

func (*Proxy) Validate

func (proxy *Proxy) Validate(resp Response, op *spec.Operation) error

func (*Proxy) ValidateBody

func (proxy *Proxy) ValidateBody(resp Response, op *spec.Operation) error

func (*Proxy) ValidateHeaders

func (proxy *Proxy) ValidateHeaders(resp Response, op *spec.Operation) error

func (*Proxy) ValidateMIME

func (proxy *Proxy) ValidateMIME(resp Response, op *spec.Operation) error

type ProxyOpt

type ProxyOpt func(*Proxy)

func WithTarget

func WithTarget(target string) ProxyOpt

func WithVerbose

func WithVerbose(v bool) ProxyOpt

type Reporter

type Reporter interface {
	Success(req *http.Request)
	Error(req *http.Request, err error)
	Warning(req *http.Request, msg string)
	Report()
}

type Response

type Response interface {
	Status() int
	Header() http.Header
	Body() []byte
}

type WalkOpsFunc

type WalkOpsFunc func(path, meth string, op *spec.Operation)

type WriterRecorder

type WriterRecorder struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func (*WriterRecorder) Body

func (w *WriterRecorder) Body() []byte

func (*WriterRecorder) Status

func (w *WriterRecorder) Status() int

func (*WriterRecorder) Write

func (w *WriterRecorder) Write(body []byte) (n int, err error)

func (*WriterRecorder) WriteHeader

func (w *WriterRecorder) WriteHeader(status int)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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