1
0
Fork 0
mirror of https://github.com/Luzifer/staticmap.git synced 2024-10-18 07:34:23 +00:00

Add rate limit not to bust the OSM servers

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-06-27 23:09:46 +02:00
parent 178253acb3
commit 1ed601b84f
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
2 changed files with 36 additions and 1 deletions

29
Godeps/Godeps.json generated
View file

@ -27,6 +27,22 @@
"ImportPath": "github.com/Wessie/appdirs",
"Rev": "6573e894f8e294cbae0c4e45c25ff9f2e2918a4e"
},
{
"ImportPath": "github.com/didip/tollbooth",
"Rev": "e11ced12e0e0bbfedbc1783fcf0a5ec7f9dc4856"
},
{
"ImportPath": "github.com/didip/tollbooth/config",
"Rev": "e11ced12e0e0bbfedbc1783fcf0a5ec7f9dc4856"
},
{
"ImportPath": "github.com/didip/tollbooth/errors",
"Rev": "e11ced12e0e0bbfedbc1783fcf0a5ec7f9dc4856"
},
{
"ImportPath": "github.com/didip/tollbooth/libstring",
"Rev": "e11ced12e0e0bbfedbc1783fcf0a5ec7f9dc4856"
},
{
"ImportPath": "github.com/flopp/go-coordsparser",
"Rev": "845bca739e263e1cd38de25024a47b4d6acbfc1f"
@ -82,6 +98,11 @@
"ImportPath": "github.com/lucasb-eyer/go-colorful",
"Rev": "c900de9dbbc73129068f5af6a823068fc5f2308c"
},
{
"ImportPath": "github.com/patrickmn/go-cache",
"Comment": "v2.0.0-9-g7ac1518",
"Rev": "7ac151875ffb48b9f3ccce9ea20f020b0c1596c8"
},
{
"ImportPath": "github.com/spf13/pflag",
"Rev": "c7e63cf4530bcd3ba943729cee0efeff2ebea63f"
@ -110,10 +131,18 @@
"ImportPath": "golang.org/x/image/math/fixed",
"Rev": "97680175a5267bb8b31f1923e7a66df98013b11a"
},
{
"ImportPath": "golang.org/x/net/context",
"Rev": "dfe83d419c9403b40b19d08cdba2afec27b002f7"
},
{
"ImportPath": "golang.org/x/sys/unix",
"Rev": "8f0908ab3b2457e2e15403d3697c9ef5cb4b57a9"
},
{
"ImportPath": "golang.org/x/time/rate",
"Rev": "8be79e1e0910c292df4e79c241bb7e8f7e725959"
},
{
"ImportPath": "gopkg.in/validator.v2",
"Rev": "07ffaad256c8e957050ad83d6472eb97d785013d"

View file

@ -12,6 +12,7 @@ import (
httpHelper "github.com/Luzifer/go_helpers/http"
"github.com/Luzifer/rconfig"
log "github.com/Sirupsen/logrus"
"github.com/didip/tollbooth"
"github.com/golang/geo/s2"
"github.com/gorilla/mux"
colorful "github.com/lucasb-eyer/go-colorful"
@ -23,6 +24,8 @@ var (
ForceCache time.Duration `flag:"force-cache" default:"24h" description:"Force map to be cached for this duration"`
Listen string `flag:"listen" default:":3000" description:"IP/Port to listen on"`
MaxSize string `flag:"max-size" default:"1024x1024" description:"Maximum map size requestable"`
RateLimit int64 `flag:"rate-limit" default:"1" description:"How many requests to allow per time"`
RateLimitTime time.Duration `flag:"rate-limit-time" default:"1s" description:"Time interval to allow N requests in"`
VersionAndExit bool `flag:"version" default:"false" description:"Print version information and exit"`
}
@ -48,9 +51,12 @@ func init() {
}
func main() {
rateLimit := tollbooth.NewLimiter(cfg.RateLimit, cfg.RateLimitTime)
rateLimit.IPLookups = []string{"X-Forwarded-For", "RemoteAddr", "X-Real-IP"}
r := mux.NewRouter()
r.HandleFunc("/status", func(res http.ResponseWriter, r *http.Request) { http.Error(res, "I'm fine", http.StatusOK) })
r.HandleFunc("/map.png", handleMapRequest)
r.Handle("/map.png", tollbooth.LimitFuncHandler(rateLimit, handleMapRequest))
log.Fatalf("HTTP Server exitted: %s", http.ListenAndServe(cfg.Listen, httpHelper.NewHTTPLogHandler(r)))
}