Skip to content

Commit 35698e1

Browse files
committed
update code
1 parent 5687dfc commit 35698e1

2 files changed

Lines changed: 42 additions & 46 deletions

File tree

src/main/java/GenericRoute.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import spark.*;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Created by stephen on 12/27/2016.
8+
*/
9+
public class GenericRoute implements TemplateViewRoute {
10+
11+
@Override
12+
public ModelAndView handle(Request request, Response response) {
13+
14+
Map<String, Object> attributes = new HashMap<>();
15+
attributes.put("message", "Hello World!");
16+
return new ModelAndView(attributes, "index.ftl");
17+
}
18+
}

src/main/java/Main.java

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,38 @@
1-
import java.sql.*;
1+
import spark.ModelAndView;
2+
import spark.Route;
3+
import spark.TemplateViewRoute;
4+
import spark.template.freemarker.FreeMarkerEngine;
5+
import spark.utils.StringUtils;
6+
27
import java.util.HashMap;
3-
import java.util.ArrayList;
48
import java.util.Map;
59

6-
import java.net.URI;
7-
import java.net.URISyntaxException;
8-
910
import static spark.Spark.*;
10-
import spark.template.freemarker.FreeMarkerEngine;
11-
import spark.ModelAndView;
12-
import static spark.Spark.get;
13-
14-
import com.heroku.sdk.jdbc.DatabaseUrl;
1511

1612
public class Main {
1713

18-
public static void main(String[] args) {
19-
20-
port(Integer.valueOf(System.getenv("PORT")));
21-
staticFileLocation("/public");
14+
public static void main(String[] args) {
15+
Integer port = 8080;
16+
try {
17+
String portString = System.getenv("PORT");
18+
if (StringUtils.isNotEmpty(portString)) {
19+
port = Integer.valueOf(portString);
20+
}
21+
} catch (NumberFormatException e) {
22+
System.out.println("Not a Number");
23+
}
24+
port(port);
25+
staticFileLocation("/public");
2226

23-
get("/hello", (req, res) -> "Hello World");
27+
Route route = (req, res) -> "Hello World";
28+
get("/hello", route);
2429

25-
get("/", (request, response) -> {
30+
TemplateViewRoute message = (request, response) -> {
2631
Map<String, Object> attributes = new HashMap<>();
2732
attributes.put("message", "Hello World!");
2833

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

0 commit comments

Comments
 (0)