still working
This commit is contained in:
@@ -14,4 +14,5 @@ type App struct {
|
||||
ZFSSvc service.ZFSService
|
||||
JobRunner service.JobRunner
|
||||
HTTPClient *http.Client
|
||||
StorageSvc *storage.StorageService
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/example/storage-appliance/internal/domain"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
var templates *template.Template
|
||||
@@ -25,7 +27,7 @@ func (a *App) DashboardHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]interface{}{
|
||||
"Title": "Storage Appliance Dashboard",
|
||||
}
|
||||
if err := templates.ExecuteTemplate(w, "dashboard.html", data); err != nil {
|
||||
if err := templates.ExecuteTemplate(w, "base", data); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -78,3 +80,64 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/example/storage-appliance/internal/service/mock"
|
||||
@@ -23,3 +24,22 @@ func TestPoolsHandler(t *testing.T) {
|
||||
t.Fatalf("expected non-empty body")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreatePoolHandler(t *testing.T) {
|
||||
m := &mock.MockZFSService{}
|
||||
j := &mock.MockJobRunner{}
|
||||
app := &App{DB: &sql.DB{}, ZFSSvc: m, JobRunner: j}
|
||||
body := `{"name":"tank","vdevs":["/dev/sda"]}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/pools", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Auth-User", "admin")
|
||||
req.Header.Set("X-Auth-Role", "admin")
|
||||
w := httptest.NewRecorder()
|
||||
app.CreatePoolHandler(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
if !strings.Contains(w.Body.String(), "job_id") {
|
||||
t.Fatalf("expected job_id in response")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,5 +20,9 @@ func RegisterRoutes(r *chi.Mux, app *App) {
|
||||
r.With(RBAC("storage.pool.create")).Post("/pools", app.CreatePoolHandler) // create a pool -> creates a job
|
||||
r.Get("/jobs", app.JobsHandler)
|
||||
})
|
||||
r.Get("/storage", app.StorageHandler)
|
||||
r.Get("/hx/pools", app.HXPoolsHandler)
|
||||
r.Post("/storage/pool/create", app.StorageCreatePoolHandler)
|
||||
r.Get("/jobs/{id}", app.JobPartialHandler)
|
||||
r.Get("/static/*", StaticHandler)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user