base dashboard and login
This commit is contained in:
+28
-12
@@ -2,34 +2,49 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"crowdsec-dashy/internal/config"
|
||||
"crowdsec-dashy/internal/crowdsec"
|
||||
"crowdsec-dashy/internal/router"
|
||||
webfiles "crowdsec-dashy/web"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// ----------------------------------------------------------------
|
||||
// CLI flags
|
||||
// ----------------------------------------------------------------
|
||||
pwhash := flag.String("pwhash", "", "hash a password for use as ui_password in app_config.conf")
|
||||
flag.Parse()
|
||||
|
||||
if *pwhash != "" {
|
||||
hash, err := config.HashPassword(*pwhash)
|
||||
if err != nil {
|
||||
log.Fatalf("hash password: %v", err)
|
||||
}
|
||||
fmt.Println(hash)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
// Configuration
|
||||
// ----------------------------------------------------------------
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
var firstRun *config.FirstRunError
|
||||
if errors.As(err, &firstRun) {
|
||||
log.Println(firstRun.Error())
|
||||
os.Exit(0)
|
||||
}
|
||||
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
|
||||
// ----------------------------------------------------------------
|
||||
@@ -40,8 +55,9 @@ func main() {
|
||||
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)
|
||||
"Check crowdsec_api_login / crowdsec_api_password in %s\n"+
|
||||
"Register the machine first: cscli machines add %s -a",
|
||||
err, config.ConfigFile(), cfg.CrowdSecAPILogin)
|
||||
}
|
||||
cancel()
|
||||
log.Println("authenticated with CrowdSec LAPI")
|
||||
@@ -56,7 +72,7 @@ func main() {
|
||||
// ----------------------------------------------------------------
|
||||
// Build router
|
||||
// ----------------------------------------------------------------
|
||||
handler, err := router.New(cfg, staticDir, templateDir)
|
||||
handler, err := router.New(cfg, lapi, webfiles.FS)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to initialise router: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user