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.

91 lines
1.7 KiB
Go

// package main starts config, database, and network setup.
package main
import (
"fmt"
"time"
conf "dmac/pkg/config"
"dmac/pkg/database"
"dmac/pkg/logging"
"dmac/pkg/server"
"dmac/pkg/websocket"
"os"
"github.com/spf13/viper"
)
type coordinator interface {
Start() error
}
func newCoordinator(config *viper.Viper, ch chan error) coordinator {
return server.NewCentralCoordinator(config, ch)
}
type ws interface {
Start()
}
func newWebSocket() ws {
return websocket.New()
}
func main() {
fmt.Printf("starting server... ")
start := time.Now()
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"))
// starting websocket server via env
if config.GetString("WEBSOCKET") == "start" {
w := newWebSocket()
go w.Start()
fmt.Printf("Websocket started\n")
logging.Debug(logging.DStart, "WS websocket started")
}
gracefulShutdown := make(chan os.Signal, 1)
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))
}
}