diff --git a/breezewiki.rkt b/breezewiki.rkt index ee4f9b9..22f368c 100644 --- a/breezewiki.rkt +++ b/breezewiki.rkt @@ -1,11 +1,17 @@ #lang racket/base -(require web-server/servlet-dispatch +(require racket/path + racket/runtime-path + net/url + web-server/servlet-dispatch + web-server/dispatchers/filesystem-map (prefix-in pathprocedure: web-server/dispatchers/dispatch-pathprocedure) (prefix-in sequencer: web-server/dispatchers/dispatch-sequencer) (prefix-in lift: web-server/dispatchers/dispatch-lift) (prefix-in filter: web-server/dispatchers/dispatch-filter) + (prefix-in files: web-server/dispatchers/dispatch-files) "src/config.rkt" - "src/reloadable.rkt") + "src/reloadable.rkt" + "src/server-utils.rkt") (define-syntax-rule (require-reloadable filename varname) (define varname @@ -17,13 +23,14 @@ (require-reloadable "src/page-not-found.rkt" page-not-found) (require-reloadable "src/page-proxy.rkt" page-proxy) (require-reloadable "src/page-search.rkt" page-search) -(require-reloadable "src/page-static.rkt" static-dispatcher) (require-reloadable "src/page-wiki.rkt" page-wiki) (when (not (config-true? 'debug)) (set-reload-poll-interval! #f)) (reload!) +(define-runtime-path path-static "static") + (serve/launch/wait #:listen-ip (if (config-true? 'debug) "127.0.0.1" #f) #:port (string->number (config-get 'port)) @@ -34,5 +41,13 @@ (filter:make #rx"^/[a-z-]+/wiki/Category:.+$" (lift:make page-category)) (filter:make #rx"^/[a-z-]+/wiki/.+$" (lift:make page-wiki)) (filter:make #rx"^/[a-z-]+/search$" (lift:make page-search)) - static-dispatcher + (filter:make #rx"^/static/" (files:make + #:url->path + (lambda (u) + ((make-url->path path-static) + (struct-copy url u [path (cdr (url-path u))]))) + #:path->mime-type + (lambda (u) + (ext->mime-type (path-get-extension u))) + #:cache-no-cache (config-true? 'debug) #;"browser applies heuristics if unset")) (lift:make page-not-found)))) diff --git a/dist.rkt b/dist.rkt index 9e190ef..19fc736 100644 --- a/dist.rkt +++ b/dist.rkt @@ -1,20 +1,26 @@ #lang racket/base -(require web-server/servlet-dispatch +(require racket/path + racket/runtime-path + net/url + web-server/servlet-dispatch + web-server/dispatchers/filesystem-map (prefix-in pathprocedure: web-server/dispatchers/dispatch-pathprocedure) (prefix-in sequencer: web-server/dispatchers/dispatch-sequencer) (prefix-in lift: web-server/dispatchers/dispatch-lift) (prefix-in filter: web-server/dispatchers/dispatch-filter) + (prefix-in files: web-server/dispatchers/dispatch-files) "src/config.rkt" - "src/reloadable.rkt") + "src/server-utils.rkt") (require (only-in "src/page-category.rkt" page-category)) (require (only-in "src/page-home.rkt" page-home)) (require (only-in "src/page-not-found.rkt" page-not-found)) (require (only-in "src/page-proxy.rkt" page-proxy)) (require (only-in "src/page-search.rkt" page-search)) -(require (only-in "src/page-static.rkt" static-dispatcher)) (require (only-in "src/page-wiki.rkt" page-wiki)) +(define-runtime-path path-static "static") + (serve/launch/wait #:listen-ip (if (config-true? 'debug) "127.0.0.1" #f) #:port (string->number (config-get 'port)) @@ -25,5 +31,13 @@ (filter:make #rx"^/[a-z-]+/wiki/Category:.+$" (lift:make page-category)) (filter:make #rx"^/[a-z-]+/wiki/.+$" (lift:make page-wiki)) (filter:make #rx"^/[a-z-]+/search$" (lift:make page-search)) - static-dispatcher + (filter:make #rx"^/static/" (files:make + #:url->path + (lambda (u) + ((make-url->path path-static) + (struct-copy url u [path (cdr (url-path u))]))) + #:path->mime-type + (lambda (u) + (ext->mime-type (path-get-extension u))) + #:cache-no-cache (config-true? 'debug) #;"browser applies heuristics if unset")) (lift:make page-not-found)))) diff --git a/src/config.rkt b/src/config.rkt index 8e785ec..e3f98b0 100644 --- a/src/config.rkt +++ b/src/config.rkt @@ -49,7 +49,6 @@ (printf "note: ~a items loaded from config file~n" (length l))))))) (when (config-true? 'debug) - ; all values here are optimised for maximum prettiness (parameterize ([pretty-print-columns 80]) (display "config: ") (pretty-write (hash->list config)))) diff --git a/src/page-static.rkt b/src/page-static.rkt deleted file mode 100644 index 30a198b..0000000 --- a/src/page-static.rkt +++ /dev/null @@ -1,69 +0,0 @@ -#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) - "config.rkt") - -(provide - static-dispatcher) - -(module+ test - (require rackunit)) - -(define-runtime-path path-static "../static") - -(define hash-ext-mime-type - (hash #".css" #"text/css" - #".png" #"image/png" - #".svg" #"image/svg+xml" - #".txt" #"text/plain")) - -(define (ext->mime-type ext) - (hash-ref hash-ext-mime-type ext)) -(module+ test - (check-equal? (ext->mime-type #".png") #"image/png")) - -(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" '())))) - -(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 - (cdr p)] - ; url is literally ^/robots.txt - [(equal? p (make-path '("robots.txt"))) - ; rewrite to ^/... -- it already is! - p] - ; not going to use the static file dispatcher - [#t (next-dispatcher)])) -(module+ test - (check-equal? (path-rewriter (make-path '("static" "main.css"))) - (make-path '("main.css"))) - (check-equal? (path-rewriter (make-path '("static" "robots.txt"))) - (make-path '("robots.txt"))) - (check-equal? (path-rewriter (make-path '("robots.txt"))) - (make-path '("robots.txt")))) - -(define (static-dispatcher conn old-req) - (define old-uri (request-uri old-req)) - (define old-path (url-path old-uri)) - (define new-path (path-rewriter old-path)) - (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) - (println u) - ((make-url->path path-static) u)) - #:path->mime-type (lambda (u) (ext->mime-type (path-get-extension u))) - #:cache-no-cache (config-true? 'debug) #;"browser applies heuristics if unset") - conn new-req)) diff --git a/src/server-utils.rkt b/src/server-utils.rkt new file mode 100644 index 0000000..f41c2e6 --- /dev/null +++ b/src/server-utils.rkt @@ -0,0 +1,16 @@ +#lang racket/base + +(provide + ext->mime-type) + +(module+ test + (require rackunit)) + +(define hash-ext-mime-type + (hash #".css" #"text/css" + #".svg" #"image/svg+xml" + #".png" #"image/png")) +(define (ext->mime-type ext) + (hash-ref hash-ext-mime-type ext)) +(module+ test + (check-equal? (ext->mime-type #".png") #"image/png")) diff --git a/static/robots.txt b/static/robots.txt deleted file mode 100644 index 6f706c5..0000000 --- a/static/robots.txt +++ /dev/null @@ -1,3 +0,0 @@ -User-Agent: * -Disallow: /*/wiki/* -Disallow: /proxy