mirror of
https://github.com/stevenhowes/osgrid-server.git
synced 2026-05-26 15:53:42 +01:00
Use go-http-middleware
This commit is contained in:
@@ -3,6 +3,7 @@ module osgrid-server
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/paulcager/go-http-middleware v0.0.0-20201217214326-f1e1271eaabe
|
||||||
github.com/paulcager/osgridref v1.0.1
|
github.com/paulcager/osgridref v1.0.1
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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/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 h1:/EX0qWi5LU9zjqE6bxIDgXSEJOAJRCqRIXAnZOB3JnI=
|
||||||
github.com/paulcager/osgridref v1.0.1/go.mod h1:ufyaMOUx5kOvX4naQZy2JxXME4RnN0y7lec+eIaAt2E=
|
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/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/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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
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/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=
|
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/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/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=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
+4
-31
@@ -4,11 +4,11 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/paulcager/go-http-middleware"
|
||||||
"github.com/paulcager/osgridref"
|
"github.com/paulcager/osgridref"
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
@@ -40,7 +40,7 @@ type Reply struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func makeHTTPServer(listenPort string) *http.Server {
|
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) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
gridRefStr := r.URL.Path[len("/"+apiVersion+"/gridref/"):]
|
gridRefStr := r.URL.Path[len("/"+apiVersion+"/gridref/"):]
|
||||||
gridRef, err := osgridref.ParseOsGridRef(gridRefStr)
|
gridRef, err := osgridref.ParseOsGridRef(gridRefStr)
|
||||||
@@ -53,7 +53,7 @@ func makeHTTPServer(listenPort string) *http.Server {
|
|||||||
handle(w, r, gridRef, lat, lon)
|
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) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
latLonStr := r.URL.Path[len("/"+apiVersion+"/latlon/"):]
|
latLonStr := r.URL.Path[len("/"+apiVersion+"/latlon/"):]
|
||||||
latLon, err := osgridref.ParseLatLon(latLonStr, 0, osgridref.WGS84)
|
latLon, err := osgridref.ParseLatLon(latLonStr, 0, osgridref.WGS84)
|
||||||
@@ -75,7 +75,7 @@ func makeHTTPServer(listenPort string) *http.Server {
|
|||||||
ReadHeaderTimeout: 20 * time.Second,
|
ReadHeaderTimeout: 20 * time.Second,
|
||||||
WriteTimeout: 2 * time.Minute,
|
WriteTimeout: 2 * time.Minute,
|
||||||
IdleTimeout: 10 * time.Minute,
|
IdleTimeout: 10 * time.Minute,
|
||||||
Handler: makeLoggingHandler(http.DefaultServeMux),
|
Handler: middleware.MakeLoggingHandler(http.DefaultServeMux),
|
||||||
Addr: listenPort,
|
Addr: listenPort,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,30 +105,3 @@ func handle(w http.ResponseWriter, _ *http.Request, ref osgridref.OsGridRef, lat
|
|||||||
w.WriteHeader(http.StatusBadGateway)
|
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())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user