-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
33 lines (27 loc) · 738 Bytes
/
Copy pathmain.go
File metadata and controls
33 lines (27 loc) · 738 Bytes
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
package main
import (
"errors"
"net/http"
"rate-limiter/internal/limiter"
"rate-limiter/internal/middleware"
"time"
"github.com/go-chi/chi/v5"
)
func main() {
r := chi.NewRouter()
redisLimiter := limiter.NewRedisLimiter("localhost:6379", 10, 2)
localLimiter := limiter.NewLocalLimiter(10, 2)
orchestrator := limiter.NewOrchestrator(redisLimiter, localLimiter)
r.Use(middleware.RateLimit(orchestrator))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("hello"))
})
srv := &http.Server{
Addr: ":8080",
Handler: r,
ReadHeaderTimeout: 5 * time.Second,
}
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}