55 lines
1.8 KiB
Scheme
55 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))
|