English 中文(简体)
如何在 Heroku 中取得当前主机名
原标题:How to get the current hostname in Heroku

我在赫鲁库玩弄Clojure/Noir Apps,我得到了我的应用程序大部分有效。然而,我最后需要的就是在赫鲁库找到我的应用程序的主机名。 理想的情况是,我想动态地这样做,而不是硬码地操作。

因此,比如说,如果我的应用程序的 URL 是"http://freez-windy-1800.herokooapp.com" rel="noreferrer" >http://freez-windy-1800.herokoapp.com , 我想能在我的阴道代码中动态地得到这个。

我知道,我可以看收到的请求来解决这个问题,但最理想的是,我希望能有某种设置,使我对一个表达式作一次评价,然后将值保存在一个变量中,然后我就可以使用(来自Python/Django世界,我想到了克洛朱尔的sets.py 等同的“Clojure”)。

用于参考的代码Im部署可在查询。

最佳回答

您可以在 Heroku 设置一个环境变量

heroku config:add BASE_IRI=http://freez-windy-1800.herokuapp.com

然后在克洛朱尔读回来

(defn- base-iri []
  (or (System/getenv "BASE_IRI") "http://localhost/"))

Heroko 已经设置了您可以使用的端口

(defn -main []
  (let [port (Integer. (or (System/getenv "PORT") 8080))]
    (run-jetty # app {:port port})))

在不同的环境下为我工作

问题回答

您通常会使用 Java 标准图书馆 < a href=" http://docs. oracle.com/javase/7/docs/api/java/net/ Inet/ InetAddress.html" 的 < a href=" < a href=" < http://docs.oracle.com/javase/7/docs/api/java/net/ InetAddress.html" " rel="nofollow"\\code> InetAddress 。

(.getCanonicalHostName (java.net.InetAddress/getLocalHost))

然而,这并没有进行DNS调查。

获取主机名的 3 种方法。 随你如何使用 。

(ns service.get-host-name
  (require [clojure.java.shell :as shell]
           [clojure.string :as str])
  (:import [java.net InetAddress]))

(defn- hostname []
  (try
    (-> (shell/sh "hostname") (:out) (str/trim))
    (catch Exception _e
      (try
        (str/trim (slurp "/etc/hostname"))
        (catch Exception _e
          (try
            (.getHostName (InetAddress/getLocalHost))
            (catch Exception _e
              nil)))))))




相关问题
How to improve Clojures error messages

I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...

clojure rmi classpath problem

I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...

Help translating this Java codeblock to Clojure?

I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

how to efficiently apply a medium-weight function in parallel

I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...

热门标签