]> git.pond.sub.org Git - eow/commitdiff
add basic web module
authorGerd Flaig <gefla@gefla-mac-zrh>
Sun, 23 Nov 2008 00:40:41 +0000 (01:40 +0100)
committerGerd Flaig <gefla@gefla-mac-zrh>
Sun, 23 Nov 2008 00:40:41 +0000 (01:40 +0100)
web.lisp [new file with mode: 0644]

diff --git a/web.lisp b/web.lisp
new file mode 100644 (file)
index 0000000..763b6f3
--- /dev/null
+++ b/web.lisp
@@ -0,0 +1,44 @@
+(in-package :empire-web)
+
+(declaim (optimize (space 0) (speed 0) (safety 3) (debug 3)))
+
+
+(defvar *this-file* (load-time-value
+                     (or #.*compile-file-pathname* *load-pathname*)))
+
+(defvar *this-dir* (make-pathname :host (pathname-host *this-file*)
+                                  :device (pathname-device *this-file*)
+                                  :directory (pathname-directory *this-file*)))
+
+(defparameter +templates-root+ (namestring *this-dir*))
+(defparameter +root-url+ "/eow/static/test.html")
+(defparameter +web-root-base+ "/eow")
+(defparameter +web-root+ (concatenate 'string +web-root-base+ "/"))
+(defparameter +static-web-root+ (concatenate 'string +web-root+ "static/"))
+(defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/"))
+
+(defun string-starts-with (string prefix)
+  ;; (from Hunchentoot)
+  (let ((mismatch (mismatch string prefix :test #'char=)))
+    (or (null mismatch)
+        (>= mismatch (length prefix)))))
+
+(defun serve-static ()
+  "Handle a request for a file under static/ directory"
+  (let* ((script-name (script-name))
+         (fname (subseq script-name (length +static-web-root+)))
+         (fullname (concatenate 'string +static-files-root+ fname)))
+    (handle-static-file fullname)))
+
+(defun dispatch (request)
+  (let ((script-name (script-name request)))
+    (cond
+      ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
+      ((or (string-equal script-name +web-root-base+)
+           (string-equal script-name +web-root+)) (redirect +root-url+)) ; go to the start page
+      ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file
+
+
+(defun start ()
+  (pushnew 'dispatch *dispatch-table* :test #'eq))
+