Skip to content

Commit

Permalink
Convert to use Hunchentoot web server.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Paul Simons committed Oct 14, 2020
1 parent 862b9b3 commit 7f58d23
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
64 changes: 41 additions & 23 deletions dice-of-doom-v3.lisp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
;; (ql:quickload :hunchentoot)

(require "asdf")
(require "hunchentoot")
(use-package :hunchentoot)

(load "dice-of-doom-v2.lisp")
(load "web-server.lisp")
;; (load "web-server.lisp")
(load "svg.lisp")

(defparameter *board-width* 900)
Expand Down Expand Up @@ -73,28 +79,26 @@
(defun make-game-link (position)
(format nil "/game.html?chosen=~a" position))

(defun dice-of-doom-request-handler (path header parameters)
(if (equal path "game.html")
(progn (format t "HTTP/1.1 200 OK")
(format t "Content-type: text/html; charset=UTF-8~%~%")
(princ "<!doctype html>")
(tag center ()
(princ "Welcome to DICE OF DOOM!")
(tag br ())
(let ((chosen (assoc 'chosen parameters)))
(when (or (not *current-game-tree*) (not chosen))
(setf chosen nil)
(web-initialize))
(cond ((lazy-null (caddr *current-game-tree*))
(web-announce-winner (cadr *current-game-tree*)))
((zerop (car *current-game-tree*))
(web-handle-human
(when chosen
(read-from-string (cdr chosen)))))
(t (web-handle-computer))))
(tag br ())
(draw-dice-of-doom-page *current-game-tree* *from-tile*)))
(princ "Sorry, I don't know that page.")))

This comment has been minimized.

Copy link
@npsimons

npsimons Oct 19, 2020

Owner

Not needed in the Hunchentoot version as the server handles this for us. Also means the path handling is unnecessary as our handler will only be called if it's the correct path.

(hunchentoot:define-easy-handler (game.html :uri "/game.html") (chosen)
(let* ((capture-output (make-string-output-stream))

This comment has been minimized.

Copy link
@npsimons

npsimons Oct 19, 2020

Owner

We do this here because it seems like the right place to do it; the model of Hunchentoot is different from the web server in "Land of Lisp" in that things are more localized to the handlers, instead of capturing stdout in the server itself.

(*standard-output* capture-output))
(princ "<!doctype html>")
(tag center ()
(princ "Welcome to DICE OF DOOM!")
(tag br ())
(when (or (not *current-game-tree*) (not chosen))
(setf chosen nil)
(web-initialize))
(cond ((lazy-null (caddr *current-game-tree*))
(web-announce-winner (cadr *current-game-tree*)))
((zerop (car *current-game-tree*))
(web-handle-human
(when chosen
(read-from-string chosen))))

This comment has been minimized.

Copy link
@npsimons

npsimons Oct 19, 2020

Owner

Hunchentoot passes in parameters as strings, so another big difference from the original handler is that the "chosen" parameter is now a string instead of a list.

(t (web-handle-computer)))
(tag br ())
(draw-dice-of-doom-page *current-game-tree* *from-tile*))
(get-output-stream-string capture-output)))

(defun web-initialize ()
(setf *from-tile* nil)
Expand Down Expand Up @@ -151,3 +155,17 @@
(cadar move)))
(caddr tree))
(lazy-mapcar #'caar (caddr tree)))))))

;; Taken from
;; https://stackoverflow.com/questions/19739527/how-do-i-start-hunchentoot
(defvar *acceptor* nil)

(defun start-server (port)
(stop-server)
(hunchentoot:start (setf *acceptor*
(make-instance 'hunchentoot:easy-acceptor
:port port))))

(defun stop-server ()
(when *acceptor*
(hunchentoot:stop *acceptor*)))
6 changes: 3 additions & 3 deletions svg.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
(list ,@(mapcar (lambda (x)
`(cons ',(car x) ,(cdr x)))
(pairs attributes)))
nil)
,@body
(print-tag ',name nil t)))
nil)
,@body
(print-tag ',name nil t)))

(defmacro html (&body body)
`(tag html ()
Expand Down

1 comment on commit 7f58d23

@npsimons
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of indentation, it looks like the whole defun is changed, but it's really just a few lines (I've commented on above).

Please # to comment.