From 97ace53ead080a8a7d0607255c274eff94e4c678 Mon Sep 17 00:00:00 2001 From: Paul Cager Date: Thu, 17 Dec 2020 21:59:36 +0000 Subject: [PATCH] Use go-http-middleware --- go.mod | 1 + go.sum | 9 +++++++++ osgrid-server.go | 35 ++++------------------------------- 3 files changed, 14 insertions(+), 31 deletions(-) diff --git a/go.mod b/go.mod index 2b99d28..1ea1dd5 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module osgrid-server go 1.15 require ( + github.com/paulcager/go-http-middleware v0.0.0-20201217214326-f1e1271eaabe github.com/paulcager/osgridref v1.0.1 github.com/spf13/pflag v1.0.5 ) diff --git a/go.sum b/go.sum index dfd4ebe..b2dc0a7 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,21 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/paulcager/go-http-middleware v0.0.0-20201217214326-f1e1271eaabe h1:fA+EZAArUBNnCYx0/5Ct/Rq4lKDmVTzxllz8qccHXFI= +github.com/paulcager/go-http-middleware v0.0.0-20201217214326-f1e1271eaabe/go.mod h1:K4zISwO5aHZ0dwjY2h0KWB939Rg/ncdkcdTZjze4cFU= github.com/paulcager/osgridref v1.0.1 h1:/EX0qWi5LU9zjqE6bxIDgXSEJOAJRCqRIXAnZOB3JnI= github.com/paulcager/osgridref v1.0.1/go.mod h1:ufyaMOUx5kOvX4naQZy2JxXME4RnN0y7lec+eIaAt2E= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robertkrimen/otto v0.0.0-20200922221731-ef014fd054ac h1:kYPjbEN6YPYWWHI6ky1J813KzIq/8+Wg4TO4xU7A/KU= github.com/robertkrimen/otto v0.0.0-20200922221731-ef014fd054ac/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI= gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/osgrid-server.go b/osgrid-server.go index 7bf5921..2c87f91 100644 --- a/osgrid-server.go +++ b/osgrid-server.go @@ -4,11 +4,11 @@ import ( "encoding/json" "fmt" "log" - "math" "net/http" "strings" "time" + "github.com/paulcager/go-http-middleware" "github.com/paulcager/osgridref" flag "github.com/spf13/pflag" ) @@ -40,7 +40,7 @@ type Reply struct { } func makeHTTPServer(listenPort string) *http.Server { - http.Handle("/"+apiVersion+"/gridref/", makeCachingHandler(1*time.Hour, http.HandlerFunc( + http.Handle("/"+apiVersion+"/gridref/", middleware.MakeCachingHandler(1*time.Hour, http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { gridRefStr := r.URL.Path[len("/"+apiVersion+"/gridref/"):] gridRef, err := osgridref.ParseOsGridRef(gridRefStr) @@ -53,7 +53,7 @@ func makeHTTPServer(listenPort string) *http.Server { handle(w, r, gridRef, lat, lon) }))) - http.Handle("/"+apiVersion+"/latlon/", makeCachingHandler(1*time.Hour, http.HandlerFunc( + http.Handle("/"+apiVersion+"/latlon/", middleware.MakeCachingHandler(1*time.Hour, http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { latLonStr := r.URL.Path[len("/"+apiVersion+"/latlon/"):] latLon, err := osgridref.ParseLatLon(latLonStr, 0, osgridref.WGS84) @@ -75,7 +75,7 @@ func makeHTTPServer(listenPort string) *http.Server { ReadHeaderTimeout: 20 * time.Second, WriteTimeout: 2 * time.Minute, IdleTimeout: 10 * time.Minute, - Handler: makeLoggingHandler(http.DefaultServeMux), + Handler: middleware.MakeLoggingHandler(http.DefaultServeMux), Addr: listenPort, } @@ -105,30 +105,3 @@ func handle(w http.ResponseWriter, _ *http.Request, ref osgridref.OsGridRef, lat w.WriteHeader(http.StatusBadGateway) } } - -func makeCachingHandler(age time.Duration, h http.Handler) http.Handler { - ageSeconds := int64(math.Round(age.Seconds())) - if ageSeconds <= 0 { - return h - } - - header := fmt.Sprintf("public,max-age=%d", ageSeconds) - return http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - w.Header().Add("Cache-Control", header) - h.ServeHTTP(w, r) - }) -} - -func makeLoggingHandler(h http.Handler) http.Handler { - return http.HandlerFunc( - func(w http.ResponseWriter, r *http.Request) { - start := time.Now() - h.ServeHTTP(w, r) - end := time.Now() - - uri := r.URL.String() - method := r.Method - fmt.Printf("%s %s %s %d\n", method, uri, r.RemoteAddr, end.Sub(start).Milliseconds()) - }) -}