Add initial Go server skeleton with HTTP handlers, middleware, job runner, and stubs
This commit is contained in:
80
internal/http/handlers.go
Normal file
80
internal/http/handlers.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/example/storage-appliance/internal/domain"
|
||||
)
|
||||
|
||||
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, "dashboard.html", 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))
|
||||
}
|
||||
Reference in New Issue
Block a user