144 lines
4.5 KiB
Go
144 lines
4.5 KiB
Go
package http
|
|
|
|
import (
|
|
"encoding/json"
|
|
"html/template"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/example/storage-appliance/internal/domain"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
var templates *template.Template
|
|
|
|
func init() {
|
|
var err error
|
|
templates, err = template.ParseGlob("internal/templates/*.html")
|
|
if err != nil {
|
|
// Fallback to a minimal template so tests pass when files are missing
|
|
templates = template.New("dashboard.html")
|
|
templates.New("dashboard.html").Parse(`<div>{{.Title}}</div>`)
|
|
}
|
|
}
|
|
|
|
func (a *App) DashboardHandler(w http.ResponseWriter, r *http.Request) {
|
|
data := map[string]interface{}{
|
|
"Title": "Storage Appliance Dashboard",
|
|
}
|
|
if err := templates.ExecuteTemplate(w, "base", data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func (a *App) PoolsHandler(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
pools, err := a.ZFSSvc.ListPools(ctx)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
j, err := json.Marshal(pools)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(j)
|
|
}
|
|
|
|
func (a *App) JobsHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`[]`))
|
|
}
|
|
|
|
// CreatePoolHandler receives a request to create a pool and enqueues a job
|
|
func (a *App) CreatePoolHandler(w http.ResponseWriter, r *http.Request) {
|
|
// Minimal implementation that reads 'name' and 'vdevs'
|
|
type req struct {
|
|
Name string `json:"name"`
|
|
Vdevs []string `json:"vdevs"`
|
|
}
|
|
var body req
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
http.Error(w, "bad request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// Create a job and enqueue
|
|
j := domain.Job{Type: "create-pool", Status: "queued", Progress: 0}
|
|
id, err := a.JobRunner.Enqueue(r.Context(), j)
|
|
if err != nil {
|
|
http.Error(w, "failed to create job", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"job_id":"` + id + `"}`))
|
|
}
|
|
|
|
func StaticHandler(w http.ResponseWriter, r *http.Request) {
|
|
p := r.URL.Path[len("/static/"):]
|
|
http.ServeFile(w, r, filepath.Join("static", p))
|
|
}
|
|
|
|
// StorageHandler renders the main storage page
|
|
func (a *App) StorageHandler(w http.ResponseWriter, r *http.Request) {
|
|
data := map[string]interface{}{
|
|
"Title": "Storage",
|
|
}
|
|
if err := templates.ExecuteTemplate(w, "base", data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
// HXPoolsHandler renders a pools partial (HTMX)
|
|
func (a *App) HXPoolsHandler(w http.ResponseWriter, r *http.Request) {
|
|
pools, _ := a.StorageSvc.ListPools(r.Context())
|
|
if err := templates.ExecuteTemplate(w, "hx_pools", pools); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
// StorageCreatePoolHandler handles HTMX pool create POST; expects form values `name` and `vdevs` (comma-separated)
|
|
func (a *App) StorageCreatePoolHandler(w http.ResponseWriter, r *http.Request) {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, "bad request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
name := r.FormValue("name")
|
|
vdevsRaw := r.FormValue("vdevs")
|
|
vdevs := []string{}
|
|
if vdevsRaw != "" {
|
|
vdevs = append(vdevs, strings.Split(vdevsRaw, ",")...)
|
|
}
|
|
user, _ := r.Context().Value(ContextKey("user")).(string)
|
|
role, _ := r.Context().Value(ContextKey("user.role")).(string)
|
|
jobID, err := a.StorageSvc.CreatePool(r.Context(), user, role, name, vdevs)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
return
|
|
}
|
|
// Return a small job row partial as response
|
|
data := map[string]string{"JobID": jobID, "Name": name}
|
|
if err := templates.ExecuteTemplate(w, "job_row", data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
// JobPartialHandler returns a job progress partial by id
|
|
func (a *App) JobPartialHandler(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
// Read job status from DB
|
|
row := a.DB.QueryRowContext(r.Context(), `SELECT id, type, status, progress FROM jobs WHERE id = ?`, id)
|
|
var jid, jtype, status string
|
|
var progress int
|
|
if err := row.Scan(&jid, &jtype, &status, &progress); err != nil {
|
|
http.Error(w, "job not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
data := map[string]any{"JobID": jid, "Type": jtype, "Status": status, "Progress": progress}
|
|
if err := templates.ExecuteTemplate(w, "job_row", data); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|