add htmx web frontend with templates and session auth

This commit is contained in:
2026-01-18 21:11:23 +01:00
parent 1bc112047d
commit 01a0ac68a4
11 changed files with 969 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
package api
import (
"log"
"net/http"
"github.com/go-chi/chi/v5"
@@ -11,6 +12,7 @@ import (
"github.com/omixlab/mosis-portal/internal/auth"
"github.com/omixlab/mosis-portal/internal/config"
"github.com/omixlab/mosis-portal/internal/database"
"github.com/omixlab/mosis-portal/internal/web"
)
// NewRouter creates and configures the HTTP router
@@ -122,5 +124,51 @@ func NewRouter(cfg *config.Config, db *database.DB) http.Handler {
r.Post("/review/{versionID}/reject", handlers.NotImplemented)
})
// Web UI routes (htmx + Go templates)
webHandler, err := web.NewHandler(db)
if err != nil {
log.Printf("Warning: Failed to initialize web handler: %v", err)
} else {
sessionMW := web.NewSessionMiddleware(db, cfg.JWTSecret)
// Public web pages
r.Group(func(r chi.Router) {
r.Use(sessionMW.LoadSession)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
})
r.Get("/login", webHandler.Login)
})
// Protected web pages
r.Group(func(r chi.Router) {
r.Use(sessionMW.LoadSession)
r.Use(sessionMW.RequireSession)
r.Get("/dashboard", webHandler.Dashboard)
r.Get("/apps/new", webHandler.AppNew)
r.Get("/apps/{appID}", webHandler.AppDetail)
// htmx partials
r.Get("/partials/apps", webHandler.AppListPartial)
})
// Auth callback that sets session (after OAuth)
r.Get("/auth/callback", func(w http.ResponseWriter, r *http.Request) {
developerID := r.URL.Query().Get("developer_id")
if developerID == "" {
http.Redirect(w, r, "/login?error=Authentication failed", http.StatusSeeOther)
return
}
web.SetSession(w, developerID)
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
})
// Logout (clears session)
r.Get("/auth/logout", func(w http.ResponseWriter, r *http.Request) {
web.ClearSession(w)
http.Redirect(w, r, "/login", http.StatusSeeOther)
})
}
return r
}