我最近刚刚和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)
当然,这不起作用,因为地图返回一个懒惰的序列而不是函数的主体(?).有人知道我需要做些什么来让这个想法发挥作用吗?
或者我应该使用完全不同的方法来减少重复自己?
我有基于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 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) 我正在尝试用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上.
AS标题,
我的IDE是intellij idea 12.1.4,
我需要什么工具包或插件才能将clojure Web应用程序部署到Amazon EC2?
是否有任何链接或参考或逐步解决方案?谢谢
deployment clojure amazon-ec2 compojure amazon-elastic-beanstalk
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 …
我正在尝试通过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)
导致空洞的回应......
我需要做些什么不同的事情?
谢谢,
马蒂亚斯
我复制了一些在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>
难道我做错了什么?
我借用的代码旨在回答这个问题.
我想弄清楚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 ×10
compojure ×10
ring ×3
amazon-ec2 ×1
clj-pdf ×1
dependencies ×1
deployment ×1
enlive ×1
hiccup ×1
la-clojure ×1
leiningen ×1
liberator ×1
pdf ×1
pedestal ×1