initial
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"crowdsec-dashy/internal/config"
|
||||
"crowdsec-dashy/internal/crowdsec"
|
||||
"crowdsec-dashy/internal/router"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// ----------------------------------------------------------------
|
||||
// Configuration
|
||||
// ----------------------------------------------------------------
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("configuration error: %v", err)
|
||||
}
|
||||
|
||||
// Resolve paths
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalf("cannot determine working directory: %v", err)
|
||||
}
|
||||
staticDir := filepath.Join(cwd, "web", "static")
|
||||
templateDir := filepath.Join(cwd, "web", "templates")
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// CrowdSec LAPI — authenticate at startup
|
||||
// ----------------------------------------------------------------
|
||||
lapi := crowdsec.NewLAPIClient(cfg.CrowdSecAPIURL, cfg.CrowdSecAPILogin, cfg.CrowdSecAPIPassword)
|
||||
|
||||
log.Printf("connecting to CrowdSec LAPI at %s ...", cfg.CrowdSecAPIURL)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
if err := lapi.Login(ctx); err != nil {
|
||||
cancel()
|
||||
log.Fatalf("failed to authenticate with CrowdSec LAPI: %v\n"+
|
||||
"Ensure CROWDSEC_API_LOGIN and CROWDSEC_API_PASSWORD are correct and\n"+
|
||||
"the machine is registered: cscli machines add %s -a", err, cfg.CrowdSecAPILogin)
|
||||
}
|
||||
cancel()
|
||||
log.Println("authenticated with CrowdSec LAPI")
|
||||
|
||||
// CLI availability
|
||||
if cfg.CscliAvailable() {
|
||||
log.Printf("cscli available at %s", cfg.CscliPath)
|
||||
} else {
|
||||
log.Printf("[WARN] cscli not found at %s — bouncer/machine/hub/metrics features disabled", cfg.CscliPath)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Build router
|
||||
// ----------------------------------------------------------------
|
||||
handler, err := router.New(cfg, staticDir, templateDir)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to initialise router: %v", err)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// HTTP server
|
||||
// ----------------------------------------------------------------
|
||||
srv := &http.Server{
|
||||
Addr: cfg.Port,
|
||||
Handler: handler,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 60 * time.Second, // longer for hub operations
|
||||
IdleTimeout: 120 * time.Second,
|
||||
}
|
||||
|
||||
log.Printf("CrowdSec UI listening on %s", srv.Addr)
|
||||
log.Printf("UI credentials: %s / [redacted]", cfg.UIUsername)
|
||||
if err := srv.ListenAndServe(); err != nil {
|
||||
log.Fatalf("server error: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user