-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathmain.go
More file actions
114 lines (95 loc) · 3.67 KB
/
main.go
File metadata and controls
114 lines (95 loc) · 3.67 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
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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
lkLogger "github.com/livekit/protocol/logger"
"github.com/mynaparrot/plugnmeet-protocol/logging"
"github.com/mynaparrot/plugnmeet-server/helpers"
"github.com/mynaparrot/plugnmeet-server/pkg/config"
"github.com/mynaparrot/plugnmeet-server/pkg/factory"
"github.com/mynaparrot/plugnmeet-server/pkg/routers"
"github.com/mynaparrot/plugnmeet-server/version"
"github.com/sirupsen/logrus"
)
func main() {
configFile := flag.String("config", "config.yaml", "Configuration file")
showVersion := flag.Bool("version", false, "Show version info")
flag.Parse()
if *showVersion {
fmt.Printf("%s\n", version.Version)
return
}
startServer(*configFile)
}
func startServer(configFile string) {
// Create a context that can be canceled to signal all services to shut down.
ctx, cancel := context.WithCancel(context.Background())
// Read the main configuration from the YAML file.
appCnf, err := helpers.ReadYamlConfigFile(configFile)
if err != nil {
logrus.WithError(err).Fatal("Failed to read config file")
}
// Initialize the configuration, setting default values and creating necessary directories.
appCnf, err = config.New(ctx, appCnf)
if err != nil {
logrus.WithError(err).Fatal("Failed to initialize config")
}
// Set up the structured logger (logrus) based on the configuration.
logger, err := logging.NewLogger(&appCnf.LogSettings)
if err != nil {
logrus.WithError(err).Fatal("Failed to setup logger")
}
appCnf.Logger = logger
// to avoid pion logs
logConf := &lkLogger.Config{
Level: "warn",
}
lkLogger.InitFromConfig(logConf, "pnm")
// Prepare server dependencies like database, Redis, and NATS connections.
err = helpers.PrepareServer(ctx, appCnf)
if err != nil {
logger.WithError(err).Fatalln("Failed to prepare server")
}
// Use the dependency injection container (wire) to build the main application object,
// which includes all the controllers.
appFactory, err := factory.NewAppFactory(ctx, appCnf)
if err != nil {
logger.WithError(err).Fatalln("Failed to create app factory")
}
// Boot up background services (e.g., NATS listeners, janitor for cleanup tasks).
appFactory.Boot()
// Defer the closing of connections (DB, Redis, NATS) to ensure they are closed gracefully on exit.
defer helpers.HandleCloseConnections(appFactory.AppConfig)
// Create a new Fiber router and register all the application routes.
rt := routers.New(appFactory.AppConfig, appFactory.Controllers)
// Set up a channel to listen for OS signals (like Ctrl+C) for graceful shutdown.
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
// Start a goroutine to handle the shutdown process when a signal is received.
go func() {
sig := <-sigChan
logger.WithField("signal", sig).Infoln("Exit requested, attempting graceful shutdown...")
// shut down the application
appFactory.Shutdown()
// Attempt to gracefully shut down the Fiber server, waiting for active connections to finish.
if err := rt.ShutdownWithTimeout(15 * time.Second); err != nil {
logger.WithError(err).Warn("Graceful shutdown failed, forcing exit.")
}
// Cancel the context to signal all other parts of the application (like background services) to stop.
cancel()
}()
appCnf.Logger.WithFields(logrus.Fields{
"version": version.Version,
"port": appFactory.AppConfig.Client.Port,
}).Info("starting plugNmeet server")
// Start the Fiber web server and listen for incoming HTTP requests. This is a blocking call.
err = rt.Listen(fmt.Sprintf(":%d", appFactory.AppConfig.Client.Port))
if err != nil {
logger.WithError(err).Fatalln("Failed to start server")
}
}