go-app/pkg/app/app.go

57 lines
1.1 KiB
Go
Raw Normal View History

2025-01-04 12:24:42 -05:00
package app
import (
"errors"
"github.com/rs/zerolog"
"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"))
}
// 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)
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
// Start HTTP
2025-03-06 17:16:27 -05:00
if err := a.initHTTP(ctx); err != nil {
initSpan.RecordError(err)
initSpan.SetStatus(codes.Error, err.Error())
}
// Start GRPC
a.initGRPC()
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, "")
}