package main import ( "context" "encoding/json" "errors" "log/slog" "net/http" "os" "os/signal" "syscall" "time" "github.com/local/glpi-neural-brain/internal/activity" "github.com/local/glpi-neural-brain/internal/config" "github.com/local/glpi-neural-brain/internal/engine" "github.com/local/glpi-neural-brain/internal/graph" "github.com/local/glpi-neural-brain/internal/ingest" "github.com/local/glpi-neural-brain/internal/sourceagent" webui "github.com/local/glpi-neural-brain/internal/web" ) const buildVersion = "production-readiness-v1.2" const agentStatusHTML = ` Neural Brain Source Agent

Neural Brain · Source Agent

Leichtgewichtiger Poller-Modus · Status aktualisiert sich automatisch.
lade…
JSON: /api/status · Health: /healthz
` func main() { cfg, err := config.Load() if err != nil { slog.Error("configuration invalid", "error", err) os.Exit(1) } slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))) ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer cancel() if cfg.Mode == "agent" { runAgent(ctx, cfg) return } runBrain(ctx, cancel, cfg) } func runAgent(ctx context.Context, cfg config.Config) { runner, err := sourceagent.NewRunner(sourceagent.RunnerConfig{ BrainURL: cfg.AgentBrainURL, AgentID: cfg.AgentID, Token: cfg.AgentToken, DataDir: cfg.DataDir, ConfigFile: cfg.AgentConfigFile, ConfigRefresh: cfg.AgentConfigRefresh, HTTPTimeout: cfg.AgentHTTPTimeout, Concurrency: cfg.AgentConcurrency, BatchSize: cfg.AgentBatchSize, AllowPrivate: cfg.AgentAllowPrivate, Version: buildVersion, ComputeEnabled: cfg.AgentComputeEnabled, ComputePollInterval: cfg.AgentComputePollInterval, ComputeMaxBytes: cfg.AgentComputeMaxBytes, DockerControllerEnabled: cfg.AgentDockerControllerEnabled, DockerSocket: cfg.AgentDockerSocket, DockerComposeBinary: cfg.AgentDockerComposeBinary, ControllerPollInterval: cfg.AgentControllerPollInterval, ControllerMaxDuration: cfg.AgentControllerMaxDuration, }) if err != nil { slog.Error("source agent initialization failed", "error", err) os.Exit(1) } defer runner.Close() runner.Start(ctx) mux := http.NewServeMux() mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Cache-Control", "no-store") _, _ = w.Write([]byte(agentStatusHTML)) }) mux.HandleFunc("GET /api/status", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(runner.Status()) }) mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }) srv := &http.Server{Addr: cfg.ListenAddr, Handler: mux, ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 30 * time.Second} go func() { slog.Info("neural brain source-agent listening", "addr", cfg.ListenAddr, "agent_id", cfg.AgentID, "brain_url", cfg.AgentBrainURL) if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { slog.Error("agent status server failed", "error", err) } }() <-ctx.Done() shutdown, c := context.WithTimeout(context.Background(), 10*time.Second) defer c() _ = srv.Shutdown(shutdown) } func runBrain(ctx context.Context, cancel context.CancelFunc, cfg config.Config) { g, err := graph.Open(cfg.DataDir) if err != nil { slog.Error("graph open failed", "error", err) os.Exit(1) } broker := activity.New(120) sourceStore, err := sourceagent.OpenStore(cfg.DataDir) if err != nil { slog.Error("source agent store open failed", "error", err) _ = g.Close() os.Exit(1) } defer sourceStore.Close() if requeued, err := sourceStore.EnsureInboxClassifierVersion(ctx, engine.SourceInboxClassifierVersion); err != nil { slog.Warn("source inbox classifier migration failed", "error", err) } else if requeued > 0 { slog.Info("source inbox documents queued for classifier upgrade", "documents", requeued, "classifier_version", engine.SourceInboxClassifierVersion) } eng := engine.New(cfg, g, broker) eng.SetSourceInbox(sourceStore) eng.Start(ctx) watcher := ingest.NewAgentWatcher(cfg.AgentRunsFiles, g, broker) watcher.Start(ctx) ui := &webui.Server{Engine: eng, Graph: g, Broker: broker, APIKey: cfg.APIKey, PublicURL: cfg.BrainPublicURL, SourceAgents: sourceStore} srv := &http.Server{Addr: cfg.ListenAddr, Handler: ui.Handler(), ReadHeaderTimeout: 10 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 10 * time.Minute, IdleTimeout: 90 * time.Second} go func() { storage := g.StorageStatus() slog.Info("neural brain listening", "mode", "brain", "addr", cfg.ListenAddr, "knowledge_dirs", cfg.KnowledgeDirs, "staging_dirs", cfg.StagingDirs, "graph_backend", storage.Backend, "graph_db", storage.Path) if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { slog.Error("server failed", "error", err) cancel() } }() <-ctx.Done() shutdown, c := context.WithTimeout(context.Background(), 10*time.Second) defer c() _ = srv.Shutdown(shutdown) _ = eng.Flush(shutdown) if err := g.Close(); err != nil { slog.Error("graph close failed", "error", err) } }