Skip to content

Commit 21acd12

Browse files
committed
refactor: centralize environment loading in main and refine auth route rate limiting
1 parent 064f1c9 commit 21acd12

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

backend/cmd/server/cli.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010
"text/tabwriter"
1111

12-
"github.com/joho/godotenv"
1312
"github.com/waitless/waitless/internal/database"
1413
"github.com/waitless/waitless/internal/models"
1514
"github.com/waitless/waitless/internal/services"
@@ -22,8 +21,6 @@ func runCLI(args []string) {
2221
os.Exit(0)
2322
}
2423

25-
godotenv.Load()
26-
2724
switch args[0] {
2825
case "users":
2926
cmdListUsers()

backend/cmd/server/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var (
2828
)
2929

3030
func main() {
31+
// Load .env first — before anything else
32+
godotenv.Load()
33+
3134
// CLI subcommands
3235
if len(os.Args) > 1 && os.Args[1] != "serve" {
3336
runCLI(os.Args[1:])
@@ -36,9 +39,6 @@ func main() {
3639

3740
StartTime = time.Now()
3841

39-
// Load .env
40-
godotenv.Load()
41-
4242
// Structured logging
4343
logLevel := slog.LevelInfo
4444
if os.Getenv("LOG_LEVEL") == "debug" {

backend/internal/api/router.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func NewRouter(version string, startTime time.Time) http.Handler {
6060
}, status)
6161
})
6262

63-
// Public waitlist API
63+
// Public waitlist API (separate rate limiter)
6464
r.Route("/api/public", func(r chi.Router) {
6565
r.Use(httprate.LimitByIP(20, time.Minute))
6666
r.Get("/w/{slug}", GetProjectBySlug)
@@ -70,18 +70,22 @@ func NewRouter(version string, startTime time.Time) http.Handler {
7070

7171
// Auth routes
7272
r.Route("/api/auth", func(r chi.Router) {
73-
r.Use(httprate.LimitByIP(10, time.Minute))
74-
r.Post("/register", Register)
75-
r.Post("/login", Login)
73+
// Rate limit only mutation endpoints (login/register/password reset)
74+
r.Group(func(r chi.Router) {
75+
r.Use(httprate.LimitByIP(10, time.Minute))
76+
r.Post("/register", Register)
77+
r.Post("/login", Login)
78+
r.Post("/forgot-password", ForgotPassword)
79+
r.Post("/reset-password", ResetPassword)
80+
})
81+
// Session endpoints — no rate limit (polled by dashboard)
7682
r.Post("/logout", Logout)
77-
r.Post("/forgot-password", ForgotPassword)
78-
r.Post("/reset-password", ResetPassword)
7983
r.With(authmw.AuthRequired).Get("/me", Me)
8084
r.With(authmw.AuthRequired).Put("/me", UpdateProfile)
8185
r.With(authmw.AuthRequired).Put("/me/password", ChangePassword)
8286
})
8387

84-
// Dashboard API (authenticated)
88+
// Dashboard API (authenticated, no shared rate limit with public)
8589
r.Route("/api/dashboard", func(r chi.Router) {
8690
r.Use(authmw.AuthRequired)
8791

0 commit comments

Comments
 (0)