据我所见,存在多个问题。首先,请求处理通过*dispatch-table*
要求,该受体为easy-acceptor
类型,即,你将不得不
(make-instance 'easy-acceptor ...)
的documentation有细节。
第二个问题是,您在设置代码期间重新绑定*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
hunchentoot
程序包,那么您还必须将调度表限定为hunchentoot:*dispatch-table*
。
编辑(以解决在评论部分的问题)有一个example in the hunchentoot documentation,这似乎做你想要做什么:
(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
是包hunchentoot
的预定义昵称。尽管我会推荐你可以互换使用,你可以选择一个并坚持下去。将两者混合可能会产生混淆。
'* dispatch-table *'已经是一个全局变量。 –