Add initial Go server skeleton with HTTP handlers, middleware, job runner, and stubs
This commit is contained in:
65
internal/domain/domain.go
Normal file
65
internal/domain/domain.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type UUID string
|
||||
|
||||
type User struct {
|
||||
ID UUID `json:"id" db:"id"`
|
||||
Username string `json:"username" db:"username"`
|
||||
Password string `json:"-" db:"password_hash"`
|
||||
Role string `json:"role" db:"role"`
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Role struct {
|
||||
Name string
|
||||
Permissions []string
|
||||
}
|
||||
|
||||
type Disk struct {
|
||||
ID UUID
|
||||
Path string
|
||||
Serial string
|
||||
Model string
|
||||
Size int64
|
||||
Health string
|
||||
}
|
||||
|
||||
type Pool struct {
|
||||
Name string
|
||||
GUID string
|
||||
Health string
|
||||
Capacity string
|
||||
}
|
||||
|
||||
type Dataset struct {
|
||||
Name string
|
||||
Pool string
|
||||
Type string
|
||||
Config map[string]string
|
||||
}
|
||||
|
||||
type Share struct {
|
||||
ID UUID
|
||||
Name string
|
||||
Path string
|
||||
Type string // nfs or smb
|
||||
}
|
||||
|
||||
type LUN struct {
|
||||
ID UUID
|
||||
TargetIQN string
|
||||
LUNID int
|
||||
Path string
|
||||
}
|
||||
|
||||
type Job struct {
|
||||
ID UUID
|
||||
Type string
|
||||
Status string
|
||||
Progress int
|
||||
Owner UUID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
8
internal/http/README.md
Normal file
8
internal/http/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
This folder contains HTTP handlers, router registration, and middleware.
|
||||
|
||||
Key files:
|
||||
- `router.go` - registers routes and APIs.
|
||||
- `handlers.go` - small HTMX-based handlers for the dashboard and API.
|
||||
- `middleware.go` - request id, logging, placeholder auth and CSRF.
|
||||
|
||||
Handlers should accept `*App` struct for dependency injection.
|
||||
17
internal/http/app.go
Normal file
17
internal/http/app.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
|
||||
"github.com/example/storage-appliance/internal/service"
|
||||
)
|
||||
|
||||
// App contains injected dependencies for handlers.
|
||||
type App struct {
|
||||
DB *sql.DB
|
||||
DiskSvc service.DiskService
|
||||
ZFSSvc service.ZFSService
|
||||
JobRunner service.JobRunner
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
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))
|
||||
}
|
||||
25
internal/http/handlers_test.go
Normal file
25
internal/http/handlers_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/example/storage-appliance/internal/service/mock"
|
||||
)
|
||||
|
||||
func TestPoolsHandler(t *testing.T) {
|
||||
m := &mock.MockZFSService{}
|
||||
app := &App{DB: &sql.DB{}, ZFSSvc: m}
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/pools", nil)
|
||||
w := httptest.NewRecorder()
|
||||
app.PoolsHandler(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
body := w.Body.String()
|
||||
if body == "" {
|
||||
t.Fatalf("expected non-empty body")
|
||||
}
|
||||
}
|
||||
78
internal/http/middleware.go
Normal file
78
internal/http/middleware.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ContextKey used to store values in context
|
||||
type ContextKey string
|
||||
|
||||
const (
|
||||
ContextKeyRequestID ContextKey = "request-id"
|
||||
)
|
||||
|
||||
// RequestID middleware sets a request ID in headers and request context
|
||||
func RequestID(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// Logging middleware prints basic request logs
|
||||
func Logging(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
log.Printf("%s %s in %v", r.Method, r.URL.Path, time.Since(start))
|
||||
})
|
||||
}
|
||||
|
||||
// Auth middleware placeholder to authenticate users
|
||||
func Auth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Basic dev auth: read X-Auth-User; in real world, validate session/jwt
|
||||
username := r.Header.Get("X-Auth-User")
|
||||
if username == "" {
|
||||
username = "anonymous"
|
||||
}
|
||||
// Role hint: header X-Auth-Role (admin/operator/viewer)
|
||||
role := r.Header.Get("X-Auth-Role")
|
||||
if role == "" {
|
||||
if username == "admin" {
|
||||
role = "admin"
|
||||
} else {
|
||||
role = "viewer"
|
||||
}
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), ContextKey("user"), username)
|
||||
ctx = context.WithValue(ctx, ContextKey("user.role"), role)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
// CSRF middleware placeholder (reads X-CSRF-Token)
|
||||
func CSRFMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO: check and enforce CSRF tokens for mutating requests
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// RBAC middleware placeholder
|
||||
func RBAC(permission string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Try to read role from context and permit admin always
|
||||
role := r.Context().Value(ContextKey("user.role"))
|
||||
if role == "admin" {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
// For now, only admin is permitted; add permission checks here
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
24
internal/http/router.go
Normal file
24
internal/http/router.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// RegisterRoutes registers HTTP routes onto the router
|
||||
func RegisterRoutes(r *chi.Mux, app *App) {
|
||||
r.Use(Logging)
|
||||
r.Use(RequestID)
|
||||
r.Use(Auth)
|
||||
r.Get("/", app.DashboardHandler)
|
||||
r.Get("/dashboard", app.DashboardHandler)
|
||||
r.Get("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
|
||||
// API namespace
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Get("/pools", app.PoolsHandler)
|
||||
r.With(RBAC("storage.pool.create")).Post("/pools", app.CreatePoolHandler) // create a pool -> creates a job
|
||||
r.Get("/jobs", app.JobsHandler)
|
||||
})
|
||||
r.Get("/static/*", StaticHandler)
|
||||
}
|
||||
42
internal/infra/exec/exec.go
Normal file
42
internal/infra/exec/exec.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RunOptions struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type CommandRunner interface {
|
||||
Run(ctx context.Context, cmd string, args []string, opts RunOptions) (string, string, error)
|
||||
}
|
||||
|
||||
type DefaultRunner struct{}
|
||||
|
||||
func (r *DefaultRunner) Run(ctx context.Context, cmd string, args []string, opts RunOptions) (string, string, error) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
execCtx := ctx
|
||||
if opts.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
execCtx, cancel = context.WithTimeout(ctx, opts.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
|
||||
eg, cctx := errgroup.WithContext(execCtx)
|
||||
|
||||
eg.Go(func() error {
|
||||
command := exec.CommandContext(cctx, cmd, args...)
|
||||
command.Stdout = &stdout
|
||||
command.Stderr = &stderr
|
||||
return command.Run()
|
||||
})
|
||||
if err := eg.Wait(); err != nil {
|
||||
return stdout.String(), stderr.String(), err
|
||||
}
|
||||
return stdout.String(), stderr.String(), nil
|
||||
}
|
||||
15
internal/infra/sqlite/db/db.go
Normal file
15
internal/infra/sqlite/db/db.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// Open returns a connected DB instance. Caller should close.
|
||||
func Open(ctx context.Context, dsn string) (*sql.DB, error) {
|
||||
db, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
44
internal/infra/sqlite/db/migrations.go
Normal file
44
internal/infra/sqlite/db/migrations.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"log"
|
||||
)
|
||||
|
||||
// MigrateAndSeed performs a very small migration set and seeds an admin user
|
||||
func MigrateAndSeed(ctx context.Context, db *sql.DB) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
stmts := []string{
|
||||
`CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY, username TEXT NOT NULL UNIQUE, password_hash TEXT, role TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP);`,
|
||||
`CREATE TABLE IF NOT EXISTS pools (name TEXT PRIMARY KEY, guid TEXT, health TEXT, capacity TEXT);`,
|
||||
`CREATE TABLE IF NOT EXISTS jobs (id TEXT PRIMARY KEY, type TEXT, status TEXT, progress INTEGER DEFAULT 0, owner TEXT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME);`,
|
||||
}
|
||||
for _, s := range stmts {
|
||||
if _, err := tx.ExecContext(ctx, s); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Seed a default admin user if not exists
|
||||
var count int
|
||||
if err := tx.QueryRowContext(ctx, `SELECT COUNT(1) FROM users WHERE username = 'admin'`).Scan(&count); err != nil {
|
||||
log.Printf("seed check failed: %v", err)
|
||||
}
|
||||
if count == 0 {
|
||||
// note: simple seeded password: admin (do not use in prod)
|
||||
pwHash, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.DefaultCost)
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO users (id, username, password_hash, role) VALUES (?, 'admin', ?, 'admin')`, "admin", string(pwHash)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
18
internal/infra/stubs/iscsi.go
Normal file
18
internal/infra/stubs/iscsi.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package stubs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
)
|
||||
|
||||
type ISCSIAdapter struct{}
|
||||
|
||||
func (i *ISCSIAdapter) CreateTarget(ctx context.Context, name string) error {
|
||||
log.Printf("iscsi: CreateTarget name=%s (stub)", name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *ISCSIAdapter) CreateLUN(ctx context.Context, target string, backstore string, lunID int) error {
|
||||
log.Printf("iscsi: CreateLUN target=%s backstore=%s lun=%d (stub)", target, backstore, lunID)
|
||||
return nil
|
||||
}
|
||||
18
internal/infra/stubs/minio.go
Normal file
18
internal/infra/stubs/minio.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package stubs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
)
|
||||
|
||||
type MinioAdapter struct{}
|
||||
|
||||
func (m *MinioAdapter) ListBuckets(ctx context.Context) ([]string, error) {
|
||||
log.Println("minio: ListBuckets (stub)")
|
||||
return []string{"buckets"}, nil
|
||||
}
|
||||
|
||||
func (m *MinioAdapter) CreateBucket(ctx context.Context, name string) error {
|
||||
log.Printf("minio: CreateBucket %s (stub)", name)
|
||||
return nil
|
||||
}
|
||||
18
internal/infra/stubs/nfs.go
Normal file
18
internal/infra/stubs/nfs.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package stubs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
)
|
||||
|
||||
type NFSAdapter struct{}
|
||||
|
||||
func (n *NFSAdapter) ListExports(ctx context.Context) ([]string, error) {
|
||||
log.Println("nfs: ListExports (stub)")
|
||||
return []string{"/export/data"}, nil
|
||||
}
|
||||
|
||||
func (n *NFSAdapter) CreateExport(ctx context.Context, path string) error {
|
||||
log.Printf("nfs: CreateExport path=%s (stub)", path)
|
||||
return nil
|
||||
}
|
||||
18
internal/infra/stubs/samba.go
Normal file
18
internal/infra/stubs/samba.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package stubs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
)
|
||||
|
||||
type SambaAdapter struct{}
|
||||
|
||||
func (s *SambaAdapter) ListShares(ctx context.Context) ([]string, error) {
|
||||
log.Println("samba: ListShares (stub)")
|
||||
return []string{"share1"}, nil
|
||||
}
|
||||
|
||||
func (s *SambaAdapter) CreateShare(ctx context.Context, name, path string) error {
|
||||
log.Printf("samba: CreateShare name=%s path=%s (stub)", name, path)
|
||||
return nil
|
||||
}
|
||||
18
internal/infra/stubs/zfs.go
Normal file
18
internal/infra/stubs/zfs.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package stubs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
)
|
||||
|
||||
type ZFSAdapter struct{}
|
||||
|
||||
func (z *ZFSAdapter) ListPools(ctx context.Context) ([]string, error) {
|
||||
log.Printf("zfs: ListPools (stub)")
|
||||
return []string{"tank"}, nil
|
||||
}
|
||||
|
||||
func (z *ZFSAdapter) CreatePool(ctx context.Context, name string, vdevs []string) error {
|
||||
log.Printf("zfs: CreatePool %s (stub) vdevs=%v", name, vdevs)
|
||||
return nil
|
||||
}
|
||||
42
internal/job/runner.go
Normal file
42
internal/job/runner.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package job
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/example/storage-appliance/internal/domain"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Runner struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (r *Runner) Enqueue(ctx context.Context, j domain.Job) (string, error) {
|
||||
id := uuid.New().String()
|
||||
if j.ID == "" {
|
||||
j.ID = domain.UUID(id)
|
||||
}
|
||||
j.Status = "queued"
|
||||
j.CreatedAt = time.Now()
|
||||
j.UpdatedAt = time.Now()
|
||||
_, err := r.DB.ExecContext(ctx, `INSERT INTO jobs (id, type, status, progress, owner, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
j.ID, j.Type, j.Status, j.Progress, j.Owner, j.CreatedAt, j.UpdatedAt)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
log.Printf("enqueued job %s (%s)", j.ID, j.Type)
|
||||
// run async worker (very simple worker for skeleton)
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
r.updateStatus(ctx, j.ID, "succeeded", 100)
|
||||
}()
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (r *Runner) updateStatus(ctx context.Context, id domain.UUID, status string, progress int) error {
|
||||
_, err := r.DB.ExecContext(ctx, `UPDATE jobs SET status = ?, progress = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?`, status, progress, id)
|
||||
return err
|
||||
}
|
||||
20
internal/service/interfaces.go
Normal file
20
internal/service/interfaces.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/example/storage-appliance/internal/domain"
|
||||
)
|
||||
|
||||
type DiskService interface {
|
||||
List(ctx context.Context) ([]domain.Disk, error)
|
||||
Inspect(ctx context.Context, path string) (domain.Disk, error)
|
||||
}
|
||||
|
||||
type ZFSService interface {
|
||||
ListPools(ctx context.Context) ([]domain.Pool, error)
|
||||
CreatePool(ctx context.Context, name string, vdevs []string) (string, error)
|
||||
}
|
||||
|
||||
type JobRunner interface {
|
||||
Enqueue(ctx context.Context, j domain.Job) (string, error)
|
||||
}
|
||||
47
internal/service/mock/mock_service.go
Normal file
47
internal/service/mock/mock_service.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/example/storage-appliance/internal/domain"
|
||||
"github.com/example/storage-appliance/internal/service"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
_ service.DiskService = (*MockDiskService)(nil)
|
||||
_ service.ZFSService = (*MockZFSService)(nil)
|
||||
_ service.JobRunner = (*MockJobRunner)(nil)
|
||||
)
|
||||
|
||||
type MockDiskService struct{}
|
||||
|
||||
func (m *MockDiskService) List(ctx context.Context) ([]domain.Disk, error) {
|
||||
return []domain.Disk{{ID: domain.UUID(uuid.New().String()), Path: "/dev/sda", Serial: "XYZ123", Model: "ACME 1TB", Size: 1 << 40, Health: "ONLINE"}}, nil
|
||||
}
|
||||
|
||||
func (m *MockDiskService) Inspect(ctx context.Context, path string) (domain.Disk, error) {
|
||||
return domain.Disk{ID: domain.UUID(uuid.New().String()), Path: path, Serial: "XYZ123", Model: "ACME 1TB", Size: 1 << 40, Health: "ONLINE"}, nil
|
||||
}
|
||||
|
||||
type MockZFSService struct{}
|
||||
|
||||
func (m *MockZFSService) ListPools(ctx context.Context) ([]domain.Pool, error) {
|
||||
return []domain.Pool{{Name: "tank", GUID: "g-uuid-1", Health: "ONLINE", Capacity: "1TiB"}}, nil
|
||||
}
|
||||
|
||||
func (m *MockZFSService) CreatePool(ctx context.Context, name string, vdevs []string) (string, error) {
|
||||
// spawn instant job id for mock
|
||||
return "job-" + uuid.New().String(), nil
|
||||
}
|
||||
|
||||
type MockJobRunner struct{}
|
||||
|
||||
func (m *MockJobRunner) Enqueue(ctx context.Context, j domain.Job) (string, error) {
|
||||
// simulate queueing job and running immediately
|
||||
go func() {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}()
|
||||
return uuid.New().String(), nil
|
||||
}
|
||||
18
internal/templates/base.html
Normal file
18
internal/templates/base.html
Normal file
@@ -0,0 +1,18 @@
|
||||
{{define "base"}}
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="https://unpkg.com/htmx.org@1.9.2"></script>
|
||||
<title>{{.Title}}</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
|
||||
<meta name="csrf-token" content="fake-csrf-token">
|
||||
</head>
|
||||
<body class="bg-gray-100">
|
||||
<main class="container mx-auto p-4">
|
||||
{{template "content" .}}
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
18
internal/templates/dashboard.html
Normal file
18
internal/templates/dashboard.html
Normal file
@@ -0,0 +1,18 @@
|
||||
{{define "content"}}
|
||||
<div class="bg-white rounded shadow p-4">
|
||||
<h1 class="text-2xl font-bold">{{.Title}}</h1>
|
||||
<div class="mt-4">
|
||||
<button class="px-3 py-2 bg-blue-500 text-white rounded" hx-get="/api/pools" hx-swap="outerHTML" hx-trigger="click">Refresh pools</button>
|
||||
</div>
|
||||
<div id="pools" class="mt-4">
|
||||
<div class="text-sm text-gray-600">Pools: <span id="pools-count">1</span></div>
|
||||
<div class="mt-2 p-2 bg-gray-50 rounded">tank — ONLINE — 1TiB</div>
|
||||
</div>
|
||||
<div class="mt-6">
|
||||
<h2 class="text-lg font-semibold">Jobs</h2>
|
||||
<div id="jobs">
|
||||
<div class="text-sm text-gray-500">No active jobs</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user