Files
rose-ash/web/request-handler.sx
giles 2d7dd7d582 Step 5 piece 6: migrate 23 .sx files to define-library/import
Wraps all core .sx files in R7RS define-library with explicit export
lists, plus (import ...) at end for backward-compatible global re-export.

Libraries registered:
  (sx bytecode)      — 83 opcode constants
  (sx render)        — 15 tag registries + render helpers
  (sx signals)       — 23 reactive signal primitives
  (sx r7rs)          — 21 R7RS aliases
  (sx compiler)      — 42 compiler functions
  (sx vm)            — 32 VM functions
  (sx freeze)        — 9 freeze/thaw functions
  (sx content)       — 6 content store functions
  (sx callcc)        — 1 call/cc wrapper
  (sx highlight)     — 13 syntax highlighting functions
  (sx stdlib)        — 47 stdlib functions
  (sx swap)          — 13 swap algebra functions
  (sx render-trace)  — 8 render trace functions
  (sx harness)       — 21 test harness functions
  (sx canonical)     — 12 canonical serialization functions
  (web adapter-html) — 13 HTML renderer functions
  (web adapter-sx)   — 13 SX wire format functions
  (web engine)       — 33 hypermedia engine functions
  (web request-handler) — 4 request handling functions
  (web page-helpers) — 12 page helper functions
  (web router)       — 36 routing functions
  (web deps)         — 19 dependency analysis functions
  (web orchestration) — 59 page orchestration functions

Key changes:
- define-library now inherits parent env (env-extend env instead of
  env-extend make-env) so library bodies can access platform primitives
- sx_server.ml: added resolve_library_path + load_library_file for
  import resolution (maps library specs to file paths)
- cek_run_with_io: handles "import" locally instead of sending to
  Python bridge

2608/2608 tests passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 21:48:54 +00:00

85 lines
2.2 KiB
Plaintext

(define-library (web request-handler)
(export
sx-url-to-expr
sx-auto-quote
sx-eval-page
sx-handle-request)
(begin
(define
sx-url-to-expr
(fn
(path)
(let
((prefix (cek-try (fn () (or (dict-get __app-config :path-prefix) "/sx/")) (fn (e) "/sx/")))
(prefix-len (len prefix))
(prefix-bare (slice prefix 0 (- prefix-len 1))))
(cond
(or (= path "/") (= path prefix) (= path prefix-bare))
"home"
(starts-with? path prefix)
(join " " (split (slice path prefix-len (len path)) "."))
(starts-with? path "/")
(join " " (split (slice path 1 (len path)) "."))
:else path))))
(define
sx-auto-quote
(fn
(expr env)
(cond
(and (symbol? expr) (not (env-has? env (symbol-name expr))))
(symbol-name expr)
(list? expr)
(map (fn (e) (sx-auto-quote e env)) expr)
:else expr)))
(define
sx-eval-page
(fn
(path-expr env)
(cek-try
(fn
()
(let
((exprs (sx-parse path-expr)))
(when
(not (empty? exprs))
(let
((expr (if (= (len exprs) 1) (first exprs) exprs))
(quoted (sx-auto-quote expr env))
(callable (if (symbol? quoted) (list quoted) quoted)))
(eval-expr callable env)))))
(fn (err) nil))))
(define
sx-handle-request
(fn
(path headers env shell-vars)
(let
((is-ajax (or (has-key? headers "sx-request") (has-key? headers "hx-request")))
(path-expr (sx-url-to-expr path))
(page-ast (sx-eval-page path-expr env)))
(if
(nil? page-ast)
nil
(let
((prefix (cek-try (fn () (or (dict-get __app-config :path-prefix) "/sx/")) (fn (e) "/sx/")))
(prefix-len (len prefix))
(nav-path
(if
(and
(>= (len path) prefix-len)
(= (slice path 0 prefix-len) prefix))
path
(str (slice prefix 0 (- prefix-len 1)) path))))
{:page-ast page-ast :nav-path nav-path :is-ajax is-ajax})))))
)) ;; end define-library
;; Re-export to global namespace for backward compatibility
(import (web request-handler))