[racket] an example for web-server for local web development?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Thu Feb 2 19:59:15 EST 2012

I end up needing to look at an HTML file in a web-serving context
every once in a while.  Rather than copy the files over to my personal
web server, I do this instead:

;;;;;;;;;;;;;;;;;;;;;;;;;;
#!/usr/bin/env racket
#lang racket/base

;; Quick and dirty program to view a local file under a web serving
;; environment.

(require web-server/servlet
         web-server/servlet-env
         racket/cmdline)

(define a-file (command-line #:args (filename) filename))
(define-values (base name dir?) (split-path a-file))

(serve/servlet (lambda (req)
                 (error 'req "shouldn't ever be here"))
               #:servlet-path (string-append "/" (path->string name))
               #:servlet-regexp #px"$^"
               #:command-line? #t
               #:launch-browser? #t
               #:extra-files-paths (list
                                    (cond [(eq? base #f)
                                           (error 'serve "Don't know
how to serve ~e" a-file)]
                                          [(eq? base 'relative)
                                           (current-directory)]
                                          [(absolute-path? base)
                                           base]
                                          [else
                                           (build-path (current-directory)
                                                       base)])))
;;;;;;;;;;;;;;;;;;;;;;;;;;

I'm abusing a few arguments: I'm not caring at all about web-servlets
at all, so I set the regexp to the never-matching pattern, and setting
servlet-path to the name of a file that I'm trying to serve  Given
this utility, I can run:

     $ ./browser-view.rkt embedding.html

where embedding.html is running in a network content, so JavaScript
APIs such as XMLRPCRequest are happy to deal.


Is this useful for anyone else?

Posted on the users mailing list.