90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
// This template contains a simple
|
|
// app with OTEL bootstrap that will create an
|
|
// HTTP server configured by environment that exports
|
|
// 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
|
|
// Reference implementation of the provided packages
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
"golang.org/x/sys/unix"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/app"
|
|
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
|
|
optshttp "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
|
|
|
|
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/config"
|
|
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/demogrpc"
|
|
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/demohttp"
|
|
)
|
|
|
|
var flagSchema bool
|
|
|
|
func main() {
|
|
ctx, cncl := signal.NotifyContext(context.Background(), os.Interrupt, unix.SIGTERM)
|
|
defer cncl()
|
|
|
|
// Load configuration and setup logging. The go-app framework
|
|
// will handle loading config and environment into our demo
|
|
// app config struct which embeds app.AooConfig
|
|
ctx, demoApp := app.MustLoadConfigInto(ctx, &config.DemoConfig{})
|
|
|
|
// Print schema if that's all we have to do
|
|
if flagSchema {
|
|
printSchema()
|
|
os.Exit(1)
|
|
}
|
|
|
|
log.Debug().Any("demoAppMergedConfig", demoApp).Msg("demo app config prepared")
|
|
|
|
// Prepare servers
|
|
demoHTTP := demohttp.NewDemoHTTPServer(ctx, demoApp)
|
|
demoGRPC := demogrpc.NewDemoGRPCServer(ctx, demoApp)
|
|
|
|
// Prepare app
|
|
app := &app.App{
|
|
AppContext: ctx,
|
|
GRPC: &opts.AppGRPC{
|
|
Services: demoGRPC.GetServices(),
|
|
GRPCDialOpts: demoGRPC.GetDialOpts(),
|
|
},
|
|
HTTP: &optshttp.AppHTTP{
|
|
Ctx: ctx,
|
|
HealthChecks: demoHTTP.GetHealthCheckFuncs(),
|
|
Funcs: demoHTTP.GetHandleFuncs(),
|
|
},
|
|
}
|
|
|
|
// Launch app
|
|
app.MustRun()
|
|
|
|
// Wait for app to complete
|
|
// 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)
|
|
}
|