openapi: Read Accept-Content to send gzip if needed

pull/6/head
Antoine Pelisse 2017-06-27 13:04:09 -07:00
parent 9120d58fc9
commit bd38dd4d12
1 changed files with 17 additions and 13 deletions

View File

@ -26,6 +26,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/NYTimes/gziphandler"
"github.com/go-openapi/spec" "github.com/go-openapi/spec"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/googleapis/gnostic/OpenAPIv2" "github.com/googleapis/gnostic/OpenAPIv2"
@ -75,19 +76,22 @@ func RegisterOpenAPIService(openapiSpec *spec.Swagger, servePath string, mux *ge
for _, file := range files { for _, file := range files {
path := servePathBase + file.ext path := servePathBase + file.ext
getData := file.getData getData := file.getData
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { mux.Handle(path, gziphandler.GzipHandler(http.HandlerFunc(
if r.URL.Path != path { func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound) if r.URL.Path != path {
w.Write([]byte("Path not found!")) w.WriteHeader(http.StatusNotFound)
return w.Write([]byte("Path not found!"))
} return
o.update(r) }
data := getData() o.update(r)
etag := computeEtag(data) data := getData()
w.Header().Set("Etag", etag) etag := computeEtag(data)
// ServeContent will take care of caching using eTag. w.Header().Set("Etag", etag)
http.ServeContent(w, r, path, o.lastModified, bytes.NewReader(data))
}) // ServeContent will take care of caching using eTag.
http.ServeContent(w, r, path, o.lastModified, bytes.NewReader(data))
}),
))
} }
return &o, nil return &o, nil