2025-01-04 12:24:42 -05:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2025-03-07 15:19:05 -05:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2025-01-04 12:24:42 -05:00
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
|
|
|
)
|
|
|
|
|
2025-03-06 17:16:27 -05:00
|
|
|
func (a *App) Done() <-chan any {
|
2025-01-04 12:24:42 -05:00
|
|
|
return a.appDone
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *App) MustRun() {
|
|
|
|
if a.cfg != nil {
|
|
|
|
panic(errors.New("already ran app trying to run"))
|
2025-03-07 16:45:50 -05:00
|
|
|
} else if !a.cfg.HTTP.Enabled && !a.cfg.GRPC.Enabled {
|
|
|
|
panic(errors.New("neither http nor grpc enabled, nothing to do"))
|
2025-01-04 12:24:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up app
|
|
|
|
a.cfg = config.MustFromCtx(a.AppContext)
|
|
|
|
a.l = zerolog.Ctx(a.AppContext)
|
|
|
|
a.shutdownFuncs = make([]shutdownFunc, 0)
|
2025-03-06 17:16:27 -05:00
|
|
|
a.appDone = make(chan any)
|
2025-03-07 15:19:05 -05:00
|
|
|
a.HTTP.HTTPDone = make(chan any)
|
2025-01-04 12:24:42 -05:00
|
|
|
|
|
|
|
if len(a.HTTP.Funcs) < 1 {
|
|
|
|
a.l.Warn().Msg("no http funcs provided, only serving health and metrics")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start OTEL
|
|
|
|
a.initOTEL()
|
2025-03-06 17:16:27 -05:00
|
|
|
ctx, initSpan := a.tracer.Start(a.AppContext, "init")
|
|
|
|
defer initSpan.End()
|
2025-01-04 12:24:42 -05:00
|
|
|
|
2025-03-07 15:19:05 -05:00
|
|
|
// Start HTTP (does not block)
|
2025-03-07 16:45:50 -05:00
|
|
|
if a.cfg.HTTP.Enabled {
|
|
|
|
if err := a.initHTTP(ctx); err != nil {
|
|
|
|
initSpan.RecordError(err)
|
|
|
|
initSpan.SetStatus(codes.Error, err.Error())
|
|
|
|
}
|
|
|
|
initSpan.AddEvent("http server started")
|
|
|
|
initSpan.SetAttributes(attribute.Int("http.handlers", len(a.HTTP.Funcs)))
|
2025-03-06 17:16:27 -05:00
|
|
|
}
|
|
|
|
|
2025-03-07 15:19:05 -05:00
|
|
|
// Start GRPC (does not block)
|
2025-03-07 16:45:50 -05:00
|
|
|
if a.cfg.GRPC.Enabled {
|
|
|
|
if err := a.initGRPC(ctx); err != nil {
|
|
|
|
initSpan.RecordError(err)
|
|
|
|
initSpan.SetStatus(codes.Error, err.Error())
|
|
|
|
}
|
|
|
|
initSpan.AddEvent("grpc server started")
|
|
|
|
initSpan.SetAttributes(attribute.Int("grpc.services", len(a.GRPC.Services)))
|
2025-03-07 15:19:05 -05:00
|
|
|
}
|
2025-01-04 12:24:42 -05:00
|
|
|
|
|
|
|
// Monitor app lifecycle
|
|
|
|
go a.run()
|
|
|
|
|
|
|
|
// Startup Complete
|
|
|
|
a.l.Info().
|
|
|
|
Str("name", a.cfg.Name).
|
|
|
|
Str("version", a.cfg.Version).
|
|
|
|
Str("logLevel", a.cfg.Logging.Level).
|
|
|
|
Msg("app initialized")
|
|
|
|
initSpan.SetStatus(codes.Ok, "")
|
|
|
|
}
|