web

package
v0.16.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MIT Imports: 33 Imported by: 0

Documentation

Overview

Package web implements the Hearth 2.0 web UI backend for the forge daemon.

The package serves a small chi-based HTTP server that exposes read-only JSON endpoints mirroring the existing IPC commands (status, queue, workers) plus a bcrypt-validated session login. It is intended to run in-process inside the daemon (no extra socket hop) and is gated by the FORGE_WEB_ENABLED environment variable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseUsers

func ParseUsers(raw string) (map[string]string, error)

ParseUsers parses a FORGE_USERS-formatted string. Exposed separately from ParseUsersFromEnv to make testing simpler.

func ParseUsersFromEnv

func ParseUsersFromEnv() (map[string]string, error)

ParseUsersFromEnv parses the FORGE_USERS environment variable. The format is a comma-separated list of user:bcrypt-hash entries:

FORGE_USERS=alice:$2a$10$abcd...,bob:$2a$10$wxyz...

Whitespace around entries is trimmed. Bcrypt hashes contain ':' inside their cost/salt section, so ParseUsersFromEnv splits on the FIRST ':' per entry and treats everything after it as the hash.

Returns an empty map when the env var is unset or empty. An entry with a blank username or hash returns an error.

func SessionFromContext

func SessionFromContext(ctx context.Context) *state.WebSession

SessionFromContext returns the authenticated session attached to ctx by the auth middleware, or nil when no session is present.

func VerifyCredentials

func VerifyCredentials(users map[string]string, username, password string) error

VerifyCredentials returns nil when the password matches the bcrypt hash stored for the user. Returns errInvalidCredentials for unknown users or password mismatches; this avoids leaking which half of the pair was wrong.

Types

type AnvilDispatchTagLister

type AnvilDispatchTagLister func() map[string]string

AnvilDispatchTagLister returns the per-anvil auto-dispatch tag from forge.yaml (`auto_dispatch_tag`) as a map of name -> tag. Anvils with no tag configured are omitted from the map. Used by the Hearth web UI's one-click "Apply tag" action so the dispatch label is resolved server-side and matches the daemon's runtime config (including hot-reloads). Implementations must be safe to call concurrently.

type AnvilLister

type AnvilLister func() map[string]string

AnvilLister returns the registered anvils as a map of name -> on-disk path. Callers use this for the Beads-Forge bead-emission flow: the web layer passes the names to claude (so it knows which anvils to target) and resolves names to paths when shelling out to bd. Implementations must be safe to call concurrently — the daemon's hot-reload may swap the config underneath.

type BdRunnerFn

type BdRunnerFn = forgechat.BdRunner

BdRunnerFn is a process-spawning function compatible with the materializer in package forgechat. The daemon supplies forgechat.DefaultBdRunner; tests inject a fake to avoid spawning real bd subprocesses.

type CommandHandler

type CommandHandler func(cmd ipc.Command) ipc.Response

CommandHandler is the in-process dispatcher used by the web layer to call daemon command handlers without going through the IPC socket. The daemon passes its own handleIPC method here.

type Config

type Config struct {
	// Addr is the TCP listen address (e.g. ":8080"). Required.
	Addr string

	// Users maps username -> bcrypt hash. May be empty, in which case the
	// /login endpoint always rejects.
	Users map[string]string

	// SessionTTL is the sliding session lifetime. Defaults to 30 days when
	// zero.
	SessionTTL time.Duration

	// CookieName is the session cookie name. Defaults to "forge_session".
	CookieName string

	// CookieSecure forces the Secure cookie attribute. Defaults to false so
	// local development over plain HTTP works; production deployments behind
	// HTTPS should set this to true.
	CookieSecure bool

	// PurgeInterval controls how often expired session rows are purged from
	// the DB. Defaults to 1 hour. Set to a negative value to disable.
	PurgeInterval time.Duration
}

Config holds the runtime configuration for the web server. Most fields are derived from environment variables in NewConfigFromEnv.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the chi-based HTTP server. Construct with New and run with Start.

func New

func New(cfg Config, db *state.DB, handler CommandHandler, logger *slog.Logger) (*Server, error)

New constructs a Server. The cfg is validated; an error is returned when required fields are missing.

func (*Server) SetAnvilDispatchTagLister

func (s *Server) SetAnvilDispatchTagLister(a AnvilDispatchTagLister)

SetAnvilDispatchTagLister installs the per-anvil dispatch tag callback used by the one-click Apply-tag action on the Hearth queue. nil clears the callback, after which apply-dispatch-tag requests are rejected.

func (*Server) SetAnvilLister

func (s *Server) SetAnvilLister(a AnvilLister)

SetAnvilLister installs the registry callback used by the Beads-Forge bead-emission flow. The daemon snapshots its current config on each call so hot-reloads are picked up automatically.

func (*Server) SetBdRunner

func (s *Server) SetBdRunner(r BdRunnerFn)

SetBdRunner installs the bd subprocess shim used for bead materialisation. nil restores the default (forgechat.DefaultBdRunner).

func (*Server) SetChatRunner

func (s *Server) SetChatRunner(r forgechat.Runner)

SetChatRunner installs the AI runner used by the Beads-Forge page. The daemon constructs the runner from its provider chain after web.New returns, so this is wired in via a setter rather than the Config struct. nil clears the runner.

func (*Server) Start

func (s *Server) Start(ctx context.Context) error

Start begins serving HTTP requests. It blocks until ctx is cancelled or the server returns a non-Closed error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL