Swagger Proxy ensure HTTP Responses correctness based on swagger specs.
SwaggerProxy is designed to assist the development process, it can be used as a reverse proxy or as a middleware.
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.
go get github.com/gchaincl/swagger-proxy/cmd/swagger-proxy`
$ 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
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)))
}