init
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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"
|
||||
webui "github.com/local/glpi-neural-brain/internal/web"
|
||||
)
|
||||
|
||||
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})))
|
||||
g, err := graph.Open(cfg.DataDir)
|
||||
if err != nil {
|
||||
slog.Error("graph open failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
broker := activity.New(120)
|
||||
eng := engine.New(cfg, g, broker)
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
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}
|
||||
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() {
|
||||
slog.Info("neural brain listening", "addr", cfg.ListenAddr, "knowledge_dirs", cfg.KnowledgeDirs, "staging_dirs", cfg.StagingDirs)
|
||||
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()
|
||||
_ = g.Persist()
|
||||
_ = srv.Shutdown(shutdown)
|
||||
}
|
||||
Reference in New Issue
Block a user