2024-03-24 15:19:26 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"log"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"go.sour.is/pkg/env"
|
|
|
|
"go.sour.is/pkg/lg"
|
|
|
|
"go.sour.is/pkg/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
var apps service.Apps
|
|
|
|
var appName, version = service.AppName()
|
|
|
|
|
|
|
|
func main() {
|
2024-04-19 10:54:35 -06:00
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
2024-03-24 15:19:26 -06:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
defer cancel() // restore interrupt function
|
|
|
|
}()
|
|
|
|
if err := run(ctx); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-04-19 10:54:35 -06:00
|
|
|
os.Exit(0)
|
2024-03-24 15:19:26 -06:00
|
|
|
}
|
|
|
|
func run(ctx context.Context) error {
|
|
|
|
level := slog.LevelError
|
|
|
|
if ok, _ := strconv.ParseBool(env.Default("LOG_DEBUG", "TRUE")); ok {
|
|
|
|
level = slog.LevelDebug
|
|
|
|
}
|
|
|
|
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level})))
|
|
|
|
|
|
|
|
svc := &service.Harness{}
|
|
|
|
os.Setenv("TRACE_ENDPOINT", "atombox:4318")
|
|
|
|
os.Setenv("EV_TRACE_SAMPLE", "always")
|
|
|
|
ctx, stop := lg.Init(ctx, appName+"-mercury")
|
|
|
|
svc.OnStop(stop)
|
|
|
|
|
|
|
|
svc.Add(lg.NewHTTP(ctx))
|
|
|
|
svc.Setup(ctx, apps.Apps()...)
|
|
|
|
|
|
|
|
// Run application
|
|
|
|
if err := svc.Run(ctx, appName+"-mercury", version); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|