;; lib/host/handler.sx — Host handler layer: the bridge from a Dream request to a ;; subsystem call and back to a Dream response. A host handler IS a Dream handler ;; (request -> response); these helpers build the JSON envelope every host ;; endpoint shares: {"ok":true,"data":...} on success, {"ok":false,"error":...} ;; on failure. Plus a status-carrying JSON constructor that Dream's own dream-json ;; (200-only) lacks, and a couple of request-reading conveniences. ;; Depends on lib/dream/types.sx + lib/dream/json.sx. ;; ── responses ────────────────────────────────────────────────────── ;; JSON response at an arbitrary status (dream-json is 200-only). (define host/json-status (fn (status value) (dream-response status {:content-type "application/json"} (dream-json-encode value)))) ;; Success envelope: 200 {"ok":true,"data":}. (define host/ok (fn (value) (host/json-status 200 {:ok true :data value}))) ;; Success envelope at a chosen status (e.g. 201 for a created resource). (define host/ok-status (fn (status value) (host/json-status status {:ok true :data value}))) ;; Error envelope: {"ok":false,"error":} at the given status. (define host/error (fn (status message) (host/json-status status {:ok false :error message}))) ;; ── request reading ──────────────────────────────────────────────── ;; Integer query param with a fallback (query params arrive as strings). ;; Absent param -> fallback; present -> parsed number. (define host/query-int (fn (req name fallback) (let ((raw (dream-query-param req name))) (if raw (string->number raw) fallback))))