improve 404 behavior

pull/9/head
Matthew Butterick 11 years ago
parent cc3c3b49cc
commit c919cae281

@ -0,0 +1 @@
File not found. Sorry.

@ -120,8 +120,7 @@
(render-files-in-ptree ptree #:force force))] (render-files-in-ptree ptree #:force force))]
[(equal? FALLBACK_TEMPLATE_NAME (->string (file-name-from-path path))) [(equal? FALLBACK_TEMPLATE_NAME (->string (file-name-from-path path)))
(message "Render: using fallback template")] (message "Render: using fallback template")]
[(file-exists? path) (message "Serving static file" (->string (file-name-from-path path)))] [(file-exists? path) (message "Serving static file" (->string (file-name-from-path path)))])))
[else (error "Render couldn't find" (->string (file-name-from-path path)))])))
(for-each &render xs)) (for-each &render xs))
;; todo: write tests ;; todo: write tests

@ -1,7 +1,9 @@
#lang racket/base #lang racket/base
(require racket/list racket/contract racket/rerequire racket/file racket/format xml racket/match racket/set) (require racket/list racket/contract racket/rerequire racket/file racket/format xml racket/match racket/set)
(require web-server/http/xexpr)
(require (only-in net/url url-query url->path url->string)) (require (only-in net/url url-query url->path url->string))
(require (only-in web-server/http/request-structs request-uri request-client-ip)) (require (only-in web-server/http/request-structs request-uri request-client-ip request?))
(require (only-in web-server/http/response-structs response?))
(require "world.rkt" "render.rkt" "readability.rkt" "predicates.rkt" "debug.rkt") (require "world.rkt" "render.rkt" "readability.rkt" "predicates.rkt" "debug.rkt")
(module+ test (require rackunit)) (module+ test (require rackunit))
@ -66,6 +68,7 @@
(define (route-dashboard dir) (define (route-dashboard dir)
(define empty-cell (cons #f #f)) (define empty-cell (cons #f #f))
(define (make-link-cell href+text) (define (make-link-cell href+text)
@ -124,14 +127,24 @@
(and result (cdar result)))) ; second value of first result (and result (cdar result)))) ; second value of first result
(define/contract (req->path req)
(request? . -> . path?)
(reroot-path (url->path (request-uri req)) PROJECT_ROOT))
; default route ; default route
(define (route-default req) (define (route-default req)
(define request-url (request-uri req)) (define force (equal? (get-query-value (request-uri req) 'force) "true"))
(define path (reroot-path (url->path request-url) PROJECT_ROOT)) (with-handlers ([exn:fail? (λ(e) (message "Render is skipping" (url->string (request-uri req)) "because of error\n" (exn-message e)))])
(define force (equal? (get-query-value request-url 'force) "true")) (render (req->path req) #:force force)))
(with-handlers ([exn:fail? (λ(e) (message "Render is skipping" (url->string request-url) "because of error\n" (exn-message e)))])
(render path #:force force)))
; error route
(define/contract (route-404 req)
(request? . -> . response?)
(define error-text (format "Can't find ~a" (->string (req->path req))))
(message error-text)
;`(html ,(slurp (build-path SERVER_EXTRAS_DIR "404.html")))
(response/xexpr `(html ,error-text)))
(module+ main (module+ main
(route-dashboard "foobar")) (route-dashboard "foobar"))

@ -1,6 +1,6 @@
#lang web-server #lang web-server
(require "startup.rkt") (require "startup.rkt")
(require web-server/servlet-env web-server/dispatch web-server/dispatchers/dispatch xml) (require web-server/servlet-env web-server/dispatch web-server/dispatchers/dispatch web-server/configuration/responders xml)
(require "server-routes.rkt" "debug.rkt" "world.rkt") (require "server-routes.rkt" "debug.rkt" "world.rkt")
(define port-number 8088) (define port-number 8088)
@ -42,12 +42,12 @@
(message (format "Project dashboard is http://localhost:~a/pollen.html" port-number)) (message (format "Project dashboard is http://localhost:~a/pollen.html" port-number))
(message "Ready to rock") (message "Ready to rock")
(serve/servlet start (serve/servlet start
#:port port-number #:port port-number
#:listen-ip #f #:listen-ip #f
#:servlet-regexp #rx"" ; respond to top level #:servlet-regexp #rx"" ; respond to top level
#:command-line? #t #:command-line? #t
#:extra-files-paths (list #:file-not-found-responder route-404
;; todo: files in this directory are wrongly reported in log as missing #:extra-files-paths (list SERVER_EXTRAS_DIR PROJECT_ROOT))
(build-path MODULE_ROOT "pollen-server-extras")
(build-path PROJECT_ROOT)))

@ -34,14 +34,13 @@
(require racket/string racket/port racket/system) (require racket/string racket/port racket/system)
;; todo: is path to racket already available as an environment variable? ;; todo: is path to racket already available as an environment variable?
;; e.g., (find-system-path 'xxx)? ;; e.g., (find-system-path 'xxx)? Because this next line is sort of slow
;;(define RACKET_PATH (string-trim (with-output-to-string (λ() (system "which racket"))))) ;;(define RACKET_PATH (string-trim (with-output-to-string (λ() (system "which racket")))))
(define RACKET_PATH "/usr/bin/racket") (define RACKET_PATH "/usr/bin/racket") ;; todo: this won't always work
(define POLLEN_ROOT 'main) (define POLLEN_ROOT 'main)
(define POLLEN_COMMAND_FILE "polcom") (define POLLEN_COMMAND_FILE "polcom")
(require "readability.rkt") (require "readability.rkt")
(define RESERVED_PATHS (define RESERVED_PATHS
(map ->path (list POLLEN_COMMAND_FILE EXTRAS_DIR "poldash.css"))) (map ->path (list POLLEN_COMMAND_FILE EXTRAS_DIR "poldash.css")))
@ -50,6 +49,6 @@
(define PROJECT_ROOT (current-directory)) (define PROJECT_ROOT (current-directory))
;; use current-contract-region to calculate containing directory of module ;; use current-contract-region to calculate containing directory of module
(define MODULE_ROOT (apply build-path (drop-right (explode-path (current-contract-region)) 1))) (define MODULE_ROOT (apply build-path (drop-right (explode-path (current-contract-region)) 1)))
(define SERVER_EXTRAS_DIR (build-path MODULE_ROOT "pollen-server-extras"))
(define DASHBOARD_NAME "poldash.html") (define DASHBOARD_NAME "poldash.html")
Loading…
Cancel
Save