2

我对 Common Lisp (SBCL) 和 Hunchentoot (使用 Quicklisp) 比较陌生。有人能告诉我如何让它工作吗?我正在尝试将 Hunchentoot 服务器和一些路径作为一个单元包装在一个函数中。当我运行它时,只有 Hunchentoot 的索引页面可用,路径 /a 和 /b 不可用。

(defun app0 (port)
  (let ((*dispatch-table* nil) (server (make-instance 'hunchentoot:acceptor :port port)))
    (push (hunchentoot:create-prefix-dispatcher "/a" (lambda () "a")) *dispatch-table*)
    (push (hunchentoot:create-prefix-dispatcher "/b" (lambda () "b")) *dispatch-table*)
    (hunchentoot:start server) server))
4

1 回答 1

4

据我所知,存在多个问题。首先,通过*dispatch-table*要求处理请求,接受器的类型为easy-acceptor,即,您必须

(make-instance 'easy-acceptor ...)

文档有详细信息。

第二个问题是,您*dispatch-table*在设置代码期间重新绑定 ,并将新值推送到此绑定中。由于绑定在let完成后恢复(并且由于hunchentoot:start异步工作),因此当服务器运行时,您在 中的条目*dispatch-table*实际上会丢失。尝试

(push (hunchentoot:create-prefix-dispatcher "/a" (lambda () "a")) *dispatch-table*)
(push (hunchentoot:create-prefix-dispatcher "/b" (lambda () "b")) *dispatch-table*)

在顶层(或在专用设置功能中执行类似的操作)。如果您不喜欢全局*dispatch-table*方法,您还可以创建 , 的子类acceptor并覆盖acceptor-dispatch-request(因此,实现您喜欢的任何类型的调度)。

顺便说一句:您不添加前缀,而您几乎可以为' 包中的*dispatch-table*任何其他符号添加前缀。hunchentoot这只是一个复制/粘贴错误,还是在您的实际代码中也是如此?如果您的代码碰巧存在:use的包中没有包,那么您还必须将调度表限定为.hunchentoothunchentoot:*dispatch-table*

编辑(以解决评论部分中的问题)hunchentoot 文档中有一个示例,它似乎完全符合您的要求:

(defclass vhost (tbnl:acceptor)
  ((dispatch-table
    :initform '()
    :accessor dispatch-table
    :documentation "List of dispatch functions"))
  (:default-initargs  
   :address "127.0.0.1"))

(defmethod tbnl:acceptor-dispatch-request ((vhost vhost) request)
  (mapc (lambda (dispatcher)
      (let ((handler (funcall dispatcher request)))
        (when handler
          (return-from tbnl:acceptor-dispatch-request (funcall handler)))))
    (dispatch-table vhost))
  (call-next-method))

(defvar vhost1 (make-instance 'vhost :port 50001))
(defvar vhost2 (make-instance 'vhost :port 50002))

(push
 (tbnl:create-prefix-dispatcher "/foo" 'foo1)
 (dispatch-table vhost1))
(push
 (tbnl:create-prefix-dispatcher "/foo" 'foo2)
 (dispatch-table vhost2))

(defun foo1 () "Hello")
(defun foo2 () "Goodbye")

(tbnl:start vhost1)
(tbnl:start vhost2)

(为简洁起见,删除了文档中的评论)。这tbnl是 package 的预定义昵称hunchentoot。尽管我建议您选择一个并坚持使用,但您可以互换使用两者。将两者混合可能会产生混淆。

于 2017-02-28T17:42:41.007 回答