0

lein new compojure mongotestlein 2.0 と Procfileを使用して compojure アプリを作成しましたweb: lein ring server-headless $PORT。これはうまくいきましたが、今追加すると

(def mongolab-url (System/getenv "MONGOLAB_URI"))
(println "mongolab-url")
(println mongolab-url)
(mg/connect-via-uri! mongolab-url)

git push heroku master私がHerokuを試すと、最終的に私に与えます

   Compiling mongotest.handler
   mongolab-url
   nil
   Exception in thread "main" java.lang.NullPointerException, compiling:(handler.clj:13:1)
    ... 25 more
   Compilation failed: Subprocess failed
   Error encountered performing task 'compile' with profile(s): 'production'
   Suppressed exit
!  Failed to build.
!  Push rejected, failed to compile Clojure (Leiningen 2) app

これらの行を の最上位に追加しましたhandler.clj。接続が で作成されていることがわかる古いドキュメントの一部ですが、生成されたアプリには (私が見つけた) もうmain存在しないため、古いバージョンの compojure 用である必要があります。main現在のバージョンではどこで接続を確立する必要がありますか? (または、私のプロファイルが正しくありませんか?)

(はい、MONGOLAB_URIで定義されていheroku configます)

4

2 に答える 2

1

init私にとってうまくいき、最も慣用的な答えは、上記の接続を同じファイルの関数に入れることです( myproject/handler.clj

(defn init[] (mg/connect-via-uri! (System/getenv "MONGOLAB_URI")))

project.cljからファイルリング記述子行を更新します

:ring {:handler myproject.handler/app}

:ring {:handler myproject.handler/app :init myproject.handler/init}
于 2013-09-16T19:12:59.740 に答える
0

You don't want side-effects (such as opening a database connection) to happen at namespace load time, which is effectively compile time. Often you can get away with such things, when you are compiling your code when clojure starts up, but in this case heroku is pre-compiling your app into a jar so that it can serve it up more quickly. Now, at compile time, you try to read the PORT environment variable; but it is not set until heroku tries to actually run your app.

The solution is to create it only at runtime, eg by doing so in -main, or in some other function, or by defining it as a delay which you only force at runtime.

于 2013-09-14T01:29:18.553 に答える