|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"FRMS/internal/pkg/config"
|
|
|
|
"FRMS/internal/pkg/logging"
|
|
|
|
"FRMS/internal/pkg/server"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type coordinator interface {
|
|
|
|
Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCoordinator(config *viper.Viper, ch chan error) coordinator {
|
|
|
|
return server.NewCentralCoordinator(config, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(file, path, ext string) (*viper.Viper, error) {
|
|
|
|
return config.LoadConfig(file, path, ext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
gracefulShutdown := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
userHome, err := os.UserHomeDir()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := fmt.Sprintf("%s/.config/FRMS", userHome)
|
|
|
|
configFile := "server"
|
|
|
|
configExt := "yaml"
|
|
|
|
conf, err := LoadConfig(configFile, configPath, configExt)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
c := NewCoordinator(conf, errCh)
|
|
|
|
go c.Start()
|
|
|
|
logging.Debug(logging.DStart, "CCO 01 Server %s started", conf.Get("name"))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh: // blocking to wait for any errors and keep alive otherwise
|
|
|
|
panic(err)
|
|
|
|
case <-gracefulShutdown:
|
|
|
|
// Shutdown via INT
|
|
|
|
// storing config
|
|
|
|
fmt.Printf("\nStoring config to %s\n", conf.ConfigFileUsed())
|
|
|
|
if err := conf.WriteConfig(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Stored config successfully. Exiting...")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|