Clojure Ring:如何在字符串旁边打印变量

时间:2013-10-28 19:45:32

标签: clojure

我正在尝试将名为POWERED_BY的环境变量设置为变量message。 然后我想测试message是空还是NULL。然后打印“Powered by”message

目前,以下代码无效。

(ns helloworld.web 
    (:use compojure.core [ring.adapter.jetty :only [run-jetty]] )
    (:require [compojure.route :as route]
              [compojure.handler :as handler]))


(defroutes main-routes
  ; what's going on
    (def message (System/getenv "POWERED_BY"))
    (GET "/" [] (apply str "Powered by " message))
    (route/resources "/")
    (route/not-found "Page not found")   )


(def app
    (handler/api main-routes))

(defn -main [port]
    (run-jetty app {:port (Integer. port)}))

1 个答案:

答案 0 :(得分:1)

定义message外部路线定义:

 (def message (System/getenv "POWERED_BY"))

 (defroutes main-routes
  ; what's going on
 (GET "/" [] (str "Powered by " message)
 (route/resources "/")
 (route/not-found "Page not found"))

如果您想在每次收到请求时检索系统环境变量值,可以使用let表单:

 (defroutes main-routes
  ; what's going on
 (GET "/" [] (let [message (System/getenv "POWERED_BY")]
                (str "Powered by " message))
 (route/resources "/")
 (route/not-found "Page not found"))

对于 concat ,只需使用(str arg1 arg2 ...)apply适用于列表,因此如果您想使用它,则应该执行类似(apply str ["Powered by" message])的操作。