Files
logpile/internal/server/server.go
2026-02-25 13:28:19 +03:00

157 lines
3.7 KiB
Go

package server
import (
"context"
"embed"
"fmt"
"io/fs"
"net/http"
"sync"
"time"
"git.mchus.pro/mchus/logpile/internal/collector"
"git.mchus.pro/mchus/logpile/internal/models"
)
// WebFS holds embedded web files (set by main package)
var WebFS embed.FS
type Config struct {
Port int
PreloadFile string
AppVersion string
AppCommit string
}
type Server struct {
config Config
mux *http.ServeMux
httpServer *http.Server
mu sync.RWMutex
result *models.AnalysisResult
detectedVendor string
rawExport *RawExportPackage
jobManager *JobManager
collectors *collector.Registry
}
func New(cfg Config) *Server {
s := &Server{
config: cfg,
mux: http.NewServeMux(),
jobManager: NewJobManager(),
collectors: collector.NewDefaultRegistry(),
}
s.setupRoutes()
return s
}
func (s *Server) setupRoutes() {
// Static files
staticContent, err := fs.Sub(WebFS, "static")
if err != nil {
panic(err)
}
s.mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(staticContent))))
// Pages
s.mux.HandleFunc("/", s.handleIndex)
// API endpoints
s.mux.HandleFunc("POST /api/upload", s.handleUpload)
s.mux.HandleFunc("GET /api/status", s.handleGetStatus)
s.mux.HandleFunc("GET /api/parsers", s.handleGetParsers)
s.mux.HandleFunc("GET /api/events", s.handleGetEvents)
s.mux.HandleFunc("GET /api/sensors", s.handleGetSensors)
s.mux.HandleFunc("GET /api/config", s.handleGetConfig)
s.mux.HandleFunc("GET /api/serials", s.handleGetSerials)
s.mux.HandleFunc("GET /api/firmware", s.handleGetFirmware)
s.mux.HandleFunc("GET /api/parse-errors", s.handleGetParseErrors)
s.mux.HandleFunc("GET /api/export/csv", s.handleExportCSV)
s.mux.HandleFunc("GET /api/export/json", s.handleExportJSON)
s.mux.HandleFunc("GET /api/export/reanimator", s.handleExportReanimator)
s.mux.HandleFunc("DELETE /api/clear", s.handleClear)
s.mux.HandleFunc("POST /api/shutdown", s.handleShutdown)
s.mux.HandleFunc("POST /api/collect", s.handleCollectStart)
s.mux.HandleFunc("GET /api/collect/{id}", s.handleCollectStatus)
s.mux.HandleFunc("POST /api/collect/{id}/cancel", s.handleCollectCancel)
}
func (s *Server) Run() error {
addr := fmt.Sprintf(":%d", s.config.Port)
s.httpServer = &http.Server{
Addr: addr,
Handler: s.mux,
}
return s.httpServer.ListenAndServe()
}
// Shutdown gracefully shuts down the server
func (s *Server) Shutdown() {
if s.httpServer != nil {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
s.httpServer.Shutdown(ctx)
}
}
// SetResult sets the analysis result (thread-safe)
func (s *Server) SetResult(result *models.AnalysisResult) {
s.mu.Lock()
defer s.mu.Unlock()
s.result = result
}
// GetResult returns the analysis result (thread-safe)
func (s *Server) GetResult() *models.AnalysisResult {
s.mu.RLock()
defer s.mu.RUnlock()
return s.result
}
func (s *Server) SetRawExport(pkg *RawExportPackage) {
s.mu.Lock()
defer s.mu.Unlock()
s.rawExport = pkg
}
func (s *Server) GetRawExport() *RawExportPackage {
s.mu.RLock()
defer s.mu.RUnlock()
if s.rawExport == nil {
return nil
}
cloned := *s.rawExport
return &cloned
}
func (s *Server) ClientVersionString() string {
s.mu.RLock()
defer s.mu.RUnlock()
v := s.config.AppVersion
c := s.config.AppCommit
if v == "" {
v = "dev"
}
if c == "" {
c = "none"
}
return fmt.Sprintf("LOGPile %s (commit: %s)", v, c)
}
// SetDetectedVendor sets the detected vendor name
func (s *Server) SetDetectedVendor(vendor string) {
s.mu.Lock()
defer s.mu.Unlock()
s.detectedVendor = vendor
}
// GetDetectedVendor returns the detected vendor name
func (s *Server) GetDetectedVendor() string {
s.mu.RLock()
defer s.mu.RUnlock()
return s.detectedVendor
}