package main import ( "fmt" "os/signal" "syscall" "FRMS/internal/pkg/config" "FRMS/internal/pkg/logging" "FRMS/internal/pkg/server" "FRMS/internal/pkg/websocket" "os" "github.com/spf13/viper" ) type coordinator interface { Start() } func NewCoordinator(config *viper.Viper, ch chan error) coordinator { return server.NewCentralCoordinator(config, ch) } func NewConfig(filename string) (*viper.Viper, error) { return config.LoadConfig(filename) } type ws interface { Start() } func NewWebSocket() ws { return websocket.New() } func main() { // lets get this bread gracefulShutdown := make(chan os.Signal, 1) signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM) // config file conf, err := NewConfig("server") if err != nil { panic(err) } errCh := make(chan error) c := NewCoordinator(conf, errCh) go c.Start() fmt.Printf("Coordiantor started\n") logging.Debug(logging.DStart, "CCO %s started", conf.Get("name")) // starting websocket server // gated behind env for testing if conf.Get("WEBSOCKET") == "start" { w := NewWebSocket() go w.Start() fmt.Printf("Websocket started\n") logging.Debug(logging.DStart, "WS websocket started") } select { case err := <-errCh: // allows passing errors up for handling panic(err) case <-gracefulShutdown: // Shutdown via INT fmt.Printf("\nExiting...\n") logging.Debug(logging.DStop, "CON storing to %s", conf.ConfigFileUsed()) if err := conf.WriteConfig(); err != nil { panic(err) } logging.Debug(logging.DStop, "CON stored successfully", conf.ConfigFileUsed()) os.Exit(0) } }