package main import ( "fmt" _ "net/http" _ "net/http/pprof" "os/signal" "strconv" "strings" "syscall" //"flag" //"log" "FRMS/internal/pkg/config" "FRMS/internal/pkg/logging" "FRMS/internal/pkg/server" "os" ) type coordinator interface { Start() } func NewCoordinator(ch chan error) coordinator { return server.NewCentralCoordinator(ch) } func LoadConfig(fname string) Config { if err := config.Load(fname); err != nil { panic(err) } return config.LoadConfig() } type Config interface { UpdatePort(string, int) error Store() error } func main() { // lets get this bread // go func() { // fmt.Println(http.ListenAndServe("localhost:6060",nil)) // }() gracefulShutdown := make(chan os.Signal, 1) signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM) conf := LoadConfig("server") errCh := make(chan error) // checking env envVars := os.Environ() for _, envString := range envVars { // looping over set ports initSplt := strings.Split(envString, "=") key := initSplt[0] val := initSplt[1] if strings.Contains(key, "PORT") { // parsing out correct port to update splt := strings.Split(key, "_") // LIS_PORT -> LIS, PORT portName := strings.ToLower(splt[0]) // LIS -> lis port, err := strconv.Atoi(val) if err != nil { panic(err) } if err := conf.UpdatePort(portName, port); err != nil { panic(err) } } } //fmt.Printf("Listening on %v\n", lport) c := NewCoordinator(errCh) go c.Start() logging.Debug(logging.DStart, "CCO 01 Server started") select { case err := <-errCh: // blocking to wait for any errors and keep alive otherwise panic(err) case <-gracefulShutdown: err := conf.Store() if err != nil { panic(err) } fmt.Println("Stored config successfully. Exiting...") os.Exit(0) } }