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.

128 lines
2.7 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
2 years ago
}
func NewCoordinator(config *viper.Viper, ch chan error) coordinator {
return server.NewCentralCoordinator(config, ch)
}
1 year ago
<<<<<<< HEAD:server/main.go
func NewConfig(filename string) (*viper.Viper, error) {
return config.LoadConfig(filename)
}
type ws interface {
Start()
}
func NewWebSocket() ws {
return websocket.New()
}
1 year ago
=======
>>>>>>> origin/wrapup:cmd/server/main.go
2 years ago
func main() {
fmt.Printf("starting server... ")
start := time.Now()
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGINT, syscall.SIGTERM)
1 year ago
<<<<<<< HEAD:server/main.go
// config file
conf, err := NewConfig("server")
if err != nil {
panic(err)
}
errCh := make(chan error)
c := NewCoordinator(conf, errCh)
go c.Start()
fmt.Printf("Coordiantor started\n")
logging.Debug(logging.DStart, "CCO %s started", conf.Get("name"))
// starting websocket server
// gated behind env for testing
if conf.Get("WEBSOCKET") == "start" {
w := NewWebSocket()
go w.Start()
fmt.Printf("Websocket started\n")
logging.Debug(logging.DStart, "WS websocket started")
}
select {
case err := <-errCh: // allows passing errors up for handling
panic(err)
case <-gracefulShutdown:
// Shutdown via INT
fmt.Printf("\nExiting...\n")
logging.Debug(logging.DStop, "CON storing to %s", conf.ConfigFileUsed())
if err := conf.WriteConfig(); err != nil {
panic(err)
}
logging.Debug(logging.DStop, "CON stored successfully", conf.ConfigFileUsed())
1 year ago
=======
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))
1 year ago
>>>>>>> origin/wrapup:cmd/server/main.go
os.Exit(0)
}
}