Improve render procedures

This commit is contained in:
Christopher Baines 2025-06-19 09:40:01 +01:00
parent 41c0d76a3d
commit 9f431462db

View file

@ -26,6 +26,7 @@
#:use-module (ice-9 ftw)
#:use-module (ice-9 iconv)
#:use-module (ice-9 binary-ports)
#:use-module (ice-9 textual-ports)
#:use-module (web request)
#:use-module (web response)
#:use-module (web uri)
@ -147,15 +148,15 @@
'((content-type . (text/html
(charset . "utf-8")))
(vary . (accept)))))
(if stream?
(lambda (port)
(set-port-encoding! port "utf-8")
(setvbuf port 'block (expt 2 20))
(sxml->html sxml port))
(call-with-encoded-output-string
"utf-8"
(and
sxml
(if stream?
(lambda (port)
(sxml->html sxml port))))))
(sxml->html sxml port))
(call-with-encoded-output-string
"utf-8"
(lambda (port)
(sxml->html sxml port)))))))
(define* (render-json json #:key (extra-headers '())
(code 200)
@ -166,27 +167,35 @@
'((content-type . (application/json
(charset . "utf-8")))
(vary . (accept)))))
(if stream?
(lambda (port)
(setvbuf port 'block (expt 2 16))
(scm->json json port #:unicode #t))
(call-with-encoded-output-string
"utf-8"
(and
json
(if stream?
(lambda (port)
(scm->json json port #:unicode #t))))))
(scm->json json port #:unicode #t))
(call-with-encoded-output-string
"utf-8"
(lambda (port)
(scm->json json port)))))))
(define* (render-text text #:key (extra-headers '())
(code 200))
(code 200)
stream?)
(list (build-response
#:code code
#:headers (append extra-headers
'((content-type . (text/plain
(charset . "utf-8")))
(vary . (accept)))))
(call-with-encoded-output-string
"utf-8"
(lambda (port)
(display text port)))))
(and
text
(if stream?
(lambda (port)
(put-string port text))
(call-with-encoded-output-string
"utf-8"
(lambda (port)
(display text port)))))))
(define (not-found uri)
(list (build-response #:code 404)