-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.go
73 lines (69 loc) · 1.76 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package alist
import (
"os"
"strconv"
"time"
"github.com/axiaoxin-com/ratelimiter"
"github.com/gin-gonic/gin"
)
func initVar() {
s := os.Getenv("ALI_LIMIT_MINUTES")
v, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
aliMinutes = v
s = os.Getenv("ALI_LIMIT_MAX")
v, err = strconv.Atoi(s)
if err != nil {
panic(err)
}
aliMax = v
// client
aliClientID = os.Getenv("ALI_DRIVE_CLIENT_ID")
aliClientSecret = os.Getenv("ALI_DRIVE_CLIENT_SECRET")
baiduClientId = os.Getenv("BAIDU_CLIENT_ID")
baiduClientSecret = os.Getenv("BAIDU_CLIENT_SECRET")
}
func Setup(g *gin.RouterGroup) {
initVar()
g.GET("/ali/qr", Qr)
g.POST("/ali/ck", Ck)
g.POST("/onedrive/get_refresh_token", onedriveToken)
g.POST("/onedrive/get_site_id", spSiteID)
g.GET("/baidu/get_refresh_token", baiduToken)
aliOpen := g.Group("/ali_open")
aliOpen.Any("/limit", func(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"minutes": aliMinutes,
"max": aliMax,
})
})
aliOpenLimit := aliOpen.Group("")
aliOpenLimit.Use(ratelimiter.GinMemRatelimiter(ratelimiter.GinRatelimiterConfig{
LimitKey: func(ctx *gin.Context) string {
return ctx.ClientIP()
},
LimitedHandler: func(ctx *gin.Context) {
ctx.JSON(429, gin.H{
"code": "Too Many Requests",
"message": "Too Many Requests",
"error": "Too Many Requests",
})
ctx.Abort()
},
TokenBucketConfig: func(context *gin.Context) (time.Duration, int) {
return time.Duration(aliMinutes) * time.Minute, aliMax
},
}))
aliOpenLimit.Any("/test", func(c *gin.Context) {
c.JSON(200, gin.H{
"ClientIP": c.ClientIP(),
"RemoteIP": c.RemoteIP(),
})
})
aliOpenLimit.Any("/token", aliAccessToken)
aliOpenLimit.Any("/refresh", aliAccessToken)
aliOpenLimit.Any("/code", aliAccessToken)
aliOpenLimit.Any("/qr", aliQrcode)
}