|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bullet-cloud-api/internal/addresses" |
| 5 | + "bullet-cloud-api/internal/auth" |
| 6 | + "bullet-cloud-api/internal/categories" |
| 7 | + "bullet-cloud-api/internal/config" |
| 8 | + "bullet-cloud-api/internal/database" |
4 | 9 | "bullet-cloud-api/internal/handlers" |
| 10 | + "bullet-cloud-api/internal/products" |
| 11 | + "bullet-cloud-api/internal/users" |
| 12 | + "context" |
5 | 13 | "log" |
6 | 14 | "net/http" |
7 | 15 | "os" |
| 16 | + "os/signal" |
| 17 | + "syscall" |
| 18 | + "time" |
8 | 19 |
|
9 | 20 | "github.com/gorilla/mux" |
10 | 21 | ) |
11 | 22 |
|
12 | 23 | const defaultPort = "4444" |
| 24 | +const defaultJWTExpiry = 24 * time.Hour |
13 | 25 |
|
14 | 26 | func main() { |
15 | | - r := setupRoutes() |
| 27 | + cfg := config.Load() |
| 28 | + |
| 29 | + dbPool, err := database.NewConnection(cfg.DatabaseURL) |
| 30 | + if err != nil { |
| 31 | + log.Fatalf("Could not connect to the database: %v", err) |
| 32 | + } |
| 33 | + defer dbPool.Close() |
| 34 | + |
| 35 | + userRepo := users.NewPostgresUserRepository(dbPool) |
| 36 | + productRepo := products.NewPostgresProductRepository(dbPool) |
| 37 | + categoryRepo := categories.NewPostgresCategoryRepository(dbPool) |
| 38 | + addressRepo := addresses.NewPostgresAddressRepository(dbPool) |
| 39 | + authHandler := handlers.NewAuthHandler(userRepo, cfg.JWTSecret, defaultJWTExpiry) |
| 40 | + userHandler := handlers.NewUserHandler(userRepo, addressRepo) |
| 41 | + productHandler := handlers.NewProductHandler(productRepo) |
| 42 | + categoryHandler := handlers.NewCategoryHandler(categoryRepo) |
| 43 | + authMiddleware := auth.NewMiddleware(cfg.JWTSecret, userRepo) |
| 44 | + |
| 45 | + r := setupRoutes(authHandler, userHandler, productHandler, categoryHandler, authMiddleware) |
16 | 46 |
|
17 | 47 | port := os.Getenv("API_PORT") |
18 | 48 | if port == "" { |
19 | | - port = defaultPort // Porta padrão definida para maior clareza |
| 49 | + port = defaultPort |
20 | 50 | } |
21 | 51 |
|
22 | | - log.Printf("Server starting on :%s", port) |
23 | | - log.Fatal(http.ListenAndServe(":"+port, r)) |
| 52 | + srv := &http.Server{ |
| 53 | + Addr: ":" + port, |
| 54 | + Handler: r, |
| 55 | + ReadTimeout: 5 * time.Second, |
| 56 | + WriteTimeout: 10 * time.Second, |
| 57 | + IdleTimeout: 15 * time.Second, |
| 58 | + } |
| 59 | + |
| 60 | + done := make(chan os.Signal, 1) |
| 61 | + signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) |
| 62 | + |
| 63 | + go func() { |
| 64 | + log.Printf("Server starting on port %s", port) |
| 65 | + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 66 | + log.Fatalf("listen: %s\n", err) |
| 67 | + } |
| 68 | + }() |
| 69 | + |
| 70 | + <-done |
| 71 | + log.Println("Server shutting down gracefully...") |
| 72 | + |
| 73 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 74 | + defer cancel() |
| 75 | + |
| 76 | + if err := srv.Shutdown(ctx); err != nil { |
| 77 | + log.Fatalf("Server shutdown failed: %+v", err) |
| 78 | + } |
| 79 | + |
| 80 | + log.Println("Server exited properly") |
24 | 81 | } |
25 | 82 |
|
26 | | -func setupRoutes() *mux.Router { |
| 83 | +func setupRoutes(ah *handlers.AuthHandler, uh *handlers.UserHandler, ph *handlers.ProductHandler, ch *handlers.CategoryHandler, mw *auth.Middleware) *mux.Router { |
27 | 84 | r := mux.NewRouter() |
28 | 85 |
|
29 | | - r.HandleFunc("/health/{id}", handlers.HealthCheck).Methods("GET") |
30 | | - r.HandleFunc("/products", handlers.GetAllProducts).Methods("GET") |
31 | | - r.HandleFunc("/products", handlers.CreateProduct).Methods("POST") |
32 | | - r.HandleFunc("/products/{id}", handlers.GetProduct).Methods("GET") |
33 | | - r.HandleFunc("/products/{id}", handlers.UpdateProduct).Methods("PUT") |
34 | | - r.HandleFunc("/products/{id}", handlers.DeleteProduct).Methods("DELETE") |
| 86 | + apiV1 := r.PathPrefix("/api").Subrouter() |
| 87 | + |
| 88 | + apiV1.HandleFunc("/health", handlers.HealthCheck).Methods("GET") |
| 89 | + apiV1.HandleFunc("/auth/register", ah.Register).Methods("POST") |
| 90 | + apiV1.HandleFunc("/auth/login", ah.Login).Methods("POST") |
| 91 | + apiV1.HandleFunc("/products", ph.GetAllProducts).Methods("GET") |
| 92 | + apiV1.HandleFunc("/products/{id:[0-9a-fA-F-]+}", ph.GetProduct).Methods("GET") |
| 93 | + apiV1.HandleFunc("/categories", ch.GetAllCategories).Methods("GET") |
| 94 | + apiV1.HandleFunc("/categories/{id:[0-9a-fA-F-]+}", ch.GetCategory).Methods("GET") |
| 95 | + |
| 96 | + protectedUserRoutes := apiV1.PathPrefix("/users").Subrouter() |
| 97 | + protectedUserRoutes.Use(mw.Authenticate) |
| 98 | + protectedUserRoutes.HandleFunc("/me", uh.GetMe).Methods("GET") |
| 99 | + protectedUserRoutes.HandleFunc("/{userId:[0-9a-fA-F-]+}/addresses", uh.ListAddresses).Methods("GET") |
| 100 | + protectedUserRoutes.HandleFunc("/{userId:[0-9a-fA-F-]+}/addresses", uh.AddAddress).Methods("POST") |
| 101 | + protectedUserRoutes.HandleFunc("/{userId:[0-9a-fA-F-]+}/addresses/{addressId:[0-9a-fA-F-]+}", uh.UpdateAddress).Methods("PUT") |
| 102 | + protectedUserRoutes.HandleFunc("/{userId:[0-9a-fA-F-]+}/addresses/{addressId:[0-9a-fA-F-]+}", uh.DeleteAddress).Methods("DELETE") |
| 103 | + protectedUserRoutes.HandleFunc("/{userId:[0-9a-fA-F-]+}/addresses/{addressId:[0-9a-fA-F-]+}/default", uh.SetDefaultAddress).Methods("PATCH") |
| 104 | + |
| 105 | + protectedProductRoutes := apiV1.PathPrefix("/products").Subrouter() |
| 106 | + protectedProductRoutes.Use(mw.Authenticate) |
| 107 | + protectedProductRoutes.HandleFunc("", ph.CreateProduct).Methods("POST") |
| 108 | + protectedProductRoutes.HandleFunc("/{id:[0-9a-fA-F-]+}", ph.UpdateProduct).Methods("PUT") |
| 109 | + protectedProductRoutes.HandleFunc("/{id:[0-9a-fA-F-]+}", ph.DeleteProduct).Methods("DELETE") |
| 110 | + |
| 111 | + protectedCategoryRoutes := apiV1.PathPrefix("/categories").Subrouter() |
| 112 | + protectedCategoryRoutes.Use(mw.Authenticate) |
| 113 | + protectedCategoryRoutes.HandleFunc("", ch.CreateCategory).Methods("POST") |
| 114 | + protectedCategoryRoutes.HandleFunc("/{id:[0-9a-fA-F-]+}", ch.UpdateCategory).Methods("PUT") |
| 115 | + protectedCategoryRoutes.HandleFunc("/{id:[0-9a-fA-F-]+}", ch.DeleteCategory).Methods("DELETE") |
| 116 | + |
35 | 117 | return r |
36 | 118 | } |
0 commit comments