|
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 | + |
2 | 7 | import java.util.HashMap; |
3 | | -import java.util.ArrayList; |
4 | 8 | import java.util.Map; |
5 | 9 |
|
6 | | -import java.net.URI; |
7 | | -import java.net.URISyntaxException; |
8 | | - |
9 | 10 | 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; |
15 | 11 |
|
16 | 12 | public class Main { |
17 | 13 |
|
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"); |
22 | 26 |
|
23 | | - get("/hello", (req, res) -> "Hello World"); |
| 27 | + Route route = (req, res) -> "Hello World"; |
| 28 | + get("/hello", route); |
24 | 29 |
|
25 | | - get("/", (request, response) -> { |
| 30 | + TemplateViewRoute message = (request, response) -> { |
26 | 31 | Map<String, Object> attributes = new HashMap<>(); |
27 | 32 | attributes.put("message", "Hello World!"); |
28 | 33 |
|
29 | 34 | 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 | + } |
60 | 38 | } |
0 commit comments