Files
logpile/internal/server/server.go
Michael Chus 1b1bc74fc7 Add Reanimator format export support
Implement export to Reanimator format for asset tracking integration.

Features:
- New API endpoint: GET /api/export/reanimator
- Web UI button "Экспорт Reanimator" in Configuration tab
- Auto-detect CPU manufacturer (Intel/AMD/ARM/Ampere)
- Generate PCIe serial numbers if missing
- Merge GPUs and NetworkAdapters into pcie_devices
- Filter components without serial numbers
- RFC3339 timestamp format
- Full compliance with Reanimator specification

Changes:
- Add reanimator_models.go: data models for Reanimator format
- Add reanimator_converter.go: conversion functions
- Add reanimator_converter_test.go: unit tests
- Add reanimator_integration_test.go: integration tests
- Update handlers.go: add handleExportReanimator
- Update server.go: register /api/export/reanimator route
- Update index.html: add export button
- Update CLAUDE.md: document export behavior
- Add REANIMATOR_EXPORT.md: implementation summary

Tests: All tests passing (15+ new tests)
Format spec: example/docs/INTEGRATION_GUIDE.md

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-02-12 21:54:37 +03:00

124 lines
3.1 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
}
type Server struct {
config Config
mux *http.ServeMux
httpServer *http.Server
mu sync.RWMutex
result *models.AnalysisResult
detectedVendor string
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/export/csv", s.handleExportCSV)
s.mux.HandleFunc("GET /api/export/json", s.handleExportJSON)
s.mux.HandleFunc("GET /api/export/txt", s.handleExportTXT)
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
}
// 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
}