package main import ( "context" "encoding/json" "errors" "log/slog" "net/http" "os" "os/signal" "path/filepath" "syscall" "time" "sea-vhost/internal/controlwire" "sea-vhost/internal/executor" ) type configuration struct { Identity controlwire.ServerIdentity `json:"identity"` Address string `json:"address"` TenantRoot string `json:"tenantRoot"` BrokerSocket string `json:"brokerSocket"` AgentSocket string `json:"agentSocket"` } func main() { path := os.Getenv("SEA_NODE_CONFIG") if path == "" { path = "/etc/sea-vhost-node/config.json" } data, err := os.ReadFile(path) if err != nil { slog.Error("cannot read node configuration") os.Exit(1) } var cfg configuration if json.Unmarshal(data, &cfg) != nil || cfg.Identity.NodeID == 0 || cfg.Address == "" || cfg.TenantRoot == "" { slog.Error("invalid node configuration") os.Exit(1) } if !filepath.IsAbs(cfg.TenantRoot) || filepath.Clean(cfg.TenantRoot) != cfg.TenantRoot { slog.Error("invalid node root") os.Exit(1) } tlsConfig, err := controlwire.ServerTLS(cfg.Identity) if err != nil { slog.Error("invalid node TLS identity") os.Exit(1) } token := os.Getenv("SEA_INTERNAL_AUTH_TOKEN") if len(token) < 32 { slog.Error("node local execution token is missing") os.Exit(1) } gateway := &executor.Gateway{NodeID: cfg.Identity.NodeID, TenantRoot: cfg.TenantRoot, BrokerSocket: cfg.BrokerSocket, AgentSocket: cfg.AgentSocket, InternalToken: token} server := &http.Server{Addr: cfg.Address, Handler: gateway.Handler(), TLSConfig: tlsConfig, ReadHeaderTimeout: 10 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 2 * time.Hour, IdleTimeout: 30 * time.Second, MaxHeaderBytes: 16 << 10} failed := make(chan error, 1) go func() { failed <- server.ListenAndServeTLS("", "") }() stop, done := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer done() select { case <-stop.Done(): case err := <-failed: if !errors.Is(err, http.ErrServerClosed) { slog.Error("node service stopped", "error", err) os.Exit(1) } } shutdown, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() _ = server.Shutdown(shutdown) }