-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
83 lines (70 loc) · 2.44 KB
/
main.go
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
package main
import (
"log"
"net/http"
"os"
"distributed-marketplace-system/controllers"
"distributed-marketplace-system/db"
"distributed-marketplace-system/errors"
"distributed-marketplace-system/util"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func routes() {
router := gin.Default()
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AllowCredentials = true
config.AddAllowHeaders("authorization")
router.Use(cors.New(config))
// User APIs
UserRoute := router.Group("/users")
{
user := new(controllers.UserController)
UserRoute.GET("", user.GetAll)
UserRoute.GET("/:id", user.GetOne)
UserRoute.GET("/balance", util.AuthMiddleware(), user.GetBalance)
UserRoute.GET("/:id/products", user.GetProducts)
UserRoute.GET("/sold_products", util.AuthMiddleware(), user.GetSoldProducts)
UserRoute.GET("/purchased_products", util.AuthMiddleware(), user.GetPurchasedProducts)
UserRoute.GET("/report/orders", util.AuthMiddleware(), user.GetReportOnOrders)
UserRoute.GET("/report/transactions", util.AuthMiddleware(), user.GetReportOnTransactions)
UserRoute.POST("/signup", user.Signup)
UserRoute.POST("/login", user.Login)
UserRoute.POST("/balance", util.AuthMiddleware(), user.AddBalance)
UserRoute.PUT("", util.AuthMiddleware(), user.EditOne)
}
// Product APIs
ProductRoute := router.Group("/products")
{
product := new(controllers.ProductController)
ProductRoute.GET("", product.GetAll)
ProductRoute.GET("/:id", product.GetOne)
ProductRoute.GET("/search", product.SearchAll)
ProductRoute.POST("/:id/store", util.AuthMiddleware(), product.AddtoStore)
ProductRoute.POST("/:id/order", util.AuthMiddleware(), product.MakeOrder)
ProductRoute.POST("", util.AuthMiddleware(), product.AddProduct)
ProductRoute.PUT("/:id", util.AuthMiddleware(), product.EditOne)
ProductRoute.DELETE("/:id", util.AuthMiddleware(), product.DeleteOne)
}
// Store APIs
StoreRoute := router.Group("/stores")
{
store := new(controllers.StoreController)
StoreRoute.GET("", store.GetAll)
StoreRoute.GET("/:id", store.GetOne)
}
// Invalid routes handler
router.NoRoute(func(c *gin.Context) {
c.AbortWithStatusJSON(http.StatusNotFound, errors.ErrNotFound)
})
// Run the server
port := os.Getenv("PORT")
log.Printf("\n\n PORT: %s \n ENV: %s \n Version: %s \n\n", port, os.Getenv("ENV"), os.Getenv("API_VERSION"))
router.Run(":" + port)
}
func main() {
db.ConnectDatabase()
util.ConnectCloudinary()
routes()
}