-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_2.go
51 lines (37 loc) · 952 Bytes
/
example_2.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
package main
import (
"log"
limiter "github.com/Jokeramc/gin-limit"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v8"
)
// Expected updates in the future
func NewServer() *gin.Engine {
server := gin.Default()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
dispatcher, _ := limiter.LimitDispatcher("2-M", 5, rdb)
server.POST("/post1", dispatcher.MiddleWare("20-S", 30), post1) // /post1
server.POST("/api/post2", dispatcher.MiddleWare("15-M", 40), post2) // /api/post2
server.POST("/post3", dispatcher.MiddleWare("11-D", 10), post3) // /post3
return server
}
func post1(ctx *gin.Context) {
ctx.String(200, ctx.FullPath())
}
func post2(ctx *gin.Context) {
ctx.String(200, ctx.FullPath())
}
func post3(ctx *gin.Context) {
ctx.String(200, ctx.ClientIP())
}
func main() {
server := NewServer()
err := server.Run(":8080")
if err != nil {
log.Println(err)
}
}