86 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			86 lines
		
	
	
		
			1.9 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"
 | 
						|
	"net/http"
 | 
						|
	"os"
 | 
						|
	"os/signal"
 | 
						|
 | 
						|
	"golang.org/x/sys/unix"
 | 
						|
 | 
						|
	"gitea.libretechconsulting.com/rmcguire/go-app/pkg/app"
 | 
						|
	optshttp "gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/http/opts"
 | 
						|
 | 
						|
	"gitea.libretechconsulting.com/rmcguire/go-http-server-with-otel/pkg/config"
 | 
						|
)
 | 
						|
 | 
						|
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, 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: &optshttp.AppHTTP{
 | 
						|
			Funcs: []optshttp.HTTPFunc{
 | 
						|
				{
 | 
						|
					Path: "/test",
 | 
						|
					HandlerFunc: func(w http.ResponseWriter, r *http.Request) {
 | 
						|
						w.WriteHeader(http.StatusNotImplemented)
 | 
						|
						w.Write([]byte("http test route"))
 | 
						|
					},
 | 
						|
				},
 | 
						|
			},
 | 
						|
		},
 | 
						|
	}
 | 
						|
 | 
						|
	// 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)
 | 
						|
}
 |