Neaten up the blog site example

And fix some issues.
This commit is contained in:
Christopher Baines 2026-04-14 15:19:11 +03:00
parent dd15e9306a
commit 0d1a31f4c7
4 changed files with 57 additions and 49 deletions

View file

@ -15,40 +15,31 @@
(exit 1))
;; Create a shared database thread pool.
(define pool (make-db "/tmp/blog-site.db"))
(let* ((db-pool (make-db "/tmp/blog-site.db"))
;; Initialise the schema.
(db-init! pool)
;; Session manager — in production, use a proper secret.
(session-manager
(make-session-config "change-me-in-production"
#:cookie-name "blog-session"))
;; Session manager — in production, use a proper secret.
(define session-manager
(make-session-config "change-me-in-production"
#:cookie-name "blog-session"))
(all-routes
(wrap-routes
(list (make-blog-component db-pool session-manager)
(route-group '("static")
(route 'GET '(. path)
(make-static-handler
"./static"
#:cache-control '((max-age . 3600)))))
(route '* '* (lambda (request body-port)
(not-found-response))))
(make-exceptions-handler-wrapper #:dev? #t)
logging-handler-wrapper
security-headers-handler-wrapper
(make-session-handler-wrapper session-manager)
csrf-handler-wrapper))
;; Build the blog component — handles both HTML and JSON via content negotiation.
(define blog-routes (make-blog-component pool session-manager))
(port 8082))
;; Static file serving.
(define static-routes
(route-group '("static")
(route 'GET '(. path)
(make-static-handler "./static"
#:cache-control '((max-age . 3600))))))
;; Apply handler wrappers and add a catch-all 404 route.
(define all-routes
(wrap-routes (list blog-routes
static-routes
(route '* '* (lambda (request body-port)
(not-found-response))))
(make-exceptions-handler-wrapper #:dev? #t)
logging-handler-wrapper
security-headers-handler-wrapper
(make-session-handler-wrapper session-manager)
csrf-handler-wrapper))
;; Start the server.
(let ((port 8082))
(format #t "Listening on http://localhost:~a~%" port)
(force-output)
(run-safsaf all-routes #:port port))