diff --git a/src/handler.lisp b/src/handler.lisp index 5f569dd..b39bdb1 100644 --- a/src/handler.lisp +++ b/src/handler.lisp @@ -3,6 +3,8 @@ (:use :cl) (:import-from :lack.util :find-package-or-load) + (:import-from :bordeaux-threads + :make-thread) (:export :run :stop)) (in-package :lack.handler) @@ -20,15 +22,22 @@ (error "~S is unknown handler." server)))) -(defun run (app server &rest args) +(defun run (app server &rest args &key use-thread &allow-other-keys) (let ((handler-package (find-handler server))) - (make-handler - :server server - :acceptor - (apply (intern #.(string '#:run) handler-package) - app - :allow-other-keys t - args)))) + (flet ((run-server () + (apply (intern #.(string '#:run) handler-package) + app + :allow-other-keys t + args))) + (make-handler + :server server + :acceptor (if use-thread + (bt:make-thread #'run-server + :name (format nil "lack-handler-~(~A~)" server) + :initial-bindings + `((*standard-output* . ,*standard-output*) + (*error-output* . ,*error-output*))) + (run-server)))))) (defun stop (handler) (let ((acceptor (handler-acceptor handler))) diff --git a/src/lack.lisp b/src/lack.lisp index 04611ef..8111e27 100644 --- a/src/lack.lisp +++ b/src/lack.lisp @@ -17,8 +17,16 @@ &key (server :hunchentoot) (port 5000) (debug t) + (use-thread #+thread-support t #-thread-support nil) &allow-other-keys) - (apply #'lack.handler:run app server - :port port - :debug debug - (delete-from-plist args :server :use-thread :port :debug))) + (flet ((start-message () + (format t "~&~:(~A~) server is started.~%Listening on localhost:~A.~%" server port))) + (unless use-thread + (start-message)) + (prog1 + (apply #'lack.handler:run app server + :port port + :debug debug + :use-thread use-thread + (delete-from-plist args :server :port :debug :use-thread)) + (start-message))))