-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (95 loc) · 2.94 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
package main
import (
"fmt"
"time"
"github.com/CUBS-sources-code/CUBS-coin/handler"
"github.com/CUBS-sources-code/CUBS-coin/logs"
"github.com/CUBS-sources-code/CUBS-coin/repository"
"github.com/CUBS-sources-code/CUBS-coin/service"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func main() {
// Load config
initConfig()
// Set timezone
initTimeZone()
// Connect database
db := initDatabase()
// Init fiber app
app := initApp()
app.Use(cors.New())
api := app.Group("/api")
userRepository := repository.NewUserRepositoryDB(db)
userService := service.NewUserService(userRepository)
userHandler := handler.NewUserHandler(userService)
transactionRepository := repository.NewTransactionRepositoryDB(db)
transactionService := service.NewTransactionService(transactionRepository, userRepository)
transactionHandler := handler.NewTransactionHandler(transactionService)
authService := service.NewAuthService(userRepository)
authHandler := handler.NewAuthHandler(authService)
// Public
api.Get("/transactions", transactionHandler.GetTransactions)
api.Get("/transaction/:id", transactionHandler.GetTransaction)
api.Post("/signup", authHandler.SignUp)
api.Post("/signin", authHandler.SignIn)
api.Get("/user/:student_id", userHandler.GetUser)
// Private
api.Use(authHandler.AuthorizationRequired())
api.Get("/user", userHandler.GetMyUser)
api.Post("/transfer", transactionHandler.Transfer)
// Admin
api.Use(authHandler.IsAdmin)
api.Post("/transaction/create", transactionHandler.CreateTransaction)
api.Post("/user/create", userHandler.CreateUser)
api.Get("/users", userHandler.GetUsers)
api.Patch("/changetoadmin/:student_id", userHandler.ChangeRoleToAdmin)
api.Patch("/changetomember/:student_id", userHandler.ChangeRoleToMember)
// Start server
logs.Info("CUBS coin service started at port " + viper.GetString("app.port"))
app.Listen(fmt.Sprintf(":%v", viper.GetInt("app.port")))
}
func initConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
}
func initDatabase() *gorm.DB {
dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?parseTime=true",
viper.GetString("db.username"),
viper.GetString("db.password"),
viper.GetString("db.host"),
viper.GetInt("db.port"),
viper.GetString("db.database"))
dial := mysql.Open(dsn)
db, err := gorm.Open(dial, &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
TranslateError: true,
})
if err != nil {
panic(err)
}
return db
}
func initTimeZone() {
ict, err := time.LoadLocation("Asia/Bangkok")
if err != nil {
panic(err)
}
time.Local = ict
}
func initApp() *fiber.App {
return fiber.New(fiber.Config{
DisableStartupMessage: true,
Prefork: viper.GetBool("app.prefork"),
AppName: viper.GetString("app.name"),
})
}