go-tools/cmd/ev/main.go

52 lines
1.0 KiB
Go
Raw Permalink Normal View History

2023-09-29 10:31:25 -06:00
package main
import (
"context"
"errors"
"log"
2023-10-07 17:57:09 -06:00
"log/slog"
2023-09-29 10:31:25 -06:00
"net/http"
"os"
"os/signal"
2023-10-07 17:57:09 -06:00
"strconv"
2023-09-29 10:31:25 -06:00
2023-10-07 17:57:09 -06:00
"go.sour.is/pkg/env"
2023-09-29 10:31:25 -06:00
"go.sour.is/pkg/lg"
"go.sour.is/pkg/service"
)
var apps service.Apps
var appName, version = service.AppName()
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
go func() {
<-ctx.Done()
defer cancel() // restore interrupt function
}()
if err := run(ctx); err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
2023-10-07 17:57:09 -06:00
// TODO: make this configurable.
level := slog.LevelError
if ok, _ := strconv.ParseBool(env.Default("LOG_DEBUG", "FALSE")); ok {
level = slog.LevelDebug
}
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level})))
2023-09-29 10:31:25 -06:00
svc := &service.Harness{}
ctx, stop := lg.Init(ctx, appName)
svc.OnStop(stop)
svc.Add(lg.NewHTTP(ctx))
svc.Setup(ctx, apps.Apps()...)
// Run application
if err := svc.Run(ctx, appName, version); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
return nil
}