go-app/pkg/app/setup.go

33 lines
807 B
Go

package app
import (
"context"
"github.com/rs/zerolog"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/logging"
)
// Helper function to return a context loaded up with
// config.AppConfig and a logger
func MustSetupConfigAndLogging(ctx context.Context) context.Context {
ctx, err := config.LoadConfig(ctx)
if err != nil {
panic(err)
}
cfg := config.MustFromCtx(ctx)
ctx = logging.MustInitLogging(ctx)
zerolog.Ctx(ctx).Trace().Any("config", *cfg).Send()
return ctx
}
// Unmarshal config into a custom type
// Type MUST include *config.AppConfig
// Stored in context as *config.AppConfig but can be asserted back
func MustSetupConfigAndLoggingInto[T any](ctx context.Context, into T) (context.Context, T) {
return ctx, into
}