-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
83 lines (69 loc) · 1.8 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 (
appMiddleware "goshaka/app/middlewares"
apiRoutes "goshaka/app/routes"
appConfig "goshaka/configs"
appDatabase "goshaka/database"
appHelper "goshaka/helpers"
"goshaka/jobs"
"github.com/gofiber/fiber/v2"
)
// @title Goshaka Golang API Starter
// @version 1.0
// @Description This is an API boilerplate using Golang
// @contact.name Aulia Wiguna
// @contact.url https://github.com/auliawiguna/
// @contact.email [email protected]
// @BasePath /
// @schemas http
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description Type "Bearer" followed by a space and JWT token.
func main() {
config := appConfig.FiberConfig()
// Define a new Fiber app with config.
app := fiber.New(config)
// Database
errConn := appDatabase.Connect()
if errConn != nil {
panic("cannot connect database: " + errConn.Error())
}
db, err := appDatabase.DB.DB()
if err != nil {
errc := db.Close()
if errc != nil {
panic("cannot connect database and close the connection")
}
panic("cannot connect database: " + errc.Error())
}
// Migrate
errM := appDatabase.MigrateDatabase()
if errM != nil {
panic("migration error: " + errM.Error())
}
defer db.Close()
// Initiate Redis
errRds := appDatabase.RedisConnect()
if errRds != nil {
panic("cannot start redis connection: " + errRds.Error())
}
// Initiate S3 Session
erraws := appHelper.StartAwsSession()
if erraws != nil {
panic("cannot start aws session: " + erraws.Error())
}
// Apply default middleware
appMiddleware.DefaultMiddleware(app)
// Router
apiRoutes.MainRoutes(app)
apiRoutes.ApiRoutes(app)
apiRoutes.StaticFile(app)
// Run cronjob
jobs.RunCron()
if appConfig.GetEnv("ENV") == "dev" {
appHelper.StartServer(app)
} else {
appHelper.StartServerWithGracefulShutdown(app)
}
}