Heroku
Heroku [1] (pronounced her-OH-koo) is a cloud application platform – a new way of building and deploying web apps. The service lets app developers spend their time on their application code, not managing servers, deployment, ongoing operations, or scaling.
Heroku was founded in 2007 by Orion Henry, James Lindenbaum, and Adam Wiggins.
Foreman
Foreman is a manager for Procfile-based applications. Its aim is to abstract away the details of the Procfile format, and allow you to either run your application directly or export it to some other process management format.
Procfile application
A Procfile application is an application that has a Procfile with instructions to start various processes it needs to run correctly.
Here’s how a Procfile might look like:
rails: bundle exec rails s postgres: postgres -D /Users/mauricio/databases/postgresql elasticsearch: elasticsearch -f
Environment Files
Foreman automatically loads the .env file that is at the same directory as your Procfile.
DTAP Loop
The steps to execute the DTAP development cycle are:
Test local
<syntaxhighlight lang="bash" line start="1"> $ mvn clean install $ foreman start web </syntaxhighlight> Now visit the application at http://localhost:5000.
Deploy remote
<syntaxhighlight lang="bash" line start="1">
- Add the changes to the repository
$ git add .
- Commit the changes
$ git commit -m "Reason..."
- Deploy the changes to the repo
$ git push heroku master
- Check if everythings works
$ heroku open </syntaxhighlight>
Database
Heroku offers a free Postgres 9.3. database. The code to access the database is straightforward. See the getConnection() method that retrieves the environment variable DATABASE_URL set by the database add-on, and establishes a connection: <syntaxhighlight lang="java" line start="1">
private Connection getConnection() throws URISyntaxException, SQLException { URI dbUri = new URI(System.getenv("DATABASE_URL"));
String username = dbUri.getUserInfo().split(":")[0]; String password = dbUri.getUserInfo().split(":")[1]; int port = dbUri.getPort();
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ":" + port + dbUri.getPath();
return DriverManager.getConnection(dbUrl, username, password); }
</syntaxhighlight>
To insert values into a table named ticks: <syntaxhighlight lang="java" line start="1"> private void showDatabase(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { Connection connection = null; try { connection = getConnection();
Statement stmt = connection.createStatement(); stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)"); stmt.executeUpdate("INSERT INTO ticks VALUES (now())"); ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
String out = "Hello!\n"; while (rs.next()) { out += "Read from DB: " + rs.getTimestamp("tick") + "\n"; }
resp.getWriter().print(out); } catch (Exception e) { resp.getWriter().print("There was an error: " + e.getMessage()); } finally { if (connection != null) try{connection.close();} catch(SQLException e){} } }
</syntaxhighlight>
See also
- Heroku Devcenter, Getting started Tutorial. Shows how to create, deploy on Local-Development & Remote-Production a java app with maven.
Reference
- ↑ Herohu home, About Heroku