pcmt/run.go

64 lines
1.1 KiB
Go

package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"time"
"git.dotya.ml/mirre-mt/pcmt/app"
"git.dotya.ml/mirre-mt/pcmt/config"
)
var (
addr = flag.String("addr", ":3000", "TCP address:port to listen at")
configFlag = flag.String("config", "config.dhall", "Default path of the config file")
)
func run() error {
a := &app.App{}
err := a.Init()
if err != nil {
return err
}
e := a.E()
logger := a.Logger()
app.StartupSettings(e)
_, err = config.LoadConfig(*configFlag)
if err != nil {
logger.Println("error loading config file", *configFlag)
return err
}
a.SetupRoutes()
go func() {
if err = e.Start(*addr); err != nil && err != http.ErrServerClosed {
logger.Println("shutting down the server")
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer func() {
logger.Println("Interrupt received, gracefully shutting down the server")
cancel()
}()
if err = e.Shutdown(ctx); err != nil {
return err
}
return nil
}