Config and logging

This commit is contained in:
2025-01-03 21:09:40 -05:00
parent 3d3b44bfc7
commit b93f8fb6c8
7 changed files with 215 additions and 42 deletions

27
main.go
View File

@ -4,35 +4,44 @@
// spans and metrics to an OTEL collector if configured
// to do so. Will also stand up a prometheus metrics
// endpoint.
//
// Configuration and logger stored in context
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/rs/zerolog"
"golang.org/x/sys/unix"
"gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/pkg/logging"
)
func main() {
ctx, cncl := signal.NotifyContext(context.Background(), os.Interrupt, unix.SIGTERM)
defer cncl()
conf, err := config.LoadConfig()
if err != nil {
panic(err)
}
ctx = conf.AddToCtx(ctx)
conf, err = config.FromCtx(ctx)
// Set up app config and logging
ctx, err := config.LoadConfig(ctx)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", conf)
cfg := config.MustFromCtx(ctx)
ctx = logging.MustInitLogging(ctx)
l := zerolog.Ctx(ctx)
l.Trace().Any("config", *cfg).Send()
l.Info().
Str("name", cfg.Name).
Str("version", cfg.Version).
Str("logLevel", cfg.Logging.Level).
Msg("app initialized")
<-ctx.Done()
}
func init() {}