// Copyright © 2023 Roberto Hidalgo // SPDX-License-Identifier: Apache-2.0 package main import ( "fmt" "log" "net/http" "os" "git.rob.mx/nidito/chinampa" "git.rob.mx/nidito/chinampa/pkg/command" "git.rob.mx/nidito/chinampa/pkg/env" "git.rob.mx/nidito/chinampa/pkg/logger" "git.rob.mx/nidito/chinampa/pkg/runtime" "git.rob.mx/nidito/event-gateway/internal/config" "git.rob.mx/nidito/event-gateway/internal/source" "git.rob.mx/nidito/event-gateway/internal/source/consul" httpsrc "git.rob.mx/nidito/event-gateway/internal/source/http" "git.rob.mx/nidito/event-gateway/internal/version" "github.com/hashicorp/consul/api" "github.com/honeycombio/honeycomb-opentelemetry-go" "github.com/honeycombio/otel-config-go/otelconfig" "github.com/julienschmidt/httprouter" ) var Server = &command.Command{ Path: []string{"server"}, Summary: "starts a server", Description: "tbd", Arguments: command.Arguments{ &command.Argument{ Name: "listen", Default: ":8000", Description: "An address for the http server to listen at", }, }, Options: command.Options{ "source": &command.Option{ Type: command.ValueTypeString, Description: "Event sources to enable", Repeated: true, Default: []string{"http"}, Values: &command.ValueSource{ Static: &[]string{ "http", "consul", }, }, }, "sink": &command.Option{ Type: command.ValueTypeString, Description: "Event sinks to enable", Repeated: true, Default: []string{"debug", "nomad"}, Values: &command.ValueSource{ Static: &[]string{ "debug", "nomad", }, }, }, "config": &command.Option{ Type: command.ValueTypeString, Description: "A URL (file:// if no scheme is provided) where to read configuration from", Default: "file://listeners.json", }, }, Action: func(cmd *command.Command) error { router := httprouter.New() addr := cmd.Arguments[0].ToString() manager := &source.Manager{} // Enable multi-span attributes bsp := honeycomb.NewBaggageSpanProcessor() // Use the Honeycomb distro to set up the OpenTelemetry SDK otelShutdown, err := otelconfig.ConfigureOpenTelemetry( otelconfig.WithSpanProcessor(bsp), ) if err != nil { log.Fatalf("error setting up OTel SDK - %e", err) } defer otelShutdown() for _, source := range cmd.Options["source"].ToValue().([]string) { switch source { case "http": manager.AddSource(&httpsrc.Source{Router: router}) case "consul": cfg := api.DefaultConfig() client, err := api.NewClient(cfg) if err != nil { return fmt.Errorf("could not create consul client: %s", err) } manager.AddSource(&consul.Source{Client: client.Event()}) default: logger.Fatalf("Unknown source type: %s", source) } logger.Infof("added source for %s events", source) } cfg, err := config.FromURN(cmd.Options["config"].ToString()) if err != nil { return fmt.Errorf("could not initialize configuration loader: %s", err) } if err := manager.Listen(cfg); err != nil { return fmt.Errorf("could not start listener: %s", err) } return http.ListenAndServe(addr, router) }, } func logLevel() logger.Level { if os.Getenv(env.Debug) == "trace" { return logger.LevelTrace } else if runtime.DebugEnabled() { return logger.LevelDebug } return logger.LevelInfo } func main() { logger.Configure("event-gateway", logLevel()) chinampa.Register(Server) if err := chinampa.Execute(chinampa.Config{ Name: "event-gateway", Summary: "routes events to places", Description: `Does things TBD`, Version: version.Version, }); err != nil { logger.Errorf("total failure: %s", err) os.Exit(2) } }