657fca325e
Adds opt-in extension package internal/automc/ that: - Subscribes to Postgres notifications on a 'servers' table and pushes route changes into server.Routes (no file I/O, no fsnotify). - Provides a WakerFunc that POSTs to a configurable HTTP control plane (server-manager) and polls until state=running. When AUTOMC_DSN is unset, Wire() is a no-op and the binary behaves exactly like upstream itzg/mc-router. Single patch site in main.go (import + 4-line call) keeps upstream rebases trivial. See docs/AUTOMC.md for env vars and the expected DB schema/trigger.
96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"syscall"
|
|
|
|
"github.com/itzg/go-flagsfiller"
|
|
"github.com/itzg/mc-router/internal/automc"
|
|
"github.com/itzg/mc-router/server"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var (
|
|
version = "dev"
|
|
commit = "none"
|
|
date = "unknown"
|
|
)
|
|
|
|
func showVersion() {
|
|
fmt.Printf("%v, commit %v, built at %v", version, commit, date)
|
|
}
|
|
|
|
type CliConfig struct {
|
|
Version bool `usage:"Output version and exit"`
|
|
Debug bool `usage:"Enable debug logs"`
|
|
Trace bool `usage:"Enable trace logs"`
|
|
LogLevel logrus.Level `usage:"Set a specific log filtering level, such as debug, info, warn, error\nIgnored when --debug or --trace is used" default:"info"`
|
|
|
|
ServerConfig server.Config `flatten:"true"`
|
|
}
|
|
|
|
func main() {
|
|
var cliConfig CliConfig
|
|
err := flagsfiller.Parse(&cliConfig, flagsfiller.WithEnv(""))
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
|
|
if cliConfig.Version {
|
|
showVersion()
|
|
os.Exit(0)
|
|
}
|
|
|
|
if cliConfig.Trace {
|
|
logrus.SetLevel(logrus.TraceLevel)
|
|
logrus.Trace("Trace logs enabled")
|
|
} else if cliConfig.Debug {
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
logrus.Debug("Debug logs enabled")
|
|
} else {
|
|
logrus.SetLevel(cliConfig.LogLevel)
|
|
}
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer stop()
|
|
|
|
signals := make(chan os.Signal, 1)
|
|
signal.Notify(signals, syscall.SIGHUP)
|
|
|
|
s, err := server.NewServer(ctx, &cliConfig.ServerConfig)
|
|
if err != nil {
|
|
logrus.WithError(err).Fatal("Could not setup server")
|
|
}
|
|
|
|
if err := automc.Wire(ctx); err != nil {
|
|
logrus.WithError(err).Fatal("automc Wire failed")
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Go(s.Run)
|
|
|
|
signalsLoop:
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
break signalsLoop
|
|
|
|
case sig := <-signals:
|
|
switch sig {
|
|
case syscall.SIGHUP:
|
|
s.ReloadConfig()
|
|
|
|
default:
|
|
logrus.WithField("signal", sig).Warn("Received unexpected signal")
|
|
}
|
|
}
|
|
}
|
|
|
|
logrus.Info("Stopping")
|
|
wg.Wait()
|
|
}
|