Skip to content

Commit b9a1f78

Browse files
committed
new ui
1 parent fb0eb9f commit b9a1f78

11 files changed

Lines changed: 236 additions & 60 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
target
2+
.env

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ $ foreman start web
1717

1818
Your app should now be running on [localhost:5000](http://localhost:5000/).
1919

20+
If you're going to use a database, ensure you have a local `.env` file that reads something like this:
21+
22+
```
23+
DATABASE_URL=postgres://localhost:5432/java_database_name
24+
```
25+
2026
## Deploying to Heroku
2127

2228
```sh
@@ -30,4 +36,3 @@ $ heroku open
3036
For more information about using Java on Heroku, see these Dev Center articles:
3137

3238
- [Java on Heroku](https://devcenter.heroku.com/categories/java)
33-

pom.xml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
<artifactId>helloworld</artifactId>
99
<dependencies>
1010
<dependency>
11-
<groupId>org.eclipse.jetty</groupId>
12-
<artifactId>jetty-servlet</artifactId>
13-
<version>9.2.10.v20150310</version>
11+
<groupId>com.sparkjava</groupId>
12+
<artifactId>spark-core</artifactId>
13+
<version>2.2</version>
1414
</dependency>
1515
<dependency>
16-
<groupId>javax.servlet</groupId>
17-
<artifactId>javax.servlet-api</artifactId>
18-
<version>3.1.0</version>
16+
<groupId>com.sparkjava</groupId>
17+
<artifactId>spark-template-freemarker</artifactId>
18+
<version>2.0.0</version>
1919
</dependency>
2020
<dependency>
2121
<groupId>org.postgresql</groupId>
@@ -25,6 +25,17 @@
2525
</dependencies>
2626
<build>
2727
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<version>2.5.1</version>
32+
<configuration>
33+
<source>1.8</source>
34+
<target>1.8</target>
35+
<optimize>true</optimize>
36+
<debug>true</debug>
37+
</configuration>
38+
</plugin>
2839
<plugin>
2940
<groupId>org.apache.maven.plugins</groupId>
3041
<artifactId>maven-dependency-plugin</artifactId>

src/main/java/Main.java

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
import java.io.IOException;
2-
import javax.servlet.ServletException;
3-
import javax.servlet.http.*;
4-
import org.eclipse.jetty.server.Server;
5-
import org.eclipse.jetty.servlet.*;
1+
import java.sql.*;
2+
import java.util.HashMap;
3+
import java.util.ArrayList;
4+
import java.util.Map;
5+
66
import java.net.URI;
77
import java.net.URISyntaxException;
8-
import java.sql.*;
98

10-
public class Main extends HttpServlet {
11-
@Override
12-
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
13-
throws ServletException, IOException {
9+
import static spark.Spark.*;
10+
import spark.template.freemarker.FreeMarkerEngine;
11+
import spark.ModelAndView;
12+
import static spark.Spark.get;
1413

15-
if (req.getRequestURI().endsWith("/db")) {
16-
showDatabase(req,resp);
17-
} else {
18-
showHome(req,resp);
19-
}
20-
}
14+
public class Main {
2115

22-
private void showHome(HttpServletRequest req, HttpServletResponse resp)
23-
throws ServletException, IOException {
24-
resp.getWriter().print("Hello from Java!");
25-
}
16+
public static void main(String[] args) {
17+
18+
port(Integer.valueOf(System.getenv("PORT")));
19+
staticFileLocation("/public");
20+
21+
get("/hello", (req, res) -> "Hello World");
2622

27-
private void showDatabase(HttpServletRequest req, HttpServletResponse resp)
28-
throws ServletException, IOException {
29-
Connection connection = null;
30-
try {
31-
connection = getConnection();
23+
get("/", (request, response) -> {
24+
Map<String, Object> attributes = new HashMap<>();
25+
attributes.put("message", "Hello World!");
3226

33-
Statement stmt = connection.createStatement();
34-
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
35-
stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
36-
ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
27+
return new ModelAndView(attributes, "index.ftl");
28+
}, new FreeMarkerEngine());
3729

38-
String out = "Hello!\n";
39-
while (rs.next()) {
40-
out += "Read from DB: " + rs.getTimestamp("tick") + "\n";
30+
get("/db", (req, res) -> {
31+
Connection connection = null;
32+
Map<String, Object> attributes = new HashMap<>();
33+
try {
34+
connection = getConnection();
35+
36+
Statement stmt = connection.createStatement();
37+
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
38+
stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
39+
ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
40+
41+
ArrayList<String> output = new ArrayList<String>();
42+
while (rs.next()) {
43+
output.add( "Read from DB: " + rs.getTimestamp("tick"));
44+
}
45+
46+
attributes.put("results", output);
47+
return new ModelAndView(attributes, "db.ftl");
48+
} catch (Exception e) {
49+
attributes.put("message", "There was an error: " + e);
50+
return new ModelAndView(attributes, "error.ftl");
51+
} finally {
52+
if (connection != null) try{connection.close();} catch(SQLException e){}
4153
}
54+
}, new FreeMarkerEngine());
4255

43-
resp.getWriter().print(out);
44-
} catch (Exception e) {
45-
resp.getWriter().print("There was an error: " + e.getMessage());
46-
} finally {
47-
if (connection != null) try{connection.close();} catch(SQLException e){}
48-
}
4956
}
5057

51-
private Connection getConnection() throws URISyntaxException, SQLException {
58+
private static Connection getConnection() throws URISyntaxException, SQLException {
5259
URI dbUri = new URI(System.getenv("DATABASE_URL"));
53-
54-
String username = dbUri.getUserInfo().split(":")[0];
55-
String password = dbUri.getUserInfo().split(":")[1];
5660
int port = dbUri.getPort();
57-
5861
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ":" + port + dbUri.getPath();
5962

60-
return DriverManager.getConnection(dbUrl, username, password);
63+
if (dbUri.getUserInfo() != null) {
64+
String username = dbUri.getUserInfo().split(":")[0];
65+
String password = dbUri.getUserInfo().split(":")[1];
66+
return DriverManager.getConnection(dbUrl, username, password);
67+
} else {
68+
return DriverManager.getConnection(dbUrl);
69+
}
6170
}
6271

63-
public static void main(String[] args) throws Exception {
64-
Server server = new Server(Integer.valueOf(System.getenv("PORT")));
65-
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
66-
context.setContextPath("/");
67-
server.setHandler(context);
68-
context.addServlet(new ServletHolder(new Main()),"/*");
69-
server.start();
70-
server.join();
71-
}
7272
}
2.66 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.jumbotron {
2+
background: #532F8C;
3+
color: white;
4+
padding-bottom: 80px; }
5+
.jumbotron .btn-primary {
6+
background: #845ac7;
7+
border-color: #845ac7; }
8+
.jumbotron .btn-primary:hover {
9+
background: #7646c1; }
10+
.jumbotron p {
11+
color: #d9ccee;
12+
max-width: 75%;
13+
margin: 1em auto 2em; }
14+
.navbar + .jumbotron {
15+
margin-top: -20px; }
16+
.jumbotron .lang-logo {
17+
display: block;
18+
background: #B01302;
19+
border-radius: 50%;
20+
overflow: hidden;
21+
width: 100px;
22+
height: 100px;
23+
margin: auto;
24+
border: 2px solid white; }
25+
.jumbotron .lang-logo img {
26+
max-width: 100%; }
27+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<#include "header.ftl">
5+
</head>
6+
7+
<body>
8+
9+
<#include "nav.ftl">
10+
11+
<div class="container">
12+
13+
<h1>Database Output</h1>
14+
<ul>
15+
<#list results as x>
16+
<li> ${x} </li>
17+
</#list>
18+
</ul>
19+
20+
</div>
21+
22+
</body>
23+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<#include "header.ftl">
5+
</head>
6+
7+
<body>
8+
9+
<#include "nav.ftl">
10+
11+
<div class="container">
12+
${message}
13+
</div>
14+
15+
</body>
16+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<title>Getting Started</title>
2+
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
3+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
4+
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
5+
<link rel="stylesheet" type="text/css" href="/stylesheets/main.css" />
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<#include "header.ftl">
5+
</head>
6+
7+
<body>
8+
9+
<#include "nav.ftl">
10+
11+
<div class="jumbotron text-center">
12+
<div class="container">
13+
<a href="/" class="lang-logo">
14+
<img src="/lang-logo.png">
15+
</a>
16+
<h1>Getting Started with Java on Heroku</h1>
17+
<p>This is a sample Java application deployed to Heroku. It's a reasonably simple app - but a good foundation for understanding how to get the most out of the Heroku platform.</p>
18+
<a type="button" class="btn btn-lg btn-default" href="https://devcenter.heroku.com/articles/getting-started-with-java"><span class="glyphicon glyphicon-flash"></span> Getting Started with Java</a>
19+
<a type="button" class="btn btn-lg btn-primary" href="https://github.com/heroku/java-getting-started"><span class="glyphicon glyphicon-download"></span> Source on GitHub</a>
20+
</div>
21+
</div>
22+
<div class="container">
23+
<div class="alert alert-info text-center" role="alert">
24+
To deploy your own copy, and learn the fundamentals of the Heroku platform, head over to the <a href="https://devcenter.heroku.com/articles/getting-started-with-java" class="alert-link">Getting Started with Java on Heroku</a> tutorial.
25+
</div>
26+
<hr>
27+
<div class="row">
28+
<div class="col-md-6">
29+
<h3><span class="glyphicon glyphicon-info-sign"></span> How this sample app works</h3>
30+
<ul>
31+
<li>This app was deployed to Heroku, either using Git or by using <a href="https://github.com/heroku/java-getting-started">Heroku Button</a> on the repository.</li>
32+
33+
<li>When Heroku received the source code, it grabbed all the dependencies in the <a href="https://github.com/heroku/java-getting-started/blob/master/pom.xml">pom.xml</a>.</li>
34+
<li>The platform then spins up a dyno, a lightweight container that provides an isolated environment in which the slug can be mounted and executed.</li>
35+
<li>You can scale your app, manage it, and deploy over <a href="https://addons.heroku.com/">150 add-on services</a>, from the Dashboard or CLI.</li>
36+
<li>Check out the <a href="https://devcenter.heroku.com/articles/getting-started-with-java">Getting Started</a> guide to learn more!</li>
37+
</ul>
38+
</div>
39+
<div class="col-md-6">
40+
<h3><span class="glyphicon glyphicon-link"></span> Helpful Links</h3>
41+
<ul>
42+
<li><a href="https://www.heroku.com/home">Heroku</a></li>
43+
<li><a href="https://devcenter.heroku.com/">Heroku Dev Center</a></li>
44+
<li><a href="https://devcenter.heroku.com/articles/getting-started-with-java">Getting Started with Java on Heroku</a></li>
45+
<li><a href="https://devcenter.heroku.com/articles/deploying-java">Deploying Java Apps on Heroku</a></li>
46+
</ul>
47+
</div>
48+
</div> <!-- row -->
49+
<div class="alert alert-info text-center" role="alert">
50+
Please do work through the Getting Started guide, even if you do know how to build such an application. The guide covers the basics of working with Heroku, and will familiarize you with all the concepts you need in order to build and deploy your own apps.
51+
</div>
52+
</div>
53+
54+
55+
</body>
56+
</html>

0 commit comments

Comments
 (0)