-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (50 loc) · 1.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
package main
import (
_ "github.com/lib/pq"
"github.com/piotrsenkow/gosyncmls/api"
"github.com/piotrsenkow/gosyncmls/cmd"
"github.com/piotrsenkow/gosyncmls/database"
"github.com/piotrsenkow/gosyncmls/services"
"github.com/piotrsenkow/gosyncmls/utils"
"os"
"os/signal"
"syscall"
)
// setupSignalHandlers is a helper function that sets up signal handlers for the program.
func setupSignalHandlers() {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-signals
utils.LogEvent("info", "Received signal: "+sig.String())
// Close database and API connections
err := database.Db.Close()
if err != nil {
utils.LogEvent("trace", "Trace: "+err.Error())
}
os.Exit(0)
}()
}
// initializeDependencies is a helper function that initializes all dependencies for the program.
func initializeDependencies() {
// Initialize logger
utils.InitializeLogger()
// Initialize the HTTP client
api.InitializeHttpClient()
// Initialize limiters
services.InitializeRateLimiter()
//// Initialize the database connection
_, err := database.InitializeDb()
if err != nil {
utils.LogEvent("fatal", "Failed to connect to the database: "+err.Error())
os.Exit(1)
}
// Initialize how program handles termination signals
setupSignalHandlers()
// Instantiate and assign the global rate tracker
services.GlobalRateTracker = services.NewRateTracker()
}
func main() {
initializeDependencies()
cmd.Execute()
}