-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
49 lines (42 loc) · 1.84 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
package main
import (
"log"
"os"
"github.com/vviia/golang-ecomerce/controllers"
"github.com/vviia/golang-ecomerce/database"
"github.com/vviia/golang-ecomerce/middleware"
"github.com/vviia/golang-ecomerce/routes"
"github.com/gin-gonic/gin"
)
func main() {
// This is good and follows the advice of: https://12factor.net/config.
// But you should do this for all config: mongodb (credentials, database, collections), SECRET_KEY in tokengen.go.
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
// This is still a bad way of dependency injection because I would break a
// lot of your code if I would do it properly. You want to create your
// database connection in your main.go file and give the database client
// to the database.ProductData and database.UserData functions.
app := controllers.NewApplication(database.ProductData(database.Client, "Products"), database.UserData(database.Client, "Users"))
router := gin.New()
router.Use(gin.Logger())
routes.UserRoutes(router)
// The authentication middleware is applied to all routes, including the /users/signup route. So nobody can actually use the application.
router.Use(middleware.Authentication())
// Your routes are inconsistent starting with and without '/'.
router.GET("/addtocart", app.AddToCart())
router.GET("/removeitem", app.RemoveItem())
router.GET("/listcart", controllers.GetItemFromCart())
router.POST("/addaddress", controllers.AddAddress())
router.PUT("/edithomeaddress", controllers.EditHomeAddress())
router.PUT("/editworkaddress", controllers.EditWorkAddress())
router.GET("/deleteaddresses", controllers.DeleteAddress())
router.GET("/cartcheckout", app.BuyFromCart())
router.GET("/instantbuy", app.InstantBuy())
//router.GET("logout", controllers.Logout())
//break :)
// Log the error that the router can possibly return.
log.Fatal(router.Run(":" + port))
}