This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
141 lines (124 loc) · 3.45 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"fmt"
"github.com/GoldenOwlAsia/golang-api-template/configs"
"github.com/GoldenOwlAsia/golang-api-template/infras"
"github.com/GoldenOwlAsia/golang-api-template/middleware"
"github.com/GoldenOwlAsia/golang-api-template/migrations"
"github.com/GoldenOwlAsia/golang-api-template/router"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
_ "github.com/swaggo/files"
_ "github.com/swaggo/gin-swagger"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"net/http"
"os"
"os/signal"
)
var (
DB *gorm.DB
Gin *gin.Engine
)
// @title GoldenOwl Gin API
// @version 1.0.0
// @description This API is for GoldenOwl API application
// @contact.name goldenowl.asia
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /
// @securityDefinitions.basic BasicAuth
func main() {
InitEnv()
InitDB()
InitGin()
InitSentry()
migrations.Migrate(DB)
migrations.Seed(DB)
Gin.Use(middleware.Cors())
Gin.Use(middleware.HandleResponse)
Gin.Use(sentrygin.New(sentrygin.Options{Repanic: true}))
appHandler := infras.DI(DB)
Gin = router.InitRouter(Gin, appHandler, DB)
SpinUp(" 🚀 ")
}
func InitEnv() {
var err error
_, err = configs.LoadConfig(".", ".env")
if err != nil {
log.Fatal("cannot load configs: ", err)
}
}
func InitDB() {
dsn := getDSN()
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: func() logger.Interface {
if configs.ConfApp.AppMode == gin.DebugMode {
return logger.Default.LogMode(logger.Info)
}
return logger.Default.LogMode(logger.Silent)
}(),
})
if err != nil {
log.Println("cannot connect to database: ", err)
}
DB = db
}
func getDSN() (dsn string) {
username := configs.ConfApp.DatabaseUserName
password := configs.ConfApp.DatabasePassword
host := configs.ConfApp.DatabaseHost
port := configs.ConfApp.DatabasePort
databaseName := configs.ConfApp.DatabaseName
timeZone := configs.ConfApp.DatabaseTimezone
dsn = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=%s", host, username, password, databaseName, port, timeZone)
return
}
func InitGin() {
g := gin.Default()
Gin = g
gin.SetMode(configs.ConfApp.AppMode)
}
func InitSentry() {
var err error
if err = sentry.Init(sentry.ClientOptions{
Dsn: configs.ConfApp.SentryDNS,
EnableTracing: true,
Environment: configs.ConfApp.AppMode,
TracesSampleRate: 1.0,
}); err != nil {
log.Printf("Sentry initialization failed: %v\n", err)
}
}
func SpinUp(msg string) (server *http.Server) {
// -- Graceful restart or stop server --
server = &http.Server{
ReadHeaderTimeout: configs.ReadHeaderTimeout,
Addr: fmt.Sprintf(":%s", configs.ConfApp.Port), // + configs.ConfApp.Port
Handler: Gin,
}
fmt.Printf("[GoldenOwl API - %s] Start to listening the incoming requests on http address: %s", gin.Mode(), server.Addr)
fmt.Println(msg)
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
go func() {
<-quit
log.Println("receive interrupt signal")
if err := server.Close(); err != nil {
log.Fatal("Server Close:", err)
}
}()
if err := server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
log.Println("Server closed under request")
} else {
log.Fatal("Server closed unexpect")
}
}
log.Println("Server exiting")
return server
}