-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (86 loc) · 2.86 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"cats-social/app/server"
"cats-social/db"
"cats-social/helpers"
"cats-social/model/properties"
"cats-social/src/handler"
"cats-social/src/middleware"
"cats-social/src/repository"
"cats-social/src/usecase"
"fmt"
"os"
"github.com/gin-gonic/gin"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/joho/godotenv"
)
func main() {
r := server.InitServer()
err := godotenv.Load()
if err != nil {
fmt.Println("Error loading .env file:", err)
return
}
dbName := os.Getenv("DB_NAME")
dbPort := os.Getenv("DB_PORT")
dbHost := os.Getenv("DB_HOST")
dbUsername := os.Getenv("DB_USERNAME")
dbPassword := os.Getenv("DB_PASSWORD")
dbParams := os.Getenv("DB_PARAMS")
connectionString := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?%s",
dbUsername, dbPassword, dbHost, dbPort, dbName, dbParams,
)
postgreConfig := properties.PostgreConfig{
DatabaseURL: connectionString,
}
db := db.InitPostgreDB(postgreConfig)
// run migrations
// m, err := migrate.New(os.Getenv("MIGRATION_PATH"), os.Getenv("DATABASE_URL"))
// if err != nil {
// log.Fatal("Error creating migration instance: ", err)
// }
// Run the migration up to the latest version
// if err := m.Up(); err != nil && err != migrate.ErrNoChange {
// log.Fatal("Error applying migrations:", err)
// }
// fmt.Println("Migration successfully applied")
helper := helpers.NewHelper()
// MIDDLEWARE
middleware := middleware.NewMiddleware(helper)
// REPOSITORY
userRepository := repository.NewUserRepository(db)
matchRepository := repository.NewMatchRepository(db)
catRepository := repository.NewCatRepository(db)
// USECASE
authUsecase := usecase.NewAuthUsecase(userRepository, helper)
matchUseCase := usecase.NewMatchUsecase(matchRepository, helper)
catUsecase := usecase.NewCatUsecase(catRepository)
// HANDLER
catHandler := handler.NewCatHandler(catUsecase)
authHandler := handler.NewAuthHandler(authUsecase)
matchHandler := handler.NewMatchHandler(matchUseCase)
// ROUTE
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello",
})
})
r.POST("/v1/user/register", authHandler.Register)
r.POST("/v1/user/login", authHandler.Login)
authorized := r.Group("")
authorized.Use(middleware.AuthMiddleware)
// CAT
authorized.POST("/v1/cat", catHandler.AddCat)
authorized.GET("/v1/cat", catHandler.GetCat)
authorized.PUT("/v1/cat/:id", catHandler.UpdateCat)
authorized.DELETE("/v1/cat/:id", catHandler.DeleteCat)
// MATCH
authorized.POST("/v1/cat/match", matchHandler.CreateMatch)
authorized.GET("/v1/cat/match", matchHandler.GetMatch)
authorized.DELETE("/v1/cat/match/:id", matchHandler.DeleteMatch)
authorized.POST("/v1/cat/match/approve", matchHandler.ApproveMatch)
authorized.POST("/v1/cat/match/reject", matchHandler.RejectMatch)
r.Run()
}