update to new go-app

This commit is contained in:
2025-03-30 11:38:56 -04:00
parent e8e4af8051
commit 1e18909940
7 changed files with 482 additions and 112 deletions

47
main.go
View File

@ -11,37 +11,45 @@ package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sys/unix"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/app"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv"
optshttp "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
"gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/pkg/config"
)
var (
cfg *config.AppConfig
l *zerolog.Logger
tracer trace.Tracer
)
var flagSchema bool
func main() {
ctx, cncl := signal.NotifyContext(context.Background(), os.Interrupt, unix.SIGTERM)
defer cncl()
// This will be loaded by go-app courtesy of MustLoadConfigInto, which will
// combine our sample configuration along with the embedded go-app AppConfig
demoAppConfig := &config.DemoConfig{}
// Load configuration and setup logging
ctx = app.MustSetupConfigAndLogging(ctx)
ctx, demoApp := app.MustLoadConfigInto(ctx, demoAppConfig)
fmt.Printf("Demo App Config:\n\n%+v\n", *demoApp)
// Print schema if that's all we have to do
if flagSchema {
printSchema()
os.Exit(1)
}
// Prepare app
app := &app.App{
AppContext: ctx,
HTTP: &app.AppHTTP{
Funcs: []srv.HTTPFunc{
HTTP: &optshttp.AppHTTP{
Funcs: []optshttp.HTTPFunc{
{
Path: "/test",
HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
@ -60,3 +68,18 @@ func main() {
// Perform any extra shutdown here
<-app.Done()
}
// flag.Parse will be called by go-app
func init() {
flag.BoolVar(&flagSchema, "schema", false, "generate json schema and exit")
}
func printSchema() {
bytes, err := app.CustomSchema(&config.DemoConfig{})
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
os.Exit(0)
}