Search code examples
common-lispexecutablesbclhunchentoot

SBCL: Deploying Hunchentoot application as executable


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?


Solution

  • (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.