Replace Go HTML template rendering with a Bun + TSX build-time static site generator. Go backend becomes API-only for auth. Frontend: - Custom JSX-to-HTML-string factory (zero dependencies) - TSX components for Header, Footer, index page, login page - Client-side login.ts handles tab switching and fetch()-based auth - Bun bundler compiles client JS, build.ts renders pages to dist/ Backend: - Auth handlers return JSON (POST /api/login, POST /api/register) - Login page served as static HTML from dist/ - Static assets served from /assets/ (public) - Auth middleware unchanged (cookie check, redirect to /login) - Removed template parsing and renderPage Dockerfile: - 3-stage build: Bun frontend -> Go backend -> alpine runtime - Frontend dist copied to /app/dist in final image Removed: templates/, static/css/ (replaced by frontend/)
41 lines
1015 B
Go
41 lines
1015 B
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"mgit.msbls.de/m/patholo/internal/auth"
|
|
)
|
|
|
|
var authClient *auth.Client
|
|
|
|
func Register(mux *http.ServeMux, client *auth.Client) {
|
|
authClient = client
|
|
|
|
// API endpoints (JSON, public)
|
|
mux.HandleFunc("POST /api/login", handleAPILogin)
|
|
mux.HandleFunc("POST /api/register", handleAPIRegister)
|
|
|
|
// Public pages
|
|
mux.HandleFunc("GET /login", handleLoginPage)
|
|
mux.HandleFunc("GET /logout", handleLogout)
|
|
|
|
// Static assets (public)
|
|
mux.Handle("GET /assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("dist/assets"))))
|
|
|
|
// Protected routes
|
|
protected := http.NewServeMux()
|
|
protected.HandleFunc("GET /{$}", handleIndex)
|
|
mux.Handle("/", client.Middleware(protected))
|
|
}
|
|
|
|
func handleIndex(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "dist/index.html")
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, data any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
json.NewEncoder(w).Encode(data)
|
|
}
|