You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.3 KiB
Go

package main
3 years ago
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"
3 years ago
)
type coordinator interface {
Start()
3 years ago
}
// NewCoordinator creates a new coordinator that runs on the central server
// The coordinator is given an error channel to feed errors back to the main process should any arise
// The zero value for a new coordinator is ready to start
func NewCoordinator(ch chan error) coordinator {
return server.NewCentralCoordinator(ch)
}
// LoadConfig loads a given config based on a string lookup
// Used to load the associated settings for a coordinator such as port and IP address as well as database settings
// LoadConfig expects the returned config to satisfy the interface
func LoadConfig(fname string) Config {
if err := config.Load(fname); err != nil {
panic(err)
}
return config.LoadConfig()
}
// Basic functions expected to be provided by the config structure
type Config interface {
UpdatePort(string, int) error
Store() error
}
3 years ago
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()
// I can put this is a seperate func
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)
}
}