Files
KamadoPool/api/internal/httpapi/server.go
T
satoshi f1dc0a8a79 Persist best share glow, share animations, and UI polish
Backend:
- Persist best_diff as a high-water mark in kv store so it survives
  restarts and transient ckpool gaps
- Persist acked_best_diff so the "new best share" glow survives pool
  restarts and works across multiple new bests before dismissal
- Add POST /api/admin/ack-best and /api/admin/reset-ack-best endpoints

Frontend:
- Best share card: golden glow, shimmer, sparkles, "NEW BEST SHARE!"
  tag with previous value, "Nice" dismiss button
- Optimistic local override so Nice/test buttons respond instantly
  instead of waiting for next WebSocket push
- Fix shimmer sweep to go left-to-right (not start from middle)
- Share animations: reject (red flash + shake) suppresses accept;
  reject effect declared first so guard reads correct state
- Share tooltips show exact counts on hover
- Block reward tooltip shows full amount in satoshis
- Expected block uses 1h hashrate instead of 1m
- formatDuration uses full unit names; years always show 1 decimal
- formatWork always shows 2 decimal places
- Map chain "main" to "mainnet" in block height card
2026-05-12 21:56:02 +03:00

235 lines
7.5 KiB
Go

// Package httpapi serves the Kamado REST API. WebSocket push and static
// UI serving land in a follow-up commit.
package httpapi
import (
"encoding/json"
"errors"
"io/fs"
"log/slog"
"net/http"
"strings"
"github.com/kamadopool/kamado-api/internal/accelerator"
"github.com/kamadopool/kamado-api/internal/state"
"github.com/kamadopool/kamado-api/internal/webui"
)
type Server struct {
Agg *state.Aggregator
Acc *accelerator.Service
Hub *Hub
Log *slog.Logger
}
func New(agg *state.Aggregator, acc *accelerator.Service, log *slog.Logger) *Server {
return &Server{Agg: agg, Acc: acc, Hub: NewHub(), Log: log}
}
// Handler returns an http.Handler with all kamado routes mounted under
// /api and the embedded Svelte dashboard served under /. Unknown
// non-/api paths fall back to index.html for SPA-style routing.
func (s *Server) Handler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /api/health", s.health)
mux.HandleFunc("GET /api/pool", s.pool)
mux.HandleFunc("GET /api/users", s.users)
mux.HandleFunc("GET /api/workers", s.workers)
mux.HandleFunc("GET /api/clients", s.clients)
mux.HandleFunc("GET /api/blocks", s.blocks)
mux.HandleFunc("GET /api/snapshot", s.snapshot)
mux.HandleFunc("GET /api/ws", s.handleWS)
mux.HandleFunc("GET /api/admin/debug-blocks", s.debugBlocks)
mux.HandleFunc("POST /api/admin/reset-latency", s.resetLatency)
mux.HandleFunc("POST /api/admin/ack-best", s.ackBest)
mux.HandleFunc("POST /api/admin/reset-ack-best", s.resetAckBest)
mux.HandleFunc("POST /api/accelerate", s.accelerate)
mux.HandleFunc("POST /api/accelerate/cancel", s.accelerateCancel)
mux.HandleFunc("POST /api/accelerate/max", s.accelerateMax)
mux.HandleFunc("GET /api/accelerate/list", s.accelerateList)
mux.Handle("/", spaHandler(webui.FS()))
return mux
}
// spaHandler serves static files from the embedded dist tree and
// falls back to index.html on 404 so client-side routing works. It
// refuses anything under /api to keep the contract with mux patterns
// explicit (those routes register their own handlers above).
func spaHandler(root fs.FS) http.Handler {
fileServer := http.FileServerFS(root)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Defence-in-depth — /api routes are matched by the mux first
// with their GET patterns, but a bare POST /api/... would fall
// through here. Return 404 so we don't accidentally shadow
// API semantics with HTML.
if strings.HasPrefix(r.URL.Path, "/api/") || r.URL.Path == "/api" {
http.NotFound(w, r)
return
}
// Fast path: exact file exists in the embed.
clean := strings.TrimPrefix(r.URL.Path, "/")
if clean == "" {
clean = "index.html"
}
if _, err := fs.Stat(root, clean); err == nil {
fileServer.ServeHTTP(w, r)
return
} else if !errors.Is(err, fs.ErrNotExist) {
http.Error(w, "webui: "+err.Error(), http.StatusInternalServerError)
return
}
// SPA fallback: serve index.html with a 200 so reloads on a
// client-side route don't 404.
r2 := r.Clone(r.Context())
r2.URL.Path = "/"
fileServer.ServeHTTP(w, r2)
})
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
// ---- handlers ---------------------------------------------------------
func (s *Server) health(w http.ResponseWriter, r *http.Request) {
snap := s.Agg.Snapshot()
status := http.StatusOK
// Submit-attempt gap: if we've tried to submit more blocks than have
// been confirmed, surface the gap. A non-zero gap is a strong signal
// even if everything else looks healthy.
submitGap := snap.BlockSubmitAttempts - snap.BlockSubmitsConfirmed
if submitGap < 0 {
submitGap = 0
}
// ZMQ freshness: only meaningful if the operator enabled it. Stale
// = no event in 30 minutes (typical mainnet block interval is 10
// min, but spikes happen).
zmqStale := false
if snap.ZMQEnabled && snap.HasLastZMQEvent && snap.LastZMQEventAge > 1800 {
zmqStale = true
}
overallOK := snap.CKPoolOK && snap.BitcoinOK && submitGap == 0 && !zmqStale
if !snap.CKPoolOK || !snap.BitcoinOK {
status = http.StatusServiceUnavailable
}
writeJSON(w, status, map[string]any{
"ok": overallOK,
"ckpool": snap.CKPoolOK,
"bitcoin": snap.BitcoinOK,
"submit_attempts": snap.BlockSubmitAttempts,
"submits_confirmed": snap.BlockSubmitsConfirmed,
"submit_gap": submitGap,
"zmq_enabled": snap.ZMQEnabled,
"zmq_event_age_seconds": snap.LastZMQEventAge,
"zmq_has_event": snap.HasLastZMQEvent,
"zmq_stale": zmqStale,
"last_error": snap.LastError,
})
}
func (s *Server) snapshot(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, s.Agg.Snapshot())
}
func (s *Server) pool(w http.ResponseWriter, r *http.Request) {
snap := s.Agg.Snapshot()
writeJSON(w, http.StatusOK, map[string]any{
"pool": snap.Pool,
"uptime_seconds": snap.Uptime,
"hashrate_hs_1m": snap.HashrateHs,
"hashrate_hs_5m": snap.HashrateHs5m,
"hashrate_hs_1h": snap.HashrateHs1h,
"hashrate_hs_24h": snap.HashrateHs24h,
"chain": snap.Chain,
"network_hashrate_hs": snap.NetworkHashrateHs,
})
}
func (s *Server) users(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, s.Agg.Snapshot().Users)
}
func (s *Server) workers(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, s.Agg.Snapshot().Workers)
}
func (s *Server) clients(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, s.Agg.Snapshot().Clients)
}
func (s *Server) blocks(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, s.Agg.Blocks())
}
// debugBlocks exposes raw block state from both the in-memory snapshot
// (what the UI sees) and the DB (ground truth) for troubleshooting.
func (s *Server) debugBlocks(w http.ResponseWriter, r *http.Request) {
type row struct {
Height int64 `json:"height"`
Hash string `json:"hash"`
Chain string `json:"chain"`
OrphanedAt string `json:"orphaned_at"`
FoundAt string `json:"found_at"`
RewardBTC float64 `json:"reward_btc"`
Source string `json:"source"`
}
fmtBlock := func(b state.BlockRecord) row {
orphaned := ""
if b.OrphanedAt != nil {
orphaned = b.OrphanedAt.Format("2006-01-02T15:04:05Z")
}
return row{
Height: b.Height,
Hash: b.Hash,
Chain: b.Chain,
OrphanedAt: orphaned,
FoundAt: b.FoundAt.Format("2006-01-02T15:04:05Z"),
RewardBTC: b.RewardBT,
Source: b.Source,
}
}
// In-memory blocks (what /api/blocks and /api/snapshot serve)
memBlocks := s.Agg.Blocks()
mem := make([]row, 0, len(memBlocks))
for _, b := range memBlocks {
mem = append(mem, fmtBlock(b))
}
// DB blocks (ground truth)
dbBlocks := s.Agg.BlocksFromStore()
db := make([]row, 0, len(dbBlocks))
for _, b := range dbBlocks {
db = append(db, fmtBlock(b))
}
writeJSON(w, http.StatusOK, map[string]any{
"memory": mem,
"db": db,
})
}
func (s *Server) resetLatency(w http.ResponseWriter, _ *http.Request) {
s.Agg.ResetLatency()
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
}
func (s *Server) ackBest(w http.ResponseWriter, _ *http.Request) {
s.Agg.AckBestDiff()
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
}
func (s *Server) resetAckBest(w http.ResponseWriter, _ *http.Request) {
s.Agg.ResetAckedBestDiff()
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
}