-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (66 loc) · 1.7 KB
/
main.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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/nathan-tw/swif_devops_assignment/limiter"
"github.com/redis/go-redis/v9"
"gopkg.in/yaml.v3"
)
type RedisConfig struct {
Host string `yaml:"Host"`
Port int `yaml:"Port"`
}
func main() {
yamlFile, err := os.ReadFile("config/redis_config.yaml")
if err != nil {
log.Fatalf("Error reading YAML file: %v", err)
return
}
var config RedisConfig
err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
log.Fatalf("Error unmarshalling YAML: %v", err)
return
}
redisAddr := fmt.Sprintf("%v:%v", config.Host, config.Port)
fmt.Println(redisAddr)
redisClient := redis.NewClient(&redis.Options{
Addr: redisAddr,
DB: 0,
})
if redisClient == nil {
log.Fatal("Failed to new redis")
return
}
limiter, err := limiter.NewLimiter(redisClient)
if err != nil {
log.Fatalf("Failed to new rate limiter: %v", err)
return
}
r := gin.Default()
r.GET("/flush_redis", func(c *gin.Context) {
if status := redisClient.FlushAll(c); status.Err() != nil {
c.String(http.StatusInternalServerError, status.Err().Error())
} else {
c.String(http.StatusOK, "successfully flush redis")
}
})
r.GET("/reload_limit_config", func(c *gin.Context) {
err := limiter.ReloadConfig()
if err != nil {
c.String(http.StatusInternalServerError, "failed to reload rate limit config")
}
c.String(http.StatusOK, "config reloaded")
})
apiGroup := r.Group("/api", limiter.LimitEndpoint(), limiter.LimitAccount())
apiGroup.GET("/path1", func(c *gin.Context) {
c.String(http.StatusOK, "path1 visited")
})
apiGroup.GET("/path2", func(c *gin.Context) {
c.String(http.StatusOK, "path2 visited")
})
r.Run("0.0.0.0:8080")
}