safsaf/examples/blog-site/blog-site.scm
Christopher Baines 5b0e6397dc
All checks were successful
/ test (push) Successful in 9s
Initial commit
Safsaf is a Guile web framework, written using Claude Code running
Claude Opus 4.6, based off of the Guix Data Service, Nar Herder and
Guix Build Coordinator codebases.
2026-04-13 14:24:19 +03:00

54 lines
1.8 KiB
Scheme

(use-modules (model)
(views)
(safsaf)
(safsaf handler-wrappers csrf)
(safsaf handler-wrappers exceptions)
(safsaf handler-wrappers logging)
(safsaf handler-wrappers security-headers)
(safsaf handler-wrappers sessions)
(safsaf response-helpers)
(safsaf router))
(unless (file-exists? "static/style.css")
(format (current-error-port)
"error: run this from the examples/blog-site/ directory~%")
(exit 1))
;; Create a shared database thread pool.
(define pool (make-db "/tmp/blog-site.db"))
;; Initialise the schema.
(db-init! pool)
;; Session manager — in production, use a proper secret.
(define session-manager
(make-session-config "change-me-in-production"
#:cookie-name "blog-session"))
;; Build the blog component — handles both HTML and JSON via content negotiation.
(define blog-routes (make-blog-component pool session-manager))
;; 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))