go-paste/v2/main.go
xuu b82a1e954e
All checks were successful
Deploy / deploy (push) Successful in 1m21s
feat: create v2 for module support
2023-10-19 17:07:37 -06:00

51 lines
1.0 KiB
Go

package main
import (
"context"
"errors"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
"strconv"
"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 {
// TODO: make this configurable.
level := slog.LevelInfo
if ok, _ := strconv.ParseBool(os.Getenv("LOG_DEBUG")); ok {
level = slog.LevelDebug
}
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})))
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
}