-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
117 lines (94 loc) · 6.44 KB
/
Copy pathmain.go
File metadata and controls
117 lines (94 loc) · 6.44 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/theinvincible/ecommerce-backend/config"
"github.com/theinvincible/ecommerce-backend/handlers"
"github.com/theinvincible/ecommerce-backend/partition"
"github.com/theinvincible/ecommerce-backend/utils"
)
func main() {
log.Println("Connecting to database...")
config.ConnectDatabase()
// config.ReinitializeDatabase()
// Check if the database connection is established
if config.DB == nil {
log.Fatal("Database connection failed")
}
// Set up router
router := mux.NewRouter()
//Login routes
router.HandleFunc("/api/v1/signup", handlers.SignUp).Methods("POST")
router.HandleFunc("/api/v1/login", handlers.Login).Methods("POST")
// User routes
router.HandleFunc("/api/v1/users", handlers.CreateUser).Methods("POST")
router.HandleFunc("/api/v1/users", handlers.GetUsers).Methods("GET")
router.HandleFunc("/api/v1/users/{id}", handlers.GetUser).Methods("GET")
router.HandleFunc("/api/v1/users/{id}", handlers.UpdateUser).Methods("PUT")
router.HandleFunc("/api/v1/users/{id}", handlers.DeleteUser).Methods("DELETE")
// Product routes
router.HandleFunc("/api/v1/products", handlers.CreateProduct).Methods("POST")
router.HandleFunc("/api/v1/products", handlers.GetProducts).Methods("GET")
router.HandleFunc("/api/v1/products/{id}", handlers.GetProductByID).Methods("GET")
router.HandleFunc("/api/v1/products/{id}", handlers.UpdateProduct).Methods("PUT")
// router.HandleFunc("/api/v1/products/{id}", handlers.DeleteProduct(db)).Methods("DELETE")
// Order routes
router.HandleFunc("/api/v1/addorders", handlers.CreateOrderHandler(config.DB)).Methods("POST")
router.HandleFunc("/api/v1/orders", handlers.GetOrders).Methods("GET")
router.HandleFunc("/api/v1/orders/{id}", handlers.GetOrder).Methods("GET")
router.HandleFunc("/api/v1/orders/{id}", handlers.UpdateOrder).Methods("PUT")
router.HandleFunc("/api/v1/orders/{id}", handlers.DeleteOrder).Methods("DELETE")
// Category routes
router.HandleFunc("/api/v1/categories", handlers.CreateCategory).Methods("POST")
router.HandleFunc("/api/v1/categories", handlers.GetCategories).Methods("GET")
router.HandleFunc("/api/v1/categories/{id}", handlers.GetCategory).Methods("GET")
router.HandleFunc("/api/v1/categories/{id}", handlers.UpdateCategory).Methods("PUT")
router.HandleFunc("/api/v1/categories/{id}", handlers.DeleteCategory).Methods("DELETE")
// Cart routes
router.HandleFunc("/api/v1/cart", handlers.CreateCart).Methods("POST")
router.HandleFunc("/api/v1/cart/{id}", handlers.GetCart).Methods("GET")
router.HandleFunc("/api/v1/cart/{id}", handlers.UpdateCart).Methods("PUT")
router.HandleFunc("/api/v1/cart/{id}", handlers.DeleteCart).Methods("DELETE")
// Payment routes
router.HandleFunc("/api/v1/payment", handlers.PaymentHandler).Methods("POST")
router.HandleFunc("/api/v1/webhook", handlers.WebhookHandler).Methods("POST")
// Checkout routes
router.HandleFunc("/api/v1/checkout", handlers.CheckoutHandler(config.DB)).Methods("POST")
router.HandleFunc("/api/v1/order/confirm/{orderID}", handlers.OrderConfirmationHandler(config.DB)).Methods("POST")
router.HandleFunc("/api/v1/store-device-token", handlers.StoreTokenHandler).Methods("POST")
//<=====================================================MIddleware routes=====================================================>
router.HandleFunc("/api/v1/admin", partition.AdminHandler).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/dashboard", partition.AdminDashboardHandler).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/users", partition.GetUsersHandler).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/users/{id}", partition.UpdateUserHandler).Methods("POST").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/users/{id}", partition.DeleteUserHandler).Methods("DELETE").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/products", partition.AddProductHandler).Methods("POST").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/products/{id}", partition.UpdateProductHandler).Methods("POST").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/products/{id}", partition.DeleteProductHandler).Methods("DELETE").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/orders", partition.GetOrdersHandler).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/orders/{id}", partition.UpdateOrderStatusHandler).Methods("POST").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/admin/categories", partition.AssignRoleHandler).Methods("POST").Subrouter().Use(handlers.RoleMiddleware("admin"))
router.HandleFunc("/api/v1/vendor", partition.VendorHandler).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("vendor"))
router.HandleFunc("/api/v1/login/{id}", partition.LoginVendor).Methods("POST").Subrouter().Use(handlers.RoleMiddleware("vendor"))
router.HandleFunc("/api/v1/vendor/products", partition.AddProduct).Methods("POST").Subrouter().Use(handlers.RoleMiddleware("vendor"))
router.HandleFunc("/api/v1/vendor/products/{id}", partition.UpdateProduct).Methods("PUT").Subrouter().Use(handlers.RoleMiddleware("vendor"))
router.HandleFunc("/api/v1vendor/products/{id}", partition.DeleteProduct).Methods("DELETE").Subrouter().Use(handlers.RoleMiddleware("vendor"))
router.HandleFunc("/api/v1/vendor/orders", partition.GetOrders).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("vendor"))
router.HandleFunc("/api/v1/vendor/orders/{id}", partition.DeleteOrder).Methods("DELETE").Subrouter().Use(handlers.RoleMiddleware("vendor"))
router.HandleFunc("/api/v1/vendor/{id}", partition.GetSalesData).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("vendor"))
// router.HandleFunc("/customer", CustomerHandler).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("customer"))
// router.HandleFunc("/dashboard", DashboardHandler).Methods("GET").Subrouter().Use(handlers.RoleMiddleware("admin", "vendor"))
rdb := utils.InitRedisClient()
if rdb == nil {
log.Fatal("Failed to initialize Redis client")
} else {
log.Println("Redis client initialized successfully")
}
err := http.ListenAndServe(":3001", router)
if err != nil {
log.Fatal("Error starting server:", err)
}
fmt.Println("Server is running on port 3001")
}