Files
KamadoPool/api/cmd/kamado-api/main.go
T
satoshi 0a40b8f84f Phase 2b: log tailer, block history, stdlib WebSocket push
Adds real-time block detection via ckpool log tailing and a push
channel for the upcoming Svelte UI, all stdlib-only:

- logmon.Tailer follows ckpool.log with rotation/truncation survival
  (inode + size tracking) and parses "Solved and confirmed block N"
  into BlockEvent values.
- state.Aggregator grows a 256-entry block ring, an OnRefresh hook,
  and IngestBlockEvents which best-effort enriches events with the
  block hash via bitcoind getblockhash.
- httpapi.Hub implements RFC 6455 from scratch (SHA1 handshake,
  unmasked text frames out, masked frames in, ping keepalive,
  per-client write mutex, slow-client drop) so we don't pull in a
  ws dependency before we can go mod tidy.
- New routes: GET /api/blocks and GET /api/ws. Snapshot pushes fire
  on every poll tick and immediately on block-solve.

ZMQ hashblock subscription and SQLite persistence are deferred to
Phase 2b.5 once the s9pk packaging repo exists and we have a real
build environment for adding Go deps.
2026-04-13 02:53:27 +03:00

83 lines
2.3 KiB
Go

// kamado-api is the middleware that sits between ckpool-solo and the
// Kamado dashboard. It polls ckpool's Unix socket API, calls bitcoind
// over JSON-RPC, and serves the merged state over REST (WebSocket push,
// SQLite persistence, ZMQ and log-tailer-based block detection land in
// a follow-up commit).
package main
import (
"context"
"errors"
"log/slog"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/kamadopool/kamado-api/internal/bitcoind"
"github.com/kamadopool/kamado-api/internal/ckpool"
"github.com/kamadopool/kamado-api/internal/config"
"github.com/kamadopool/kamado-api/internal/httpapi"
"github.com/kamadopool/kamado-api/internal/logmon"
"github.com/kamadopool/kamado-api/internal/state"
)
func main() {
log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
slog.SetDefault(log)
cfg, err := config.FromEnv()
if err != nil {
log.Error("config", "err", err)
os.Exit(1)
}
log.Info("kamado-api starting",
"listen", cfg.ListenAddr,
"sockdir", cfg.CKPoolSockDir,
"bitcoind", cfg.BitcoinRPCURL,
"poll_interval", cfg.PollInterval,
)
ck := ckpool.New(cfg.CKPoolSockDir)
rpc := bitcoind.NewRPC(cfg.BitcoinRPCURL, cfg.BitcoinRPCUser, cfg.BitcoinRPCPassword, cfg.BitcoinRPCTimeout)
agg := state.New(ck, rpc, cfg.PollInterval, log)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
api := httpapi.New(agg, log)
// Wire snapshot refreshes into the WebSocket hub so subscribers get
// real-time updates without polling.
agg.OnRefresh = api.Hub.Broadcast
go agg.Run(ctx)
// Tail the ckpool log for block-solve events.
tailer := logmon.New(cfg.CKPoolLogFile, log)
go tailer.Run(ctx)
go agg.IngestBlockEvents(ctx, tailer.Events)
srv := &http.Server{
Addr: cfg.ListenAddr,
Handler: api.Handler(),
ReadHeaderTimeout: 5 * time.Second,
}
// Shutdown on ctx cancel
go func() {
<-ctx.Done()
log.Info("shutting down")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
_ = srv.Shutdown(shutdownCtx)
}()
log.Info("http listening", "addr", cfg.ListenAddr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Error("http server", "err", err)
os.Exit(1)
}
}