A blog and other things. https://etc.fsh.ee/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.3 KiB

#lang racket
(require web-server/servlet
(require (prefix-in router: "routes.rkt")
(prefix-in wp: "wp.rkt"))
(provide make-server)
(define (make-cache)
(let ((h (make-hash)))
(lambda (key promise)
(with-handlers ((exn:fail?
(lambda (exn) (cache-attempt h key))))
(hash-set! h key (force promise))
(cache-attempt h key)))))
(define (cache-attempt h key)
(if (hash-has-key? h key)
(hash-ref h key)
'())) ;; All downstream APIs return list and empty lists are valid returns.
(define (forever f)
(forever f))
(define (cache-refresher wp post-cat page-cat)
(displayln 'cache-init)
(let ((cache (make-cache)))
(wp:prefetch cache wp (list post-cat page-cat))
(displayln 'cache-done)
(define (make-server wp port home-content post-cat page-cat)
(let ((cache (make-cache))
(cache-timeout (* 60 60)))
(lambda ()
(lambda ()
(set! cache (cache-refresher wp post-cat page-cat))
(sleep cache-timeout))))
(lambda ()
(lambda (req)
(router:route-root cache wp home-content post-cat page-cat req))
#:servlet-regexp #rx""
#:servlet-path "/"
#:port port
#:launch-browser? #f
#:listen-ip #f)))))