-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
69 lines (57 loc) · 1.77 KB
/
main.go
File metadata and controls
69 lines (57 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"github.com/YassinNouh21/GoShopCart-Ecommerce/database"
"github.com/YassinNouh21/GoShopCart-Ecommerce/middlewares"
routers "github.com/YassinNouh21/GoShopCart-Ecommerce/routes"
"net/http"
"os"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func envPortOr(port string) string {
// If `PORT` variable in environment exists, return it
if envPort := os.Getenv("PORT"); envPort != "" {
return ":" + envPort
}
// Otherwise, return the value of `port` variable from function argument
return ":" + port
}
// initializeDB initializes the database client and collections.
func initializeDB() {
database.InitializeMongoDBCollections()
}
func main() {
initializeDB()
// Load environment variables from .env file
err := godotenv.Load(".env")
if err != nil {
// Handle error loading .env file
return
}
// Get the port from the environment variable
port := os.Getenv("PORT")
// Create a new Gin router with default middleware
router := gin.Default()
router.Use(gin.Logger())
// Initialize database client and collections
authRoutes := router.Group("/auth")
routers.GetAuthRoutes(authRoutes)
// Use Authentication middleware
router.Use(middlewares.Authentication())
// Set up user-related routes under /user and product-related routes under /product
userRoutes := router.Group("/user")
routers.ProfileRoutes(userRoutes)
routers.AddressRoutes(userRoutes)
routers.CartRoutes(userRoutes)
// Set up product-related routes under /product
productRoutes := router.Group("/product")
routers.ProductRoutes(productRoutes)
routers.ProductFilterRoutes(productRoutes)
router.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"error": "Route not defined",
})
})
// Run the server on the specified port
router.Run(":" + port)
}