felt/main.go

78 lines
1.6 KiB
Go

package main
import (
"context"
"forge.lightcrystal.systems/nilix/felt/cmd"
"forge.lightcrystal.systems/nilix/felt/config"
"forge.lightcrystal.systems/nilix/felt/gametable"
"forge.lightcrystal.systems/nilix/felt/mongodb"
"forge.lightcrystal.systems/nilix/felt/register"
"forge.lightcrystal.systems/nilix/quartzgun/indentalUserDB"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"strconv"
"time"
)
func main() {
err := run()
if err != nil {
log.Fatal(err)
}
}
func run() error {
cfg := config.ReadConfig()
if cfg.IsNull() {
cfg.RunWizard()
}
udb := indentalUserDB.CreateIndentalUserDB(
filepath.Join(config.GetConfigLocation(), "user.db"))
crypto := &register.SymmetricCrypt{Secret: cfg.RegistrationSecret, Iv: cfg.RegistrationSalt}
if cmd.ProcessCmd(os.Args, udb, crypto) {
os.Exit(0)
}
l, err := net.Listen("tcp", ":"+strconv.FormatInt(int64(cfg.Port), 10))
if err != nil {
return err
}
dbEngine := &mongodb.DbEngine{}
err = dbEngine.Init(cfg.MongoURI)
if err != nil {
return err
}
gt := gametable.New(dbEngine, udb, cfg.Uploads, cfg.UploadMaxMB, crypto)
s := &http.Server{
Handler: gt,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
}
errc := make(chan error, 1)
go func() {
errc <- s.Serve(l)
}()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
select {
case err := <-errc:
log.Printf("failed to serve: %v", err)
case sig := <-sigs:
log.Printf("terminating: %v", sig)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
return s.Shutdown(ctx)
}