-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (70 loc) · 1.71 KB
/
main.go
File metadata and controls
86 lines (70 loc) · 1.71 KB
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
package main
import (
"os"
"os/signal"
"runtime/debug"
"syscall"
"github.com/Sn0wo2/QuickNote/internal/listener"
"github.com/Sn0wo2/QuickNote/internal/router"
"github.com/Sn0wo2/QuickNote/internal/setup"
"github.com/Sn0wo2/QuickNote/pkg/config"
"github.com/Sn0wo2/QuickNote/pkg/database/orm"
"github.com/Sn0wo2/QuickNote/pkg/database/table"
"github.com/Sn0wo2/QuickNote/pkg/log"
"github.com/Sn0wo2/QuickNote/pkg/version"
"github.com/gofiber/fiber/v2"
"github.com/joho/godotenv"
"go.uber.org/zap"
)
func init() {
// stw
debug.SetGCPercent(50)
_ = godotenv.Load()
err := config.Init()
if err != nil {
// log not init~
panic(err)
}
log.Init()
}
func main() {
defer func() {
_ = log.Instance.Sync()
}()
if !fiber.IsChild() {
log.Instance.Info("Starting QuickNote...", zap.String("version", version.GetFormatVersion()))
}
if err := orm.Init(config.Instance.Database.Type, config.Instance.Database.URL); err != nil {
log.Instance.Fatal("Failed to initialize database",
zap.Error(err),
)
}
if err := table.Init(); err != nil {
log.Instance.Fatal("Failed to initialize tables",
zap.Error(err),
)
}
if !fiber.IsChild() {
log.Instance.Info("Starting server",
zap.String("address", config.Instance.Listener.Address),
zap.Int("pid", os.Getpid()),
)
}
app := setup.Fiber()
router.Setup(app)
shutdownChan := make(chan os.Signal, 1)
signal.Notify(shutdownChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
if err := listener.Start(app); err != nil {
log.Instance.Fatal("Server failed to start",
zap.Error(err),
)
}
}()
<-shutdownChan
if err := app.Shutdown(); err != nil {
log.Instance.Error("Server shutdown error",
zap.Error(err),
)
}
}