breezewiki/src/page-static.rkt

79 lines
3.2 KiB
Racket
Raw Normal View History

2022-08-30 12:45:37 +00:00
#lang racket/base
(require racket/path
racket/runtime-path
net/url
web-server/http/request-structs
web-server/servlet-dispatch
web-server/dispatchers/filesystem-map
(only-in web-server/dispatchers/dispatch next-dispatcher)
(prefix-in files: web-server/dispatchers/dispatch-files)
"../lib/mime-types.rkt"
"../lib/syntax.rkt"
2022-08-30 12:45:37 +00:00
"config.rkt")
(provide
static-dispatcher)
(module+ test
(require rackunit))
(define-runtime-path path-static "../static")
(define path-archive (anytime-path ".." "storage/archive"))
2022-08-30 12:45:37 +00:00
(define hash-ext-mime-type
(hash #".css" #"text/css"
2022-10-22 11:26:06 +00:00
#".js" #"text/javascript"
2022-08-30 12:45:37 +00:00
#".png" #"image/png"
#".svg" #"image/svg+xml"
2022-11-10 10:55:31 +00:00
#".woff2" #"font/woff2"
2022-08-30 12:45:37 +00:00
#".txt" #"text/plain"))
(define (make-path segments)
(map (λ (seg) (path/param seg '())) segments))
(module+ test
(check-equal? (make-path '("static" "main.css"))
(list (path/param "static" '()) (path/param "main.css" '()))))
;; given a request path, return a rewritten request path and the source directory on the filesystem to serve based on
2022-08-30 12:45:37 +00:00
(define (path-rewriter p)
(cond
; url is ^/static/... ?
[(equal? (path/param-path (car p)) "static")
; rewrite to ^/... which will be treated as relative to static/ on the filesystem
(values (cdr p) path-static)]
; url is ^/archive/... ?
[(equal? (path/param-path (car p)) "archive")
; rewrite req to ^/<wikiname> and dir to /storage/archive
(values (cdr p) path-archive)]
2022-08-30 12:45:37 +00:00
; url is literally ^/robots.txt
[(equal? p (make-path '("robots.txt")))
; rewrite to ^/... -- it already is!
(values p path-static)]
2022-08-30 12:45:37 +00:00
; not going to use the static file dispatcher
[#t (next-dispatcher)]))
(module+ test
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("static" "main.css")))) cons)
(cons (make-path '("main.css")) path-static))
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("static" "robots.txt")))) cons)
(cons (make-path '("robots.txt")) path-static))
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("robots.txt")))) cons)
(cons (make-path '("robots.txt")) path-static))
(check-equal? (call-with-values (λ () (path-rewriter (make-path '("archive" "minecraft" "styles" "main.css")))) cons)
(cons (make-path '("minecraft" "styles" "main.css")) path-archive)))
2022-08-30 12:45:37 +00:00
(define (static-dispatcher conn old-req)
(define old-uri (request-uri old-req))
(define old-path (url-path old-uri))
(define-values (new-path source-dir) (path-rewriter old-path))
2022-08-30 12:45:37 +00:00
(define new-uri (struct-copy url old-uri [path new-path]))
(define new-req (struct-copy request old-req [uri new-uri]))
((files:make
#:url->path (lambda (u) ((make-url->path source-dir) u))
#:path->headers (lambda (p) (list (header #"Access-Control-Allow-Origin" #"*")
(header #"Referrer-Policy" #"same-origin")))
2022-08-30 12:45:37 +00:00
#:path->mime-type (lambda (u) (ext->mime-type (path-get-extension u)))
2022-10-30 10:51:15 +00:00
#:cache-no-cache (config-true? 'debug)
#:cache-immutable (not (config-true? 'debug))
#:cache-max-age (if (config-true? 'debug) #f 604800))
2022-08-30 12:45:37 +00:00
conn new-req))