How to obtain databse url etc without the heroku CLI

自古美人都是妖i 提交于 2019-12-25 09:17:09

问题


I am using Heroku PostgreSQL in my Java application and obtain the database url, username, database name, and password like so

Properties props = new Properties();
props.setProperty("sslmode","require");
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("heroku config:get DATABASE_URL -a myMegaCoolApplication");
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
Pattern p = Pattern.compile("(postgres)://([^:]+):([^@]+)@(.*)");
Matcher m = p.matcher(br.readLine());
m.find();
props.setProperty("user",m.group(2));
props.setProperty("password",m.group(3));
conn = DriverManager.getConnection("jdbc:"+m.group(1)+"ql://"+m.group(4),props);

Here one needs to have the heroku tool installed on a local machine. Can I do without it? The goal is to be able to ship the Java jar-file alone, without the requiring the user to install heroku.


回答1:


You can make an HTTP request to the Heroku Platform API to retrieve config vars for an app.

A convenient way to do this is with the heroku.jar client library. For example

HerokuAPI api = new HerokuAPI(apiKey);
Map<String, String> config = api.listConfig("myExistingApp");
for (Map.Entry<String, String> var : config.entrySet()) {
    System.out.println(var.getKey() + "=" + var.getValue());
}


来源:https://stackoverflow.com/questions/41624335/how-to-obtain-databse-url-etc-without-the-heroku-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!