use serve instead of serve/servlet

dev-nonsettable
Matthew Butterick 5 years ago
parent 53ffc88be0
commit e5ddd18cba

@ -1,7 +1,13 @@
#lang web-server/base #lang racket/base
(require racket/runtime-path (require racket/runtime-path
web-server/servlet-env
web-server/dispatch web-server/dispatch
web-server/web-server
web-server/servlet-dispatch
web-server/private/mime-types
(prefix-in files: web-server/dispatchers/dispatch-files)
(prefix-in sequencer: web-server/dispatchers/dispatch-sequencer)
web-server/dispatchers/filesystem-map
net/sendurl
"project-server-routes.rkt" "project-server-routes.rkt"
"log.rkt" "log.rkt"
"../setup.rkt" "../setup.rkt"
@ -12,31 +18,53 @@
(define-runtime-path mime-types "server-extras/mime.types") (define-runtime-path mime-types "server-extras/mime.types")
(define (start-server servlet-path [open-browser-window? #f]) (define (make-static-dispatcher-sequence . pths)
(define-values (pollen-servlet _) (apply sequencer:make
(dispatch-rules (for/list ([pth (in-list pths)])
[((string-arg) ... (? (λ (x) (string=? "" x)))) route-index] ; last element of a "/"-terminated url is "" (files:make
[((string-arg) ... (? pagetree-source?)) route-dashboard] #:path->mime-type (make-path->mime-type mime-types)
[((string-arg) ... "in" (string-arg) ...) route-in] #:url->path (make-url->path (path->string pth))))))
[((string-arg) ... "out" (string-arg) ...) route-out]
[else route-default])) (define-values (pollen-servlet _)
(dispatch-rules
;; last element of a "/"-terminated url is ""
[((string-arg) ... "") route-index]
[((string-arg) ... (? pagetree-source?)) route-dashboard]
[((string-arg) ... "in" (string-arg) ...) route-in]
[((string-arg) ... "out" (string-arg) ...) route-out]
[else route-default]))
(define (start-server servlet-path [open-browser-window? #false] #:return [return? #false])
(define server-name (format "http://localhost:~a" (current-server-port)))
(message (format "welcome to Pollen ~a (Racket ~a)" pollen:version (version))) (message (format "welcome to Pollen ~a (Racket ~a)" pollen:version (version)))
(message (format "project root is ~a" (current-project-root))) (message (format "project root is ~a" (current-project-root)))
(define server-name (format "http://localhost:~a" (current-server-port)))
(message (format "project server is ~a (Ctrl+C to exit)" server-name)) (message (format "project server is ~a (Ctrl+C to exit)" server-name))
(message (format "project dashboard is ~a/~a" server-name (setup:main-pagetree))) (message (format "project dashboard is ~a/~a" server-name (setup:main-pagetree)))
(message (let ([clsi (current-server-listen-ip)])
(if clsi
(format "project server permitting access only to ~a"
(case clsi
[("127.0.0.1") "localhost"]
[else clsi]))
"project server permitting access to all clients")))
(message "ready to rock") (message "ready to rock")
(parameterize ([error-print-width 1000]) (define stop-func
(serve/servlet pollen-servlet (parameterize ([error-print-width 1000])
#:launch-browser? open-browser-window? (serve
#:servlet-path servlet-path #:dispatch (sequencer:make
#:port (current-server-port) (dispatch/servlet pollen-servlet)
#:listen-ip (current-server-listen-ip) (make-static-dispatcher-sequence
#:servlet-regexp #rx"" ; respond to top level (current-project-root)
#:command-line? #true (current-server-extras-path))
#:file-not-found-responder route-404 (dispatch/servlet route-404))
#:mime-types-path mime-types #:listen-ip (current-server-listen-ip)
#:extra-files-paths (list (current-project-root) (current-server-extras-path))))) #:port (current-server-port))))
(when open-browser-window?
(send-url (string-append server-name servlet-path)))
(if return?
stop-func
(with-handlers ([exn:break? (λ (e) (stop-func) (message "project server stopped"))])
(do-not-return))))

@ -1 +1 @@
1587667269 1587667365

Loading…
Cancel
Save