Skip to content

Commit 0ead621

Browse files
committed
Add Rate Limit
1 parent 7ab2173 commit 0ead621

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

cmd/web/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ import (
1515
"github.com/stripe/stripe-go/v84"
1616
)
1717

18-
// TODO: 防止 /checkout/success 被滥用
1918
// TODO: 创建订阅集合迁移文件
2019
// TODO: 创建用户集合迁移文件
2120
// TODO: 用.env初始化SMTP和设置
22-
// TODO: 添加限速
2321
// TODO: 发送各种邮件
2422

2523
const version string = "v1.0.0-alpha"
@@ -42,6 +40,8 @@ func main() {
4240
// 版本
4341
app.Logger().Info("Pdnode Website API " + version)
4442

43+
// 初始化
44+
4545
// loosely check if it was executed using "go run"
4646
isGoRun := strings.HasPrefix(os.Args[0], os.TempDir())
4747

@@ -53,6 +53,10 @@ func main() {
5353

5454
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
5555

56+
settings := app.Settings()
57+
58+
config.InitRateLimitRule(settings)
59+
5660
se.Router.GET("/{path...}", apis.Static(os.DirFS("./web/build"), true))
5761

5862
// 调用订阅模块,把 app, se 和 cfg 传进去

config/rate_limits.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package config
2+
3+
import "github.com/pocketbase/pocketbase/core"
4+
5+
func InitRateLimitRule(settings *core.Settings) {
6+
settings.RateLimits.Enabled = true
7+
settings.RateLimits.Rules = []core.RateLimitRule{
8+
{
9+
Label: "*:auth",
10+
MaxRequests: 2,
11+
Duration: 3,
12+
Audience: "",
13+
},
14+
{
15+
Label: "/api/",
16+
MaxRequests: 300,
17+
Duration: 10,
18+
Audience: "",
19+
},
20+
{
21+
Label: "/api/webhook/stripe",
22+
MaxRequests: 500,
23+
Duration: 5,
24+
Audience: "",
25+
},
26+
{
27+
Label: "/api/checkout/subscription",
28+
MaxRequests: 5,
29+
Duration: 10,
30+
Audience: "@auth",
31+
},
32+
{
33+
Label: "/api/checkout/subscription",
34+
MaxRequests: 2,
35+
Duration: 10,
36+
Audience: "@guest",
37+
},
38+
}
39+
}

0 commit comments

Comments
 (0)