Browse Source

Limit API endpoint with 5 requests per IP by default

Volodymyr Tkach 2 years ago
parent
commit
77de2cf70d
2 changed files with 3 additions and 2 deletions
  1. 1 1
      internal/consts/consts.go
  2. 2 1
      internal/server/server.go

+ 1 - 1
internal/consts/consts.go

@@ -16,7 +16,7 @@ var Config struct {
 	Deployment    string `default:"development" description:"Deployment type"`
 	Deployment    string `default:"development" description:"Deployment type"`
 	ErrorLogFile  string `description:"Error log file"`
 	ErrorLogFile  string `description:"Error log file"`
 	Host          string `default:"127.0.0.1" description:"Web server IP"`
 	Host          string `default:"127.0.0.1" description:"Web server IP"`
-	LimitRequests int64  `default:"5" description:"Requests per second per one IP"`
+	LimitRequests int    `default:"5" description:"Requests per second per one IP"`
 	Port          string `default:"8080" description:"Web server port"`
 	Port          string `default:"8080" description:"Web server port"`
 	WebURL        string `default:"http://localhost:8080/" description:"Web server home URL"`
 	WebURL        string `default:"http://localhost:8080/" description:"Web server home URL"`
 }
 }

+ 2 - 1
internal/server/server.go

@@ -14,6 +14,7 @@ import (
 	"github.com/vladimirok5959/golang-ip2location/internal/server/web"
 	"github.com/vladimirok5959/golang-ip2location/internal/server/web"
 	"github.com/vladimirok5959/golang-utils/utils/http/apiserv"
 	"github.com/vladimirok5959/golang-utils/utils/http/apiserv"
 	"github.com/vladimirok5959/golang-utils/utils/http/helpers"
 	"github.com/vladimirok5959/golang-utils/utils/http/helpers"
+	"github.com/vladimirok5959/golang-utils/utils/http/servlimit"
 )
 )
 
 
 func NewMux(ctx context.Context, shutdown context.CancelFunc, client *client.Client) *apiserv.ServeMux {
 func NewMux(ctx context.Context, shutdown context.CancelFunc, client *client.Client) *apiserv.ServeMux {
@@ -30,7 +31,7 @@ func NewMux(ctx context.Context, shutdown context.CancelFunc, client *client.Cli
 	// API
 	// API
 	mux.Get("/api/v1/app/health", v1_app_health.Handler{Handler: handler})
 	mux.Get("/api/v1/app/health", v1_app_health.Handler{Handler: handler})
 	mux.Get("/api/v1/app/status", helpers.HandleAppStatus())
 	mux.Get("/api/v1/app/status", helpers.HandleAppStatus())
-	mux.Get("/api/v1/ip2location/{s}", v1_ip2location.Handler{Handler: handler})
+	mux.Get("/api/v1/ip2location/{s}", servlimit.ReqPerSecond(v1_ip2location.Handler{Handler: handler}, consts.Config.LimitRequests))
 
 
 	// Assets
 	// Assets
 	mux.Get("/styles.css", helpers.HandleTextCss(web.StylesCss))
 	mux.Get("/styles.css", helpers.HandleTextCss(web.StylesCss))