package main import ( "fmt" "html/template" "log" "net/http" "os" "gitea.tecamino.local/paadi/licenseServer/internal/handlers" "gitea.tecamino.local/paadi/licenseServer/internal/handlers/middleware" "gitea.tecamino.local/paadi/licenseServer/internal/models" "github.com/go-chi/chi/v5" chimiddleware "github.com/go-chi/chi/v5/middleware" ) func main() { // Config from env with sensible defaults port := getenv("PORT", "8080") dbPath := getenv("DB_PATH", "./licenses.db") // Database db, err := models.Open(dbPath) if err != nil { log.Fatalf("failed to open database: %v", err) } log.Printf("database opened: %s", dbPath) tmpl := template.Must( template.New("").ParseGlob("templates/*.html"), ) // Session store sessions := middleware.NewSessionStore() // Handler h := &handlers.Handler{ DB: db, Sessions: sessions, Tmpl: tmpl, } // Router r := chi.NewRouter() r.Use(chimiddleware.Logger) r.Use(chimiddleware.Recoverer) // Public routes r.Get("/setup", h.ShowSetup) r.Post("/setup", h.PostSetup) r.Get("/login", h.ShowLogin) r.Post("/login", h.PostLogin) // REST API (for client apps) — no web auth needed, validate by license key r.Post("/api/v1/validate", h.APIValidate) // Protected web UI routes r.Group(func(r chi.Router) { r.Use(sessions.RequireAuth) r.Get("/", h.Dashboard) r.Post("/logout", h.PostLogout) r.Get("/licenses/new", h.ShowNewLicense) r.Post("/licenses", h.PostNewLicense) r.Delete("/licenses/{id}/revoke", h.RevokeLicense) r.Post("/licenses/{id}/reinstate", h.ReinstateLicense) }) // Redirect root to setup if no users exist addr := fmt.Sprintf(":%s", port) log.Printf("LicenseVault running on http://localhost%s", addr) log.Fatal(http.ListenAndServe(addr, r)) } func getenv(key, fallback string) string { if v := os.Getenv(key); v != "" { return v } return fallback }