Skip to content

Commit 2a34e2f

Browse files
committed
update endpoints 2 eccomerce
1 parent 974d89a commit 2a34e2f

File tree

373 files changed

+167884
-231
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

373 files changed

+167884
-231
lines changed

.gitignore

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
# Go files
1+
# Environment variables
2+
.env
3+
4+
# Binaries for programs and plugins
25
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
312
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
415
*.out
5-
go.sum
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
23+
# Swagger codegen output
24+
/output/.swagger-codegen/

cmd/main.go

Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,118 @@
11
package main
22

33
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"
49
"bullet-cloud-api/internal/handlers"
10+
"bullet-cloud-api/internal/products"
11+
"bullet-cloud-api/internal/users"
12+
"context"
513
"log"
614
"net/http"
715
"os"
16+
"os/signal"
17+
"syscall"
18+
"time"
819

920
"github.com/gorilla/mux"
1021
)
1122

1223
const defaultPort = "4444"
24+
const defaultJWTExpiry = 24 * time.Hour
1325

1426
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)
1646

1747
port := os.Getenv("API_PORT")
1848
if port == "" {
19-
port = defaultPort // Porta padrão definida para maior clareza
49+
port = defaultPort
2050
}
2151

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")
2481
}
2582

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 {
2784
r := mux.NewRouter()
2885

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+
35117
return r
36118
}

go.mod

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
module bullet-cloud-api
22

3-
go 1.23
3+
go 1.23.0
4+
5+
toolchain go1.24.1
46

57
require github.com/gorilla/mux v1.8.1
8+
9+
require (
10+
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
11+
github.com/google/uuid v1.6.0 // indirect
12+
github.com/jackc/pgpassfile v1.0.0 // indirect
13+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
14+
github.com/jackc/pgx/v5 v5.7.4 // indirect
15+
github.com/jackc/puddle/v2 v2.2.2 // indirect
16+
github.com/joho/godotenv v1.5.1 // indirect
17+
golang.org/x/crypto v0.37.0 // indirect
18+
golang.org/x/sync v0.13.0 // indirect
19+
golang.org/x/text v0.24.0 // indirect
20+
)

go.sum

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
3+
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
4+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
16
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
27
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
8+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
9+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
10+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
11+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
12+
github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg=
13+
github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
14+
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
15+
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
16+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
17+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
18+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
21+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
22+
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
23+
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
24+
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
25+
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
26+
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
27+
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
28+
golang.org/x/sync v0.13.0 h1:AauUjRAJ9OSnvULf/ARrrVywoJDy0YS2AwQ98I37610=
29+
golang.org/x/sync v0.13.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
30+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
31+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
32+
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
33+
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
34+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
35+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)