@@ -34,6 +34,7 @@ func main() {
3434	}
3535	defer  dbPool .Close ()
3636
37+ 	// Instantiate repositories 
3738	userRepo  :=  users .NewPostgresUserRepository (dbPool )
3839	productRepo  :=  products .NewPostgresProductRepository (dbPool )
3940	categoryRepo  :=  categories .NewPostgresCategoryRepository (dbPool )
@@ -44,13 +45,15 @@ func main() {
4445	// Instantiate the password hasher 
4546	hasher  :=  auth .NewBcryptPasswordHasher ()
4647
47- 	// Pass the hasher to NewAuthHandler  
48+ 	// Instantiate handlers  
4849	authHandler  :=  handlers .NewAuthHandler (userRepo , hasher , cfg .JWTSecret , defaultJWTExpiry )
4950	userHandler  :=  handlers .NewUserHandler (userRepo , addressRepo )
5051	productHandler  :=  handlers .NewProductHandler (productRepo )
5152	categoryHandler  :=  handlers .NewCategoryHandler (categoryRepo )
5253	cartHandler  :=  handlers .NewCartHandler (cartRepo , productRepo )
5354	orderHandler  :=  handlers .NewOrderHandler (orderRepo , cartRepo , addressRepo )
55+ 
56+ 	// Instantiate middleware 
5457	authMiddleware  :=  auth .NewMiddleware (cfg .JWTSecret , userRepo )
5558
5659	r  :=  setupRoutes (authHandler , userHandler , productHandler , categoryHandler , cartHandler , orderHandler , authMiddleware )
@@ -60,6 +63,7 @@ func main() {
6063		port  =  defaultPort 
6164	}
6265
66+ 	// Configure HTTP server 
6367	srv  :=  & http.Server {
6468		Addr :         ":"  +  port ,
6569		Handler :      r ,
@@ -68,19 +72,23 @@ func main() {
6872		IdleTimeout :  15  *  time .Second ,
6973	}
7074
75+ 	// Setup graceful shutdown 
7176	done  :=  make (chan  os.Signal , 1 )
7277	signal .Notify (done , os .Interrupt , syscall .SIGINT , syscall .SIGTERM )
7378
79+ 	// Start server in a goroutine 
7480	go  func () {
7581		log .Printf ("Server starting on port %s" , port )
7682		if  err  :=  srv .ListenAndServe (); err  !=  nil  &&  err  !=  http .ErrServerClosed  {
7783			log .Fatalf ("listen: %s\n " , err )
7884		}
7985	}()
8086
87+ 	// Wait for shutdown signal 
8188	<- done 
8289	log .Println ("Server shutting down gracefully..." )
8390
91+ 	// Create context for shutdown 
8492	ctx , cancel  :=  context .WithTimeout (context .Background (), 30 * time .Second )
8593	defer  cancel ()
8694
@@ -91,11 +99,13 @@ func main() {
9199	log .Println ("Server exited properly" )
92100}
93101
102+ // setupRoutes configures the API routes using mux router. 
94103func  setupRoutes (ah  * handlers.AuthHandler , uh  * handlers.UserHandler , ph  * handlers.ProductHandler , ch  * handlers.CategoryHandler , cartH  * handlers.CartHandler , oh  * handlers.OrderHandler , mw  * auth.Middleware ) * mux.Router  {
95104	r  :=  mux .NewRouter ()
96105
97106	apiV1  :=  r .PathPrefix ("/api" ).Subrouter ()
98107
108+ 	// Public routes 
99109	apiV1 .HandleFunc ("/health" , handlers .HealthCheck ).Methods ("GET" )
100110	apiV1 .HandleFunc ("/auth/register" , ah .Register ).Methods ("POST" )
101111	apiV1 .HandleFunc ("/auth/login" , ah .Login ).Methods ("POST" )
@@ -104,6 +114,7 @@ func setupRoutes(ah *handlers.AuthHandler, uh *handlers.UserHandler, ph *handler
104114	apiV1 .HandleFunc ("/categories" , ch .GetAllCategories ).Methods ("GET" )
105115	apiV1 .HandleFunc ("/categories/{id:[0-9a-fA-F-]+}" , ch .GetCategory ).Methods ("GET" )
106116
117+ 	// Protected routes (grouped by resource) 
107118	protectedUserRoutes  :=  apiV1 .PathPrefix ("/users" ).Subrouter ()
108119	protectedUserRoutes .Use (mw .Authenticate )
109120	protectedUserRoutes .HandleFunc ("/me" , uh .GetMe ).Methods ("GET" )
0 commit comments