Skip to content

Commit

Permalink
Allow setting rate limitation for API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
bclswl0827 committed Feb 10, 2024
1 parent 6237433 commit 61b34eb
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Starting from v2.2.5, all notable changes to this project will be documented in this file.

## v2.8.0

- Allow setting rate limitation for API endpoints

## v2.7.1

- Update frontend dependencies
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.7.1
v2.8.0
1 change: 1 addition & 0 deletions app/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ type ServerOptions struct {
CORS bool
WebPrefix string
APIPrefix string
RateFactor int
FeatureOptions *feature.FeatureOptions
}
3 changes: 2 additions & 1 deletion build/assets/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"host": "0.0.0.0",
"port": 8073,
"cors": true,
"debug": false
"debug": false,
"rate": 30
},
"miniseed_settings": {
"enable": false,
Expand Down
1 change: 1 addition & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func main() {
APIPrefix: API_PREFIX,
FeatureOptions: featureOptions,
CORS: conf.Server.CORS,
RateFactor: conf.Server.Rate,
})

// Receive interrupt signals
Expand Down
1 change: 1 addition & 0 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type server struct {
Port int `json:"port"`
CORS bool `json:"cors"`
Debug bool `json:"debug"`
Rate int `json:"rate"`
}

type miniseed struct {
Expand Down
4 changes: 2 additions & 2 deletions frontend/dist/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"files": {
"main.css": "/static/css/main.40ace055.css",
"main.js": "/static/js/main.a8051476.js",
"main.js": "/static/js/main.f3306104.js",
"static/css/290.525e2941.chunk.css": "/static/css/290.525e2941.chunk.css",
"static/js/290.4f498365.chunk.js": "/static/js/290.4f498365.chunk.js",
"static/js/735.26a45829.chunk.js": "/static/js/735.26a45829.chunk.js",
Expand Down Expand Up @@ -48,6 +48,6 @@
},
"entrypoints": [
"static/css/main.40ace055.css",
"static/js/main.a8051476.js"
"static/js/main.f3306104.js"
]
}
2 changes: 1 addition & 1 deletion frontend/dist/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><link rel="icon" href="/favicon.ico"/><link rel="manifest" href="/manifest.json"/><script defer="defer" src="/static/js/main.a8051476.js"></script><link href="/static/css/main.40ace055.css" rel="stylesheet"></head><body><div id="root"></div></body></html>
<!doctype html><html><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"/><link rel="icon" href="/favicon.ico"/><link rel="manifest" href="/manifest.json"/><script defer="defer" src="/static/js/main.f3306104.js"></script><link href="/static/css/main.40ace055.css" rel="stylesheet"></head><body><div id="root"></div></body></html>

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions frontend/src/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
REACT_APP_VERSION=v2.7.1
REACT_APP_RELEASE=51086059-20240201011711
REACT_APP_VERSION=v2.8.0
REACT_APP_RELEASE=6237433a-20240209214911
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/swaggo/files v1.0.1
github.com/swaggo/swag v1.16.2
github.com/wille/osutil v0.0.0-20230417145339-416c15a22a77
golang.org/x/time v0.5.0
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
gorm.io/driver/sqlserver v1.5.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
5 changes: 4 additions & 1 deletion server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
)

func RegisterRouterV1(rg *gin.RouterGroup, options *app.ServerOptions) {
rg.Use(limit.RateLimit(time.Second, CAPACITY, CAPACITY))
if options.RateFactor > 0 {
rateFactor := int64(options.RateFactor)
rg.Use(limit.RateLimit(time.Second, rateFactor, rateFactor))
}
services := []ApiServices{
&station.Station{},
&history.History{},
Expand Down
2 changes: 0 additions & 2 deletions server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"github.com/gin-gonic/gin"
)

const CAPACITY = 30 // Capacity for rate limiter to prevent from being attacked

type ApiServices interface {
RegisterModule(rg *gin.RouterGroup, options *app.ServerOptions)
}

0 comments on commit 61b34eb

Please sign in to comment.