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.
75 lines
1.5 KiB
Go
75 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"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() {
|
|
|
|
fmt.Printf("starting server... ")
|
|
start := time.Now()
|
|
|
|
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 %s started", conf.Get("name"))
|
|
|
|
elapsed := time.Now().Sub(start)
|
|
fmt.Printf("done %v\n", elapsed.Round(time.Microsecond))
|
|
|
|
select {
|
|
case err := <-errCh:
|
|
panic(err)
|
|
case <-gracefulShutdown:
|
|
fmt.Printf("\nstopping server... ")
|
|
start := time.Now()
|
|
if err := conf.WriteConfig(); err != nil {
|
|
panic(err)
|
|
}
|
|
logging.Debug(logging.DExit, "CON wrote %s", conf.ConfigFileUsed())
|
|
elapsed := time.Now().Sub(start)
|
|
fmt.Printf("done %v\n", elapsed.Round(time.Microsecond))
|
|
os.Exit(0)
|
|
}
|
|
}
|