Scaffold tomswallapi control-plane service

This commit is contained in:
benvin
2026-07-19 13:31:14 +10:00
commit 6de48552fb
19 changed files with 1021 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
// Command tomswallapi is the fleet control-plane HTTP server for tomswall.
//
// It stores the fleet-global model (zones, address groups, portgroups, policies,
// rules, fabrics) and the per-device layer (devices, zone->interface bindings),
// compiles intents into per-device tomswall configs, and serves those configs to
// tomswall agents. The read/write API backs a Terraform provider and the agents.
package main
import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
"git.unkin.net/unkin/tomswallapi/internal/config"
"git.unkin.net/unkin/tomswallapi/internal/database"
"git.unkin.net/unkin/tomswallapi/internal/server"
)
var version = "dev"
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
slog.Info("starting tomswallapi", "version", version)
cfg, err := config.Load()
if err != nil {
slog.Error("load config", "err", err)
os.Exit(1)
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
db, err := database.New(ctx, cfg.DatabaseDSN())
if err != nil {
slog.Error("connect database", "err", err)
os.Exit(1)
}
defer db.Close()
if err := db.Migrate(ctx); err != nil {
slog.Error("migrate database", "err", err)
os.Exit(1)
}
if cfg.WriteToken == "" {
slog.Warn("TOMSWALLAPI_WRITE_TOKEN is not set; write endpoints are disabled")
}
if cfg.AgentToken == "" {
slog.Warn("TOMSWALLAPI_AGENT_TOKEN is not set; agent config endpoint is disabled")
}
srv := server.New(server.Options{
DB: db,
WriteToken: cfg.WriteToken,
AgentToken: cfg.AgentToken,
Version: version,
})
if err := srv.ListenAndServe(ctx, cfg.ListenAddr); err != nil {
slog.Error("server", "err", err)
os.Exit(1)
}
}