在嵌套的Compojure路由中使用空路径参数

时间:2016-02-14 13:13:49

标签: clojure compojure

如何在Compojure嵌套上下文中匹配没有尾部退格的URL?

问题我试图解决:

我想匹配以下路线:

/users/
/users/:user-id
/users/:user-id/resources/

第一个访问所有用户, 第二个用于单个用户实体, 最后是给定用户的所有资源。

使用以下defroutes作品:

(defroutes
  (GET "/users/" ...)
  (GET "/users/:user-id" ...)
  (GET "/users/:user-id/resources/" ...))

然而,随着我的API增长,我想使用上下文来澄清代码。

(defroutes
  (context "/users"
    (users-routes) ;; All users operations
    (context "/:user-id" [user-id]
      (user-routes user-id) ;; All per-user operations
      (context "/resources" []
        (resources-routes user-id))))) ;; All user - resources operations

这意味着user-routes应该是这样的:

(defn user-routes [user-id]
  (routes
    (GET "" [uid] ...)))

在编译时给出了:

             clojure.lang.ExceptionInfo: Parse error in route string
    data: {:failure
           {:index 0,
            :reason
            [{:tag :regexp, :expecting #"(https?:)?//"}
             {:tag :string, :expecting ":"}
             {:tag :string, :expecting "*"}
             {:tag :regexp, :expecting #"\\."}
             {:tag :regexp, :expecting #"(:[^\p{L}_*{}\\]|[^:*{}\\])+"}],
            :line 1,
            :column 1,
            :text nil}}
clojure.lang.Compiler$CompilerException: clojure.lang.ExceptionInfo: Parse error in route string {:failure Parse error at line 1, column 1:
                                         nil
                                         ^
                                         Expected one of:
                                         #"(https?:)?//"
                                         ":"
                                         "*"
                                         #"\\."
                                         #"(:[^\p{L}_*{}\\]|[^:*{}\\])+"
                                         }, compiling:(/Users/laurent/dev/suricate/web/src/web/routes/api.clj:134:5)

我认为这是由于解析网址的方式(使用Clout)因此我的问题,

在Compojure嵌套上下文中匹配没有尾随退格的URL的正确方法是什么?

0 个答案:

没有答案
相关问题