FlexDXCluster/main.go

91 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-09-23 16:24:22 +07:00
package main
import (
"flag"
"log"
2024-09-26 12:24:56 +07:00
"os"
"os/signal"
"syscall"
2024-09-23 16:24:22 +07:00
)
func ParseFlags() (string, error) {
// String that contains the configured configuration path
var configPath string
// Set up a CLI flag called "-config" to allow users
// to supply the configuration file
flag.StringVar(&configPath, "config", "./config.yml", "path to config file")
// Actually parse the flags
flag.Parse()
// Validate the path first
if err := ValidateConfigPath(configPath); err != nil {
return "", err
}
// Return the configuration path
return configPath, nil
}
func main() {
// Generate our config based on the config supplied
// by the user in the flags
cfgPath, err := ParseFlags()
if err != nil {
log.Fatal(err)
}
err = NewConfig(cfgPath)
if err != nil {
log.Fatal(err)
}
2024-09-26 19:14:29 +07:00
log := NewLog()
2024-09-23 16:24:22 +07:00
log.Info("config loaded.")
2024-09-24 11:57:48 +07:00
log.Infof("Callsign: %s", Cfg.SQLite.Callsign)
2024-09-23 16:24:22 +07:00
DeleteDatabase("./flex.sqlite", log)
2024-09-26 12:24:56 +07:00
fRepo := NewFlexDXDatabase("flex.sqlite", log)
defer fRepo.db.Close()
cRepo := NewLog4OMContactsRepository(Cfg.SQLite.SQLitePath, log)
defer cRepo.db.Close()
2024-09-23 16:24:22 +07:00
TCPServer := NewTCPServer(Cfg.Telnet.Host, Cfg.Telnet.Port, log)
2024-09-26 12:24:56 +07:00
FlexClient := NewFlexClient(*fRepo, *TCPServer, log)
2024-09-24 11:57:48 +07:00
TCPClient := NewTCPClient(*Cfg, TCPServer, FlexClient, log)
2024-09-26 15:23:05 +07:00
HTTPServer := NewHTTPServer(*cRepo, *fRepo, *FlexClient, log)
2024-09-26 12:24:56 +07:00
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
2024-09-23 16:24:22 +07:00
go FlexClient.StartFlexClient()
2024-09-24 11:57:48 +07:00
go TCPClient.StartClient()
2024-09-23 16:24:22 +07:00
go TCPServer.StartServer()
2024-09-26 12:24:56 +07:00
go HTTPServer.StartHTTPServer()
for sig := range sigCh {
log.Infof("received signal: %v, shutting down TCP Client.", sig)
TCPClient.Close()
if err := fRepo.db.Close(); err != nil {
log.Error("failed to close the database connection properly")
os.Exit(1)
}
if err := cRepo.db.Close(); err != nil {
log.Error("failed to close Log4OM database connection properly")
os.Exit(1)
}
os.Exit(0)
}
2024-09-23 16:24:22 +07:00
}