#lang scheme/base
(require mzlib/etc
web-server/web-server)
(require (file "defaults.ss")
(prefix-in dispatcher: (file "dispatcher.ss")))
(provide instaweb
instaweb-here
(rename-out (dispatcher:make make-dispatcher))
run-server
console-loop)
(define-syntax instaweb-here
(syntax-rules ()
[(instaweb/here arg ...)
(parameterize [(current-directory (this-expression-source-directory))]
(instaweb arg ...))]))
(define (instaweb #:port [port 8765]
#:listen-ip [listen-ip "127.0.0.1"]
#:servlet-path [servlet-path default-servlet-path]
#:htdocs-path [htdocs-path default-htdocs-path]
#:mime-types-path [mime-types-path default-mime-types-path]
#:servlet-namespace [servlet-namespace default-servlet-namespace])
(define (run-server-thunk)
(run-server port
listen-ip
#:servlet-path servlet-path
#:htdocs-path htdocs-path
#:mime-types-path mime-types-path
#:servlet-namespace servlet-namespace))
(parameterize
([print-hash-table #t]
[print-struct #t]
[error-print-width 1024]
[error-print-context-length 50])
(console-loop run-server-thunk)))
(define (run-server port listen-ip
#:servlet-path servlet-path
#:htdocs-path htdocs-path
#:mime-types-path mime-types-path
#:servlet-namespace servlet-namespace)
(define dispatch
(dispatcher:make #:servlet-path servlet-path
#:htdocs-path htdocs-path
#:mime-types-path mime-types-path
#:servlet-namespace servlet-namespace))
(begin0 (serve #:dispatch dispatch
#:port port
#:listen-ip listen-ip)
(printf "Web server started on port ~a\n" port)
(printf "Listening on IP address: ~a\n"
(if listen-ip listen-ip "all addresses"))))
(define (console-loop run-server)
(define (display-usage)
(printf "Type stop to stop the server and exit\n")
(printf "Type restart to restart the server\n"))
(let ([stop-server (run-server)])
(display-usage)
(let loop ([cmd (read)])
(cond
[(eof-object? cmd)
(printf "Instaweb: Received EOF from input port. Will not read further\n")
(thread-wait (current-thread))]
[(eq? cmd 'stop) (stop-server)]
[(eq? cmd 'restart)
(stop-server)
(console-loop run-server)]
[else (printf "Don't know what to do with ~a. Try again.\n" cmd)
(display-usage)
(loop (read))]))))