标签: compojure

从列表创建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)

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

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

clojure compojure

7
推荐指数
1
解决办法
708
查看次数

在Clojure中验证compojure请求时,从字符串中解析整数

我有基于compojure的应用程序,我需要解析一个请求并检索可以是数字的参数.我希望能够在实际处理请求之前验证参数是否存在以及它们是否为数字.这是我到目前为止:

(defn get-int [str]
  "Returns nil if str is not a number"
  (try (Integer/parseInt str)
    (catch NumberFormatException _)))

(defn some-request [request]
    (let [some-number (get-int (get-in request [:route-params :some-number])
         other-number (get-int (get-in request [:route-params :other-number])]
    (if (every? identity [some-number other-number])
        (process-the-request)
        (bad-request "The request was malformed")))
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来进行字符串 - >数字转换?

有没有更好的方法来进行请求验证?

clojure compojure

7
推荐指数
1
解决办法
1758
查看次数

使用Hiccup和Compojure编写模板

我对Clojure和Compojure Web开发相对较新.在我正在构建的玩具示例中,我注意到的第一个问题是HTML模板.我想支持Rails中的partials或Django使用的模板框架.

目前我有:

(defn index-page []
(html5
    [:head
        [:title "Home | Compojure Docs"]
        (include-css "/css/bootstrap.min.css")
        (include-css "/css/bootstrap-responsive.min.css")]
    [:body
        [:div {:class "container-fluid"}
            [:div {:class "row-fluid"}
                [:div {:class "span2 menu"}]
                [:div {:class "span10 content"}
                    [:h1 "Compojure Docs"]
                    [:ul
                        [:li
                            [:a {:href "/getting-started"} "Getting Started"]]
                        [:li
                            [:a {:href "/routes-in-detail"} "Routes in Detail"]]
                        [:li
                            [:a {:href "/destructuring-syntax"} "Destructuring Syntax"]]
                        [:li
                            [:a {:href "/nesting-routes"} "Nesting Routes"]]
                        [:li
                            [:a {:href "/api-documentation"} "API Documentation"]]
                        [:li
                            [:a {:href "/paas-platforms"} "PaaS Platforms"]]
                        [:li
                            [:a {:href "/example-project"} "Example Project"]]
                        [:li …
Run Code Online (Sandbox Code Playgroud)

clojure compojure hiccup

7
推荐指数
1
解决办法
4182
查看次数

如何通过逆时针(或la clojure)运行/调试compojure web应用程序

我正在尝试用compojure编写我的第一个Web应用程序.我正在使用ccw,而且我File-New-Project, Clojure Project使用"compojure"leiningen模板.最终看起来像project.clj

(defproject asdf "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [compojure "1.1.5"]]
  :plugins [[lein-ring "0.8.2"]]
  :ring {:handler asdf.handler/app}
  :profiles
  {:dev {:dependencies [[ring-mock "0.1.3"]]}})
Run Code Online (Sandbox Code Playgroud)

src/asdf/handler.clj看起来像

(ns asdf.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]))

(defroutes app-routes
  (GET "/" [] "Hello World")
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))
Run Code Online (Sandbox Code Playgroud)

我发现我可以从命令行运行它lein ring server,但我不知道如何从eclipse运行它.我当然希望不仅能够运行它,还能调试它并设置断点等.在eclipse中有没有办法做到这一点?或者,如果没有,IntelliJ/La-Clojure怎么样?(我现在有点害怕emacs,但也许如果它超级简单我会尝试一下).

或者,这不是一个compojure应用程序的典型开发过程吗?(如果不是,那是什么?只是跑步lein ring server并祈祷?)

如果它有所作为,这是在Win7上.

clojure compojure counterclockwise la-clojure

7
推荐指数
1
解决办法
3498
查看次数

如何将clojure Web应用程序部署到Amazon EC2(AWS Elastic Beanstalk + Leiningen + Compojure + Ring + Tomcat)

AS标题,
我的IDE是intellij idea 12.1.4,
我需要什么工具包或插件才能将clojure Web应用程序部署到Amazon EC2?

是否有任何链接或参考或逐步解决方案?谢谢

deployment clojure amazon-ec2 compojure amazon-elastic-beanstalk

7
推荐指数
1
解决办法
2827
查看次数

Clojure web frameworks for responsive apps

I have recently inherited a non-finished web app written in Clojure, based on compojure and hiccup basically. It's a bad attempt to model some sort of MVC with OO style not in the FP style as seen here . So I bet to re-start the project almost from the scratch reusing the useful parts. I consider these alternatives:

The least breaking alternative would be Compojure+Enlive+jquery-pjax

Using a clojure web framework like Pedestal Any experiences about this?

The initial idea was …

clojure compojure enlive liberator pedestal

7
推荐指数
1
解决办法
4182
查看次数

如何用环提供流pdf

我正在尝试通过ring/compojure直接提供clj-http生成的文档.

我以为ring.util/piped-output-stream会起作用,但似乎我在这里不了解一些东西......

这个:

(defn laminat-pdf-t
  [natno]
  (piped-input-stream
   (fn [output-stream])
   (pdf
    [ {:title (str "Omanimali-Kuscheltierpass" natno)
       :orientation :landscape
       :size :a6
       :author "Omanimali - Stefanie Tuschen"
       :register-system-fonts true
       }
      ;; [:svg {} (clojure.java.io/file
      ;;           (str "/einbuergern/" natno "/svg" ))]
      [:paragraph "Some Text"]      ]
    output-stream)))

(defn laminat-pdf
  "generate individualized cuddly toy passport page"
  [natno]
  {:headers {"Content-Type" "application/pdf"}
   :body (laminat-pdf-t natno)})
Run Code Online (Sandbox Code Playgroud)

导致空洞的回应......

我需要做些什么不同的事情?

谢谢,

马蒂亚斯

pdf clojure compojure ring clj-pdf

7
推荐指数
1
解决办法
1580
查看次数

在没有创建Maven回购的情况下,Leiningen的本地依赖关系?

我正在构建一个Compojure Web应用程序,我希望它能够使用我编写的另一个Clojure项目中的函数.我对Maven并不熟悉,从我所听到的,它有一个非常陡峭的学习曲线.不幸的是,我所看到的一切 建议使用私有Maven仓库作为依赖,并没有提出替代方案.如果可能的话,我真的很想避免与Maven挣扎.有没有人知道另一种选择?我目前正在使用最新版的Leiningen.

dependencies clojure compojure leiningen

7
推荐指数
1
解决办法
2771
查看次数

如何使用最新版本的ring/compojure环防伪/ CSRF令牌?

我复制了一些在compojure 1.1.18和其他旧库中运行的旧代码,但是使用最新版本我无法使用它.

这里是我的小例子,代码从复制这里的小例子来证明,用最新环的Compojure库,我得到一个错误,当我发送一个HTTP POST,甚至与报头组.

lein ring server 启动它,然后做

curl -X GET --cookie-jar cookies "http://localhost:3000/" 结果是这样的:

{"csrf-token":"7JnNbzx8BNG/kAeH4bz1jDdGc7zPC4TddDyiyPGX3jmpVilhyXJ7AOjfJgeQllGthFeVS/rgG4GpkUaF"}
Run Code Online (Sandbox Code Playgroud)

但是当我这样做的时候

curl -X POST -v --cookie cookies -F "email=someone@gmail.com" --header "X-CSRF-Token: 7JnNbzx8BNG/kAeH4bz1jDdGc7zPC4TddDyiyPGX3jmpVilhyXJ7AOjfJgeQllGthFeVS/rgG4GpkUaF" http://localhost:3000/send
Run Code Online (Sandbox Code Playgroud)

我明白了 <h1>Invalid anti-forgery token</h1>

难道我做错了什么?

我借用的代码旨在回答这个问题.

clojure compojure ring csrf-protection

7
推荐指数
1
解决办法
3046
查看次数

为什么ring的资源响应会响应application/octet-stream内容类型?

我想弄清楚Ring为什么resource-response选择回复application/octet-stream内容类型.我最近更新了一些我一直在学习的示例代码,以便它使用更新的代码ring-defaults.在使用之前ring-defaults,此代码使用html内容类型进行响应.为什么现在选择八位字节流?

(ns replays.handler
  (:require [compojure.core :refer [GET defroutes]]
            [compojure.route :as route]
            [ring.util.response :refer [response resource-response]]
            [ring.middleware.json :as middleware]
            [ring.middleware.defaults :refer [wrap-defaults api-defaults]]))

(defroutes app-routes
  (GET  "/" [] (resource-response "index.html" {:root "public"}))
  (GET  "/widgets" [] (response [{:name "Widget 1"} {:name "Widget 2"}]))
  (route/resources "/public")
  (route/not-found "not found"))

(def app
  (-> app-routes
      (middleware/wrap-json-body)
      (middleware/wrap-json-response)
      (wrap-defaults api-defaults)))
Run Code Online (Sandbox Code Playgroud)

而且,对于版本号,这是项目文件......

(defproject replays "0.1.0-SNAPSHOT"

  :url "http://example.com/FIXME"
  :description "FIXME: write description"

  :plugins [[lein-pdo "0.1.1"]
            [lein-ring "0.9.3"]
            [lein-cljsbuild …
Run Code Online (Sandbox Code Playgroud)

clojure compojure ring

6
推荐指数
1
解决办法
1978
查看次数