]> git.pond.sub.org Git - eow/blob - web.lisp
Handle connection refused and closed
[eow] / web.lisp
1 (in-package :empire-web)
2
3 (declaim (optimize (space 0) (speed 0) (safety 3) (debug 3)))
4
5
6 (defvar *this-file* (load-time-value
7                      (or #.*compile-file-pathname* *load-pathname*)))
8
9 (defvar *this-dir* (make-pathname :host (pathname-host *this-file*)
10                                   :device (pathname-device *this-file*)
11                                   :directory (pathname-directory *this-file*)))
12
13 (defparameter +templates-root+ (namestring *this-dir*))
14 (defparameter +root-url+ "/eow/static/test.html")
15 (defparameter +web-root-base+ "/eow")
16 (defparameter +web-root+ (concatenate 'string +web-root-base+ "/"))
17 (defparameter +static-web-root+ (concatenate 'string +web-root+ "static/"))
18 (defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/"))
19 (defparameter +login-page+ (concatenate 'string +static-web-root+ "login.html"))
20
21 (defun string-starts-with (string prefix)
22   ;; (from Hunchentoot)
23   (let ((mismatch (mismatch string prefix :test #'char=)))
24     (or (null mismatch)
25         (>= mismatch (length prefix)))))
26
27 (defun serve-static ()
28   "Handle a request for a file under static/ directory"
29   (let* ((script-name (script-name))
30          (fname (subseq script-name (length +static-web-root+)))
31          (fullname (concatenate 'string +static-files-root+ fname)))
32     (handle-static-file fullname)))
33
34 (defvar *update-queue* (locked-queue:create))
35
36 (defun update ()
37   "Send stream of updates to client"
38   (locked-queue:dequeue *update-queue*))
39
40 (defun send (string)
41   "Push a javascript update fragment to the client."
42   (locked-queue:enqueue *update-queue* string))
43
44 (defun prompt (minutes btus)
45   (send (format nil "prompt(~a,~a);~%" minutes btus)))
46
47 (defun data (message)
48   (send (parenscript:ps* `(msg ,message))))
49
50 (defun login ()
51   (let ((connection (session-value 'connection)))
52     (if connection
53         (redirect +root-url+)
54         (redirect +login-page+))))
55
56 ;; destination of login-form
57 (defun login-action ()
58   (handler-case
59       (let ((connection (empire:connect :user (post-parameter "username")
60                                         :password (post-parameter "password"))))
61         (setf (session-value 'connection) connection)
62         (redirect +root-url+))
63     (usocket:connection-refused-error (e)
64       (format nil "Connection error: ~a~%" e))))
65
66 (defun command-action ()
67   (let ((connection (session-value 'connection)))
68     (empire:send-message connection (get-parameter "q"))))
69
70 (defun dispatch (request)
71   (let ((script-name (script-name request)))
72     (cond
73       ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
74       ((string= script-name "/eow/update") 'update)
75       ((string= script-name "/eow/login") 'login-action)
76       ((string= script-name "/eow/command") 'command-action)
77       ((or (string-equal script-name +web-root-base+)
78            (string-equal script-name +web-root+)) 'login) ; go to the start page
79       ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file
80
81
82 (defun start ()
83   (pushnew 'dispatch *dispatch-table* :test #'eq))
84