2023-03-19 22:03:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-22 22:56:25 +01:00
|
|
|
"context"
|
2023-03-19 22:03:12 +01:00
|
|
|
"flag"
|
2023-03-22 22:13:23 +01:00
|
|
|
"net/http"
|
2023-03-22 22:56:25 +01:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
2023-03-19 22:03:12 +01:00
|
|
|
|
2023-03-22 22:13:23 +01:00
|
|
|
"git.dotya.ml/mirre-mt/pcmt/app"
|
2023-03-19 22:03:12 +01:00
|
|
|
"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 {
|
2023-03-22 22:13:23 +01:00
|
|
|
a := &app.App{}
|
|
|
|
|
|
|
|
err := a.Init()
|
2023-03-19 22:03:12 +01:00
|
|
|
if err != nil {
|
2023-03-22 22:13:23 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
e := a.E()
|
|
|
|
logger := a.Logger()
|
|
|
|
|
2023-03-22 23:04:57 +01:00
|
|
|
a.StartupSettings()
|
2023-03-22 22:13:23 +01:00
|
|
|
|
|
|
|
_, err = config.LoadConfig(*configFlag)
|
|
|
|
if err != nil {
|
|
|
|
logger.Println("error loading config file", *configFlag)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-22 23:03:21 +01:00
|
|
|
a.SetupRoutes()
|
2023-03-22 22:13:23 +01:00
|
|
|
|
2023-03-22 22:56:25 +01:00
|
|
|
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 {
|
2023-03-19 22:03:12 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|