-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
48 lines (40 loc) · 1.03 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
package main
import (
"log"
"net/http"
"github.com/go-redis/redis/v8"
"github.com/joho/godotenv"
"go-expert-rater-limit/config"
"go-expert-rater-limit/limiter"
"go-expert-rater-limit/middleware"
"go-expert-rater-limit/storage"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
cfg := config.Load()
redisClient := redis.NewClient(&redis.Options{
Addr: cfg.RedisAddr,
})
store := storage.NewRedisStorage(redisClient)
rateLimiter := limiter.NewRateLimiter(store)
limiterMiddleware := middleware.NewRateLimiterMiddleware(
rateLimiter,
cfg.IPLimit,
cfg.TokenLimit,
cfg.IPDuration,
cfg.IPBlockTime,
cfg.TokenBlockTime,
)
mux := http.NewServeMux()
mux.Handle("/", limiterMiddleware.Handle(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("Heeeey Rater Limit :)"))
if err != nil {
log.Println(err)
}
})))
log.Printf("Server starting on port %s", cfg.ServerPort)
log.Fatal(http.ListenAndServe(":"+cfg.ServerPort, mux))
}