feat: integrate new rate limiter into server
This commit is contained in:
@@ -35,6 +35,14 @@ func main() {
|
|||||||
|
|
||||||
jikanClient := jikan.NewClient(queries)
|
jikanClient := jikan.NewClient(queries)
|
||||||
|
|
||||||
|
authLimiter := server.NewAuthLimiter()
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
authLimiter.Cleanup(time.Now())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||||
defer stop()
|
defer stop()
|
||||||
|
|
||||||
@@ -45,6 +53,7 @@ func main() {
|
|||||||
SQLDB: dbConn,
|
SQLDB: dbConn,
|
||||||
JikanClient: jikanClient,
|
JikanClient: jikanClient,
|
||||||
AuthService: auth.NewService(queries),
|
AuthService: auth.NewService(queries),
|
||||||
|
AuthLimiter: authLimiter,
|
||||||
PlaybackProxySecret: playbackSecret(),
|
PlaybackProxySecret: playbackSecret(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ type Config struct {
|
|||||||
SQLDB *sql.DB
|
SQLDB *sql.DB
|
||||||
JikanClient *jikan.Client
|
JikanClient *jikan.Client
|
||||||
AuthService *auth.Service
|
AuthService *auth.Service
|
||||||
|
AuthLimiter *pkgmiddleware.Limiter
|
||||||
PlaybackProxySecret string
|
PlaybackProxySecret string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,6 +43,13 @@ func withMimeTypes(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewAuthLimiter() *pkgmiddleware.Limiter {
|
||||||
|
return pkgmiddleware.NewLimiter(pkgmiddleware.Config{
|
||||||
|
MaxAttempts: 5,
|
||||||
|
Window: time.Minute,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func NewRouter(cfg Config) http.Handler {
|
func NewRouter(cfg Config) http.Handler {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
@@ -115,22 +123,12 @@ func NewRouter(cfg Config) http.Handler {
|
|||||||
mux.HandleFunc("/api/watch/episode/", playbackHandler.HandleEpisodeData)
|
mux.HandleFunc("/api/watch/episode/", playbackHandler.HandleEpisodeData)
|
||||||
mux.HandleFunc("/api/watch/thumbnails/", playbackHandler.HandleEpisodeThumbnails)
|
mux.HandleFunc("/api/watch/thumbnails/", playbackHandler.HandleEpisodeThumbnails)
|
||||||
|
|
||||||
authLimiter := pkgmiddleware.NewLimiter(pkgmiddleware.Config{
|
|
||||||
MaxAttempts: 5,
|
|
||||||
Window: time.Minute,
|
|
||||||
})
|
|
||||||
go func() {
|
|
||||||
for range time.Tick(time.Minute) {
|
|
||||||
authLimiter.Cleanup(time.Now())
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Auth Endpoints
|
// Auth Endpoints
|
||||||
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
authHandler.HandleLoginPage(w, r)
|
authHandler.HandleLoginPage(w, r)
|
||||||
} else {
|
} else {
|
||||||
authLimiter.AuthMiddleware(pkgmiddleware.VerifyOrigin(http.HandlerFunc(authHandler.HandleLogin))).ServeHTTP(w, r)
|
cfg.AuthLimiter.AuthMiddleware(pkgmiddleware.VerifyOrigin(http.HandlerFunc(authHandler.HandleLogin))).ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
mux.HandleFunc("/logout", authHandler.HandleLogout)
|
mux.HandleFunc("/logout", authHandler.HandleLogout)
|
||||||
|
|||||||
Reference in New Issue
Block a user