|
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 | + |
6 | 6 | import java.net.URI; |
7 | 7 | import java.net.URISyntaxException; |
8 | | -import java.sql.*; |
9 | 8 |
|
10 | | -public class Main extends HttpServlet { |
11 | | - @Override |
12 | | - protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
13 | | - throws ServletException, IOException { |
14 | 9 |
|
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; |
21 | 14 |
|
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"); |
26 | 23 |
|
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!"); |
32 | 27 |
|
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()); |
37 | 30 |
|
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){} |
41 | 56 | } |
| 57 | + }, new FreeMarkerEngine()); |
42 | 58 |
|
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 | | - } |
49 | 59 | } |
50 | 60 |
|
51 | | - private Connection getConnection() throws URISyntaxException, SQLException { |
| 61 | + private static Connection getConnection() throws URISyntaxException, SQLException { |
52 | 62 | URI dbUri = new URI(System.getenv("DATABASE_URL")); |
53 | | - |
54 | | - String username = dbUri.getUserInfo().split(":")[0]; |
55 | | - String password = dbUri.getUserInfo().split(":")[1]; |
56 | 63 | int port = dbUri.getPort(); |
57 | | - |
58 | 64 | String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ":" + port + dbUri.getPath(); |
59 | 65 |
|
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 | + } |
61 | 73 | } |
62 | 74 |
|
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 | | - } |
72 | 75 | } |
0 commit comments