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.

77 lines
1.5 KiB
Go

package main
3 years ago
import (
"fmt"
"os/signal"
"syscall"
"time"
conf "FRMS/internal/pkg/config"
"FRMS/internal/pkg/database"
"FRMS/internal/pkg/logging"
"FRMS/internal/pkg/server"
"os"
"github.com/spf13/viper"
3 years ago
)
type coordinator interface {
Start() error
3 years ago
}
func NewCoordinator(config *viper.Viper, ch chan error) coordinator {
return server.NewCentralCoordinator(config, ch)
}
3 years ago
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"
config, err := conf.Load(configFile, configPath, configExt)
if err != nil {
panic(err)
}
database.Connect(config)
errCh := make(chan error)
c := NewCoordinator(config, errCh)
if err := c.Start(); err != nil {
panic(err)
}
logging.Debug(logging.DStart, "CCO 01 %s started", config.Get("name"))
elapsed := time.Since(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 := config.WriteConfig(); err != nil {
panic(err)
}
logging.Debug(logging.DExit, "CON wrote %s", config.ConfigFileUsed())
elapsed := time.Since(start)
fmt.Printf("done %v\n", elapsed.Round(time.Microsecond))
os.Exit(0)
}
}