There are a number of ways to run Web servlets.
The fastest way to get a servlet running in the Web server is to use the "Insta" language in DrRacket. Enter the following into DrRacket:
#lang web-server/insta (define (start req) (response/xexpr `(html (head (title "Hello world!")) (body (p "Hey out there!")))))
And press Run. A Web browser will open up showing your new servlet. This servlet will only be accessible from your local machine.
The following API is provided to customize the server instance:
If you want more control over specific parameters, keep reading about web-server/servlet-env.
#lang racket (require web-server/servlet web-server/servlet-env) (define (start req) (response/xexpr `(html (head (title "Hello world!")) (body (p "Hey out there!"))))) (serve/servlet start)
#lang racket (require web-server/servlet web-server/servlet-env) (define (my-app req) (response/xexpr `(html (head (title "Hello world!")) (body (p "Hey out there!"))))) (serve/servlet my-app)
Let’s look at some of the customizations serve/servlet allows.
(serve/servlet my-app #:port 8080)
(serve/servlet my-app #:listen-ip #f)
(serve/servlet my-app #:servlet-path "/hello.rkt")
(serve/servlet my-app #:servlet-regexp #rx"")
(serve/servlet my-app #:servlet-path "/main")
(serve/servlet my-app #:command-line? #t)
#lang web-server (require web-server/servlet-env) (define (start req) (start (send/suspend (lambda (k-url) (response/xexpr `(html (body (a ([href ,k-url]) "Hello world!")))))))) (serve/servlet start #:stateless? #t)
(serve/servlet start [ #:command-line? command-line? #:connection-close? connection-close? #:launch-browser? launch-browser? #:quit? quit? #:banner? banner? #:listen-ip listen-ip #:port port #:servlet-path servlet-path #:servlet-regexp servlet-regexp #:stateless? stateless? #:stuffer stuffer #:manager manager #:servlet-namespace servlet-namespace #:server-root-path server-root-path #:extra-files-paths extra-files-paths #:servlets-root servlets-root #:servlet-current-directory servlet-current-directory #:file-not-found-responder file-not-found-responder #:mime-types-path mime-types-path #:ssl? ssl? #:ssl-cert ssl-cert #:ssl-key ssl-key #:log-file log-file #:log-format log-format]) → void start : (request? . -> . can-be-response?) command-line? : boolean? = #f connection-close? : boolean? = #f launch-browser? : boolean? = (not command-line?) quit? : boolean? = (not command-line?) banner? : boolean? = (not command-line?) listen-ip : (or/c false/c string?) = "127.0.0.1" port : tcp-listen-port? = 8000 servlet-path : string? = "/servlets/standalone.rkt"
servlet-regexp : regexp? =
(regexp (format "^~a$" (regexp-quote servlet-path))) stateless? : boolean? = #f stuffer : (stuffer/c serializable? bytes?) = default-stuffer
manager : manager? = (make-threshold-LRU-manager #f (* 128 1024 1024)) servlet-namespace : (listof module-path?) = empty server-root-path : path-string? = default-server-root-path
extra-files-paths : (listof path-string?) = (list (build-path server-root-path "htdocs"))
servlets-root : path-string? = (build-path server-root-path "htdocs") servlet-current-directory : path-string? = servlets-root
file-not-found-responder : (request? . -> . can-be-response?) =
(gen-file-not-found-responder (build-path server-root-path "conf" "not-found.html")) mime-types-path : path-string? = .... ssl? : boolean? = #f
ssl-cert : (or/c false/c path-string?) = (and ssl? (build-path server-root-path "server-cert.pem"))
ssl-key : (or/c false/c path-string?) = (and ssl? (build-path server-root-path "private-key.pem")) log-file : (or/c false/c path-string?) = #f
log-format : (or/c log-format/c format-req/c) = 'apache-default
start is loaded as a servlet and responds to requests that match servlet-regexp. The current directory of servlet execution is servlet-current-directory.
If launch-browser? is true, then a web browser is opened to "http://localhost:<port><servlet-path>". servlet-path has no other purpose, if servlet-regexp is provided.
If quit? is true, then the URL "/quit" ends the server.
Advanced users may need the following options:
The server listens on listen-ip and port port. If listen-ip is #f, then the server accepts connections to all of the listening machine’s addresses. Otherwise, the server accepts connections only at the interface(s) associated with the given string. For example, providing "127.0.0.1" (the default) as listen-ip creates a server that accepts only connections to "127.0.0.1" (the loopback interface) from the local machine.
If ssl-cert and ssl-key are not false, then the server runs in HTTPS mode with ssl-cert and ssl-key as the certificates and private keys.
The servlet is loaded with manager as its continuation manager. (The default manager limits the amount of memory to 64 MB and deals with memory pressure as discussed in the make-threshold-LRU-manager documentation.)
The server files are rooted at server-root-path (which is the distribution root by default.) File paths, in addition to the "htdocs" directory under server-root-path may be provided with extra-files-paths. These paths are checked first, in the order they appear in the list.
Other servlets are served from servlets-root. The modules specified by servlet-namespace are shared between servlets found in servlets-root and the current namespace (and therefore the start procedure.)
If a file cannot be found, file-not-found-responder is used to generate an error response.
If banner? is true, then an informative banner is printed. You may want to use this when running from the command line, in which case the command-line? option controls similar options.
MIME types are looked up at mime-types-path. By default the "mime.types" file in the server-root-path is used, but if that file does not exist, then the file that ships with the Web Server is used instead. Of course, if a path is given, then it overrides this behavior.
If log-file is given, then it used to log requests using log-format as the format. Allowable formats are those allowed by log-format->format. If log-format is a function, it is used directly to render the log entry.
If connection-close? is #t, then every connection is closed after one request. Otherwise, the client decides based on what HTTP version it uses.
One command-line utility is provided with the Web Server:
plt-web-server [-f <file-name> -p <port> -a <ip-address> --ssl]
The optional file-name argument specifies the path to a configuration-table S-expression (see configuration-table->sexpr for the syntax documentation.) If this is not provided, the default configuration shipped with the server is used. The optional port and ip-address arguments override the corresponding portions of the configuration-table. If the SSL option is provided, then the server uses HTTPS with "server-cert.pem" and "private-key.pem" in the current directory, with 443 as the default port. (See the openssl module for details on the SSL implementation.)
The configuration-table is given to configuration-table->web-config@ and used to construct a web-config^ unit, and is linked with the web-server@ unit. The resulting unit is invoked, and the server runs until the process is killed.