Skip to content

Commit d8ae5e6

Browse files
committed
new ui
1 parent fb0eb9f commit d8ae5e6

File tree

12 files changed

+240
-60
lines changed

12 files changed

+240
-60
lines changed

.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: 56 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,75 @@
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 {
149

15-
if (req.getRequestURI().endsWith("/db")) {
16-
showDatabase(req,resp);
17-
} else {
18-
showHome(req,resp);
19-
}
20-
}
10+
import static spark.Spark.*;
11+
import spark.template.freemarker.FreeMarkerEngine;
12+
import spark.ModelAndView;
13+
import static spark.Spark.get;
2114

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

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

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");
28+
return new ModelAndView(attributes, "index.ftl");
29+
}, new FreeMarkerEngine());
3730

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

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-
}
4959
}
5060

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

60-
return DriverManager.getConnection(dbUrl, username, password);
66+
if (dbUri.getUserInfo() != null) {
67+
String username = dbUri.getUserInfo().split(":")[0];
68+
String password = dbUri.getUserInfo().split(":")[1];
69+
return DriverManager.getConnection(dbUrl, username, password);
70+
} else {
71+
return DriverManager.getConnection(dbUrl);
72+
}
6173
}
6274

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-
}
7275
}
2.24 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+

src/main/resources/public/x.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdf
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" />

0 commit comments

Comments
 (0)