-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
121 lines (96 loc) · 3.4 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
"health-record/app/server"
"health-record/db"
"health-record/helpers"
"health-record/model/properties"
"health-record/src/handler"
"health-record/src/middleware"
"health-record/src/repository"
"health-record/src/usecase"
// "log"
"os"
"github.com/gin-gonic/gin"
// "github.com/golang-migrate/migrate/v4"
// _ "github.com/golang-migrate/migrate/v4/database/postgres"
// _ "github.com/golang-migrate/migrate/v4/source/file"
)
func main() {
r := server.InitServer()
// godotenv.Load()
// 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")
// Construct the connection string
connectionString := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?%s",
dbUsername, dbPassword, dbHost, dbPort, dbName, dbParams,
)
fmt.Println("connectionString>>>>>>", connectionString)
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)
nurseRepository := repository.NewNurseRepository(db)
patientRepository := repository.NewPatientRepository(db)
// USECASE
authUsecase := usecase.NewAuthUsecase(userRepository, helper)
nurseUscase := usecase.NewNurseUsecase(nurseRepository)
patientUsecase := usecase.NewPatientUsecase(patientRepository)
// HANDLER
authHandler := handler.NewAuthHandler(authUsecase)
nurseHandler := handler.NewNurseHandler(nurseUscase)
patientHandler := handler.NewPatientHandler(patientUsecase)
imageHandler := handler.NewImageHandler()
// ROUTE
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello",
})
})
r.POST("/v1/user/it/register", authHandler.Register)
r.POST("/v1/user/it/login", authHandler.Login)
r.POST("/v1/user/nurse/login", authHandler.LoginNurse)
authorized := r.Group("")
authorized.Use(middleware.AuthMiddleware)
// IT user only
itAuthorized := authorized.Group("")
itAuthorized.Use(middleware.RoleMiddleware("it"))
itAuthorized.POST("/v1/user/nurse/register", nurseHandler.RegisterNurse)
itAuthorized.GET("/v1/user", nurseHandler.GetUsers)
itAuthorized.PUT("/v1/user/nurse/:userId", nurseHandler.UpdateNurse)
itAuthorized.DELETE("/v1/user/nurse/:userId", nurseHandler.DeleteNurse)
itAuthorized.POST("/v1/user/nurse/:userId/access", nurseHandler.AddAccess)
// Manage medical records
authorized.POST("/v1/medical/patient", patientHandler.CreatePatient)
authorized.GET("/v1/medical/patient", patientHandler.GetPatients)
authorized.POST("/v1/medical/record", patientHandler.CreateRecord)
authorized.GET("/v1/medical/record", patientHandler.GetRecords)
// upload image
authorized.POST("/v1/image", imageHandler.UploadImage)
r.Run()
}