标签: compojure

使用compojure从数据库提供二进制文件

我有以下路线定义:

(require '[compojure.core :as ccore]
         '[ring.util.response :as response])

(def *main-routes*
     (ccore/defroutes avalanche-routes
       (ccore/GET "/" [] "Hello World 2")
       (ccore/GET "/images/:id" [id] (get-image-response id))))
Run Code Online (Sandbox Code Playgroud)

在此示例中,请求/像魅力一样工作并返回预期的Hello World 2.

get-images-response方法定义如下:

(defn get-image-response
  [id]
  (let [record (db/get-image id false)]
    (-> (response/response (:data record))
        (response/content-type (:content-type record))
        (response/header "Content-Length" (:size record)))))
Run Code Online (Sandbox Code Playgroud)

虽然我得到了404,但是二进制文件的服务还没有完成.有什么想法吗?

编辑:好的,问题与正在请求图像的事实有关/images/name.jpg.一旦我删除.jpg了处理程序被调用.所以问题是如何匹配除扩展名之外的任何内容?

clojure binary-data compojure

6
推荐指数
2
解决办法
2999
查看次数

解构形式和Compojure?

我想我会发布这篇文章,因为我在没有真正了解正在发生的事情的情况下通过猜测来完成工作,我认为如果有人解释它可能会有所帮助.

我理解如何在Compojure处理程序中获取:params map的元素:

(GET "/something" [some_arg] "this is the response body")
Run Code Online (Sandbox Code Playgroud)

要么

(GET "/something" {{some_arg "some_arg"} :params} "this is the response body")
Run Code Online (Sandbox Code Playgroud)

虽然我不完全明白这{some_arg "some_arg"}部分在做什么:(

我也想访问:remote-addr请求的一部分以及some_arg.我结束了

(GET "/something" {{some_arg "some_arg"} :params ip :remote-addr}
    (do-something-with some_arg ip))
Run Code Online (Sandbox Code Playgroud)

所以,我得到了未加引号的字符串,some_arg并且ip是我想要绑定值的变量的名称,但上面的地图不是有效的Clojure映射.它是如何工作的?

我还得到这是根据Ring请求映射(由某种方式由defroutes宏提供)进行评估,但上面的表达式不是函数或宏定义,那么它如何在我的代码中"存在"为有效表达式?宏观参数的正常规则是否存在某种暂停?我一直无法找到这种非Lisp'er可理解的解构形式语法的定义.

clojure destructuring compojure

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

使用lein uberjar生成的jar在NoClassDefFoundError上失败

我有一个简单的web应用程序与这个project.clj:

(defproject squirrel-money "1.0.0-SNAPSHOT"
  :description "Squirrel Money"
  :dependencies [[org.clojure/clojure "1.2.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [compojure "0.5.3"]
                 [ring/ring-jetty-adapter "0.3.5"]
                 [hiccup "0.3.1"]
                 [postgresql "8.4-701.jdbc4"]
                 [clj-time "0.2.0-SNAPSHOT"]]
  :dev-dependencies [[lein-eclipse "1.0.0"]]
  :main squirrel-money.main
  :repl-init-script "src/squirrel_money/init_repl.clj")
Run Code Online (Sandbox Code Playgroud)

我的主要看起来像这样:

(ns squirrel-money.main
  (:gen-class)
  (:use 
    [compojure.core]
    [ring.adapter.jetty])
  (:require 
    [compojure.route :as route]
    [squirrel-money.savings :as savings]))

(defn launch [routedef]
  (run-jetty routedef {:port 17080}))

(defroutes money-routes
  (GET "/savings" [] (savings/render))
  (route/not-found "Page not found"))

(defn -main [& args] (launch money-routes))
Run Code Online (Sandbox Code Playgroud)

使用REPL工作得很好.但是,当我生成一个jar lein uberjar并尝试执行它时:

java -jar squirrel-money-1.0.0-SNAPSHOT-standalone.jar
Run Code Online (Sandbox Code Playgroud)

它死于这个例外:

Exception in thread "main" java.lang.NoClassDefFoundError: compojure/response/Renderable …
Run Code Online (Sandbox Code Playgroud)

clojure compojure leiningen

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

"帮助Arthur找到他的受限制的课程"或"我怎样才能使谷歌应用程序引擎快乐"

在这里,我正在使用java.rmi.server.UID哪个令人不安的GAE.之后:只有我对骨头的依赖我才陷入僵局.

(ns helloworld.core
  (:use ;[hiccup.core]
        [hiccup.page-helpers :only (html5 include-css)]
        [clojure.contrib.string :only (split)]
        [compojure.core :only (defroutes GET)]
        [hiccup.middleware :only (wrap-base-url)])
  (:require [appengine-magic.core :as ae]
            [compojure.route :as route
              :only (resources not-found) ]
            [compojure.handler :as handler :only (site)])
  (:gen-class :extends javax.servlet.http.HttpServlet))

 (defn index-page
  ([name]
     (html5
      [:head
       [:title (str "Hello " name)]
       (include-css "/css/style.css")]
      [:body
       [:h1 (str "Hello " name)]]))
  ([] (index-page "World")))

(def match-opperator
  { "add"      +
    "subtract" -
    "multiply" *
    "divide"   /})

(defroutes hello-routes
  (GET "/:f/*" [f & x]
       (index-page …
Run Code Online (Sandbox Code Playgroud)

java google-app-engine clojure compojure appengine-magic

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

Compojure route params为空

我的Compojure网络应用程序([compojure"1.0.1"])总是收到一个空的参数图,尽管添加了wrap-params等.下面的代码示例:

(defroutes public-routes
  (PUT "/something" {params :params}
      (println (str "Params: " params))
      (do-put-something params)))

(def myapp 
  (-> public-routes
      ring-params/wrap-params))

(defn start-server []
  (future (jetty/run-jetty (var myapp) {:port 8080})))
Run Code Online (Sandbox Code Playgroud)

我已经尝试添加wrap-params,wrap-keyword-params和wrap-multipart-params但是当我使用httpie(或我的客户端)将其PUT到端点时,我发现params总是空的.有人可以帮忙吗?

谢谢!

clojure compojure

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

Clojure环wrap -json-params弄乱了JSON数组

我目前正在使用clojure中的一些REST API,我正在使用带有compojure的ring.middleware.format库来将jSON转换为clojure数据结构.

我遇到了一个很大的问题,因为发布到环形应用程序的JSON会将所有数组替换为数组中的第一个项目.IE它将把这个JSON发布到它

{
    "buyer":"Test Name",
    "items":[
        {"qty":1,"size":"S","product":"Red T-Shirt"},
        {"qty":1,"size":"M","product":"Green T-Shirt"}
    ],
    "address":"123 Fake St",
    "shipping":"express"
}
Run Code Online (Sandbox Code Playgroud)

对此

{
    "buyer": "Test Name",
    "items": {
        "qty": 1,
        "size": "M",
        "product": "Green T-Shirt"
    },
    "address": "123 Fake St",
    "shipping": "express"
}
Run Code Online (Sandbox Code Playgroud)

它适用于任何数组,包括数组是根元素时.

我在clojure中使用以下代码来返回json:

(defroutes app-routes
  (GET "/"
       []
       {:body test-data})
  (POST "/"
        {data :params}
        {:body data}))
        ;{:body (str "Printing " (count (data :jobs)) " jobs")}))

(def app
  (-> (handler/api app-routes)
      (wrap-json-params)
      (wrap-json-response)))
Run Code Online (Sandbox Code Playgroud)

GET路由没有正确的数组和输出问题,因此它必须是我获取数据或wrap-restful-params中间件的方式.

有任何想法吗?

clojure compojure ring

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

在clojure中访问POST json

编辑 - 如果您有兴趣,源代码在github上.谢谢


关于如何访问已发布到clojure中的url的json数据,我有点困惑; 我似乎无法让它工作.

这是我的路线:

(cc/POST "/add" 
request
(str ": " request))
Run Code Online (Sandbox Code Playgroud)

我不完全确定我必须代替什么request- 我刚看到一些在线博客并试图遵循它,但无法让它工作.

以下是我试图发布的内容:(来自提琴手)

提琴手发布数据

注意:请求标头端口在图像中是不同的; 这是一个错误,我试图弄乱这些数据,看看它是什么,所以请忽略上面图片中的那一部分

在卷曲中,我只是这样做:

curl -X POST -H "Content-Type: application/json" -d '{"foo":"bar","baz":5}' 
     http://localhost:3005/add
Run Code Online (Sandbox Code Playgroud)

看起来clojure没有收到我发布的json数据.

这是请求var包含的内容:

: {:scheme :http, :query-params {}, :form-params {}, :request-method :post, 
   :query-string nil, :route-params {}, :content-type "\"application/json\"", 
   :uri "/event", :server-name "localhost", :params {}, 
   :headers {"user-agent" "Fiddler", "content-type" "\"application/json\"", 
          "content-length" "23", "host" "localhost:3005"}, 
   :content-length 23, :server-port 3005, :character-encoding nil, :body #}
Run Code Online (Sandbox Code Playgroud)

如你所见,一切params都是空的......

我正在使用compojure和cheshire - 我可以将数据转换为json并返回它们就好了 …

post json clojure compojure

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

如何在Compojure中使用lib-noir有状态会话

我想我在这里有一个相当直接的问题.但我一直在看这个屏幕太久了.所以我正在尝试(并且失败)在Compojure中使用有状态会话.refheap代码粘贴在这里.

您可以看到我尝试使用lib-noir(第62行)来初始化有状态会话.然后当应用程序运行时,我尝试调用session/put!会话中的一些数据(第43行).

现在,这个堆栈跟踪说在session.put!中,lib-noir正在尝试交换一个尚未绑定的会话var.现在,我以为我在第62行做到了.所以我确信这是一个简单的解决方案,另一组眼睛会看到.

__CODE__.例如:

java.lang.ClassCastException: clojure.lang.Var$Unbound cannot be cast to clojure.lang.Atom                                                                                                                                                                                                                                                                                                                                
        at clojure.core$swap_BANG_.invoke(core.clj:2110)                                                                                                                                                                                                                                                                                                                                                                  
        at noir.session$put_BANG_.invoke(session.clj:18)                                                                                                                                                                                                                                                                                                                                                                  
        at bkell.http.handler$fn__6159.invoke(handler.clj:156)                                                                                                                                                                                                                                                                                                                                                            
        at compojure.core$make_route$fn__3800.invoke(core.clj:93)                                                                                                                                                                                                                                                                                                                                                         
        at compojure.core$if_route$fn__3784.invoke(core.clj:39)                                                                                                                                                                                                                                                                                                                                                           
        at compojure.core$if_method$fn__3777.invoke(core.clj:24)                                                                                                                                                                                                                                                                                                                                                          
        at compojure.core$routing$fn__3806.invoke(core.clj:106)                                                                                                                                                                                                                                                                                                                                                           
        at clojure.core$some.invoke(core.clj:2390)                                                                                                                                                                                                                                                                                                                                                                        
        at compojure.core$routing.doInvoke(core.clj:106)                                                                                                                                                                                                                                                                                                                                                                  
        at clojure.lang.RestFn.applyTo(RestFn.java:139)                                                                                                                                                                                                                                                                                                                                                                   
        ...
        at java.lang.Thread.run(Thread.java:619)
Run Code Online (Sandbox Code Playgroud)

显然,__CODE__如果您要为不同的注释类型调用不同的segue,则可以检查与之关联的注释类型等.

当然,如果你想像__CODE__往常一样将任何信息传递给下一个场景.

java.lang.ClassCastException: clojure.lang.Var$Unbound cannot be cast to clojure.lang.Atom                                                                                                                                                                                                                                                                                                                                
        at clojure.core$swap_BANG_.invoke(core.clj:2110)                                                                                                                                                                                                                                                                                                                                                                  
        at noir.session$put_BANG_.invoke(session.clj:18)                                                                                                                                                                                                                                                                                                                                                                  
        at bkell.http.handler$fn__6159.invoke(handler.clj:156)                                                                                                                                                                                                                                                                                                                                                            
        at compojure.core$make_route$fn__3800.invoke(core.clj:93)                                                                                                                                                                                                                                                                                                                                                         
        at compojure.core$if_route$fn__3784.invoke(core.clj:39)                                                                                                                                                                                                                                                                                                                                                           
        at compojure.core$if_method$fn__3777.invoke(core.clj:24)                                                                                                                                                                                                                                                                                                                                                          
        at compojure.core$routing$fn__3806.invoke(core.clj:106)                                                                                                                                                                                                                                                                                                                                                           
        at clojure.core$some.invoke(core.clj:2390)                                                                                                                                                                                                                                                                                                                                                                        
        at compojure.core$routing.doInvoke(core.clj:106)                                                                                                                                                                                                                                                                                                                                                                  
        at clojure.lang.RestFn.applyTo(RestFn.java:139)                                                                                                                                                                                                                                                                                                                                                                   
        ...
        at java.lang.Thread.run(Thread.java:619)
Run Code Online (Sandbox Code Playgroud)

如您所见,我将__CODE__对象传递给下一个视图.如果JSON结构中的其他字段与每个注释相关联,则一个简单的解决方案是将属性添加到与要跟踪的每个字段关联的自定义视图中.只需转到.h作为注释自定义类并添加所需的属性.然后,当您创建自定义注释(要添加到地图)时,也可以设置这些属性.然后,当您将此注释传递给下一个视图控制器时,所有所需的属性都将在那里可用. …

web-applications clojure compojure noir

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

如何在Ring-Compojure应用程序上设置Content-Type标头

我正在尝试通过实现一个简单的Web应用程序开始使用Clojure和Clojurescript.到目前为止事情进展顺利,从不同的教程中读到我提出的代码如下:

core.clj:

(ns myapp.core
(:require [compojure.core :as compojure]
          [compojure.handler :as handler]
          [compojure.route :as route]
          [myapp.controller :as controller]))

(compojure/defroutes app-routes
  (compojure/GET "/" [] controller/index)
  (route/resources "/public")
  (route/not-found "Not Found"))

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

controller.clj:

(ns myapp.controller
  (:use ring.util.response)
  (:require [myapp.models :as model]
            [myapp.templates :as template]))

(defn index
  "Index page handler"
  [req]
  (->> (template/home-page (model/get-things)) response))
Run Code Online (Sandbox Code Playgroud)

templates.clj:

(ns myapp.templates
  (:use net.cgrand.enlive-html)
  (:require [myapp.models :as model]))


(deftemplate home-page "index.html" [things]
  [:li] (clone-for [thing things] (do->
                                   (set-attr 'data-id (:id thing))
                                   (content (:name thing)))))
Run Code Online (Sandbox Code Playgroud)

问题是我无法在页面上显示非ascii字符,我不知道如何在页面上设置HTTP标头. …

clojure compojure ring http-headers

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

为什么compojure路由被定义为宏?

例如,Luminus网站说明了这一点

Compojure路由定义只是接受请求映射和返回响应映射的函数......

(GET "/" [] "Show something")
...
Run Code Online (Sandbox Code Playgroud)

但是组合路线不是功能

(defmacro GET "Generate a `GET` route."
  [path args & body]
  (compile-route :get path args body))
Run Code Online (Sandbox Code Playgroud)

可以使用make-route返回函数的函数,但不允许进行解构.因此,作为一个函数,你不能使用compojure的特殊语法来破坏(即向量)但是这会阻止任何形式的解构吗?宏给它们提高了性能吗?

(make-route :get "/some_path" some_handler)
Run Code Online (Sandbox Code Playgroud)

难道不能使用宏包装器将破坏语法传递给函数吗?

clojure compojure compojure-api

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