-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
149 lines (130 loc) · 3.82 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
142
143
144
145
146
147
148
149
package main
import (
"github.com/SyncYomi/SyncYomi/internal/api"
"github.com/SyncYomi/SyncYomi/internal/auth"
"github.com/SyncYomi/SyncYomi/internal/config"
"github.com/SyncYomi/SyncYomi/internal/database"
"github.com/SyncYomi/SyncYomi/internal/events"
"github.com/SyncYomi/SyncYomi/internal/http"
"github.com/SyncYomi/SyncYomi/internal/logger"
"github.com/SyncYomi/SyncYomi/internal/notification"
"github.com/SyncYomi/SyncYomi/internal/scheduler"
"github.com/SyncYomi/SyncYomi/internal/server"
"github.com/SyncYomi/SyncYomi/internal/sync"
"github.com/SyncYomi/SyncYomi/internal/update"
"github.com/SyncYomi/SyncYomi/internal/user"
"github.com/asaskevich/EventBus"
"github.com/r3labs/sse/v2"
"github.com/spf13/pflag"
"os"
"os/signal"
"syscall"
)
var (
version = "dev"
commit = ""
date = ""
)
func main() {
var configPath string
pflag.StringVar(&configPath, "config", "", "path to configuration file")
pflag.Parse()
// read config
cfg := config.New(configPath, version)
// init new logger
log := logger.New(cfg.Config)
// init dynamic config
cfg.DynamicReload(log)
// setup server-sent-events
serverEvents := sse.New()
serverEvents.AutoReplay = false
serverEvents.CreateStream("logs")
// register SSE hook on logger
log.RegisterSSEHook(serverEvents)
// setup internal eventbus
bus := EventBus.New()
// open database connection
db, _ := database.NewDB(cfg.Config, log)
if err := db.Open(); err != nil {
log.Fatal().Err(err).Msg("could not open db connection")
}
log.Info().Msgf("Starting SyncYomi")
log.Info().Msgf("Version: %s", version)
log.Info().Msgf("Commit: %s", commit)
log.Info().Msgf("Build date: %s", date)
log.Info().Msgf("Log-level: %s", cfg.Config.LogLevel)
log.Info().Msgf("Using database: %s", db.Driver)
// setup repos
var (
apikeyRepo = database.NewAPIRepo(log, db)
notificationRepo = database.NewNotificationRepo(log, db)
userRepo = database.NewUserRepo(log, db)
syncRepo = database.NewSyncRepo(log, db)
)
// setup services
var (
apiService = api.NewService(log, apikeyRepo)
notificationService = notification.NewService(log, notificationRepo)
updateService = update.NewUpdate(log, cfg.Config)
schedulingService = scheduler.NewService(log, cfg.Config, notificationService, updateService)
userService = user.NewService(userRepo)
authService = auth.NewService(log, userService)
syncService = sync.NewService(log, syncRepo, notificationService, apikeyRepo)
)
// register event subscribers
events.NewSubscribers(log, bus, notificationService)
errorChannel := make(chan error)
go func() {
httpServer := http.NewServer(
log,
cfg,
serverEvents,
db,
version,
commit,
date,
apiService,
authService,
notificationService,
updateService,
syncService,
)
errorChannel <- httpServer.Open()
}()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM)
srv := server.NewServer(log, cfg.Config, schedulingService, updateService)
if err := srv.Start(); err != nil {
log.Fatal().Stack().Err(err).Msg("could not start server")
return
}
for sig := range sigCh {
switch sig {
case syscall.SIGHUP:
log.Log().Msg("shutting down server sighup")
srv.Shutdown()
err := db.Close()
if err != nil {
log.Fatal().Stack().Err(err).Msg("could not close db connection")
return
}
os.Exit(1)
case syscall.SIGINT, syscall.SIGQUIT:
srv.Shutdown()
err := db.Close()
if err != nil {
log.Fatal().Stack().Err(err).Msg("could not close db connection")
return
}
os.Exit(1)
case syscall.SIGKILL, syscall.SIGTERM:
srv.Shutdown()
err := db.Close()
if err != nil {
log.Fatal().Stack().Err(err).Msg("could not close db connection")
return
}
os.Exit(1)
}
}
}