SBCL: Deploying Hunchentoot application as executable

1.3k Views Asked by At

I started playing with SBCL Common Lisp and want to develop a small web application using Hunchentoot. For easy deployment I planned to save everything in a binary using sb-ext:save-lisp-and-die as I can live with the big output size.

For the executable you need to supply a toplevel function. The problem is that the program exits when the toplevel function returns. I tried to start Hunchentoot from the executable, but the program ended after two seconds.

How can I wait until Hunchentoot was shut down (from inside a request) before stopping the program? Can I do something like join the Hunchentoot acceptor thread? Or can I even include the REPL into the executable to be able to do live debugging?

2

There are 2 best solutions below

4
On BEST ANSWER
(ql:quickload :hunchentoot)
(use-package :hunchentoot)

(defun main ()
  (hunchentoot:start-server :port 8082)
  (sb-thread:join-thread (find-if
                          (lambda (th)
                            (string= (sb-thread:thread-name th) "hunchentoot-listener-1"))
                          (sb-thread:list-all-threads))))

No explicit code is required to give you access to a REPL if you keep a terminal open (perhaps via GNU Screen). Send Ctrl+C to the terminal to break into the debugger.

1
On
;;; I simply use sleep to yield the main thread.
;;; To start the server while developing I use
;;; start-server.  For deployment I use the main
;;; function.

(defun start-server ()
  (hunchentoot:start
    (make-instance 'hunchentoot:easy-acceptor :port 8000)))

(defun main()
  (start-server)
  (sleep #xffffffff))