(module instaweb mzscheme
(require (lib "kw.ss")
(lib "etc.ss")
(lib "web-server.ss" "web-server"))
(require (file "dispatcher.ss"))
(provide instaweb
instaweb-here)
(define-syntax instaweb-here
(syntax-rules ()
[(instaweb/here arg ...)
(parameterize
[(current-directory (this-expression-source-directory))]
(instaweb arg ...))]))
(define instaweb
(lambda/kw (#:key [port 8765]
[listen-ip "127.0.0.1"]
#:other-keys dispatch-args)
(define dispatcher (apply make dispatch-args))
(define (run-server)
(serve #:dispatch dispatcher
#:port port
#:listen-ip listen-ip))
(define (display-usage)
(printf "Web server started on port ~a\n" port)
(printf "Listening on IP address: ~a\n"
(if listen-ip listen-ip "all addresses"))
(printf "Type stop to stop the server and exit\n")
(printf "Type restart to restart the server\n"))
(define (server-loop stop-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)
(server-loop (run-server))]
[else (printf "Don't know what to do with ~a. Try again.\n" cmd)
(display-usage)
(loop (read))])))
(parameterize
([print-hash-table #t]
[print-struct #t]
[error-print-width 1024]
[error-print-context-length 50])
(server-loop (run-server)))))
)