从列表创建Compojure路由

Mik*_*ike 7 clojure compojure

我最近刚刚和Compojure一起玩,而且我有一个小的基本webapp.对于我的HTML模板,我使用Enlive,并且我有一个包含所有简单静态页面的命名空间.对这些页面的defroute调用如下所示:

(defroutes public-routes
  (GET "/" []
    (info/index-template))
  (GET "/about" []
    (info/about-template))
  (GET "/contact" []
    (info/contact-template)))
Run Code Online (Sandbox Code Playgroud)

我实际上得到了更多,但这应该让我知道我在做什么.

现在,我想,这真的只是我的一堆重复,所以我想我会尝试以下方法:

(defroutes info-routes
  (map #(GET (str "/" %) [] (ns-resolve 'webapp.pages.info
                                        (symbol (str % "-template"))))
       '("about" "contact")))
Run Code Online (Sandbox Code Playgroud)

当然,这不起作用,因为地图返回一个懒惰的序列而不是函数的主体(?).有人知道我需要做些什么来让这个想法发挥作用吗?

或者我应该使用完全不同的方法来减少重复自己?

Nic*_*roi 6

您始终可以使用routesdefroutes 使用的功能:

(defroutes info-routes
  (apply routes
    (map #(GET (str "/" %) [] 
               (ns-resolve 'webapp.pages.info
                           (symbol (str % "-template"))))
         '("about" "contact"))))
Run Code Online (Sandbox Code Playgroud)

但这仍然很无聊,让我们加油吧!;-)

(defn templates-for [& nss]
  (->> nss
       (map ns-publics)
       (apply concat)
       (filter #(->> % first str
                     (re-seq #"-template$")))
       (map second)))

(defn template-uri [template]
  (->> template meta :name name
       (re-seq  #"(.*)-template$")
       first second (str "/")))

(defn template->route [template]
  (GET (template-uri template) [] template))

(defroutes public-routes
  (GET "/" [] "foo")
  (apply routes (map template->route
                     (templates-for 'webapp.pages.info))))
Run Code Online (Sandbox Code Playgroud)

使用此代码,该templates-for函数将查找在给定名称空间中以"-template"结尾的任何函数,并使用它们编写适当的路径.看看我没有使用任何宏,但有很多组成.