|
1 | | -import java.sql.*; |
2 | | -import java.util.HashMap; |
3 | | -import java.util.ArrayList; |
4 | | -import java.util.Map; |
5 | | - |
6 | | -import java.net.URI; |
7 | | -import java.net.URISyntaxException; |
8 | | - |
9 | | -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 | | - |
16 | | -public class Main { |
17 | | - |
18 | | - public static void main(String[] args) { |
19 | | - |
20 | | - port(Integer.valueOf(System.getenv("PORT"))); |
21 | | - staticFileLocation("/public"); |
22 | | - |
23 | | - get("/hello", (request, response) -> { |
24 | | - Map<String, Object> attributes = new HashMap<>(); |
25 | | - attributes.put("message", "Hello World!"); |
26 | | - |
27 | | - return new ModelAndView(attributes, "hello.ftl"); |
28 | | - }, new FreeMarkerEngine()); |
29 | | - |
30 | | - get("/", (request, response) -> { |
31 | | - Map<String, Object> attributes = new HashMap<>(); |
32 | | - attributes.put("message", "Hello World!"); |
33 | | - |
34 | | - return new ModelAndView(attributes, "index.ftl"); |
35 | | - }, new FreeMarkerEngine()); |
36 | | - get("/db", (req, res) -> { |
37 | | - Connection connection = null; |
38 | | - Map<String, Object> attributes = new HashMap<>(); |
39 | | - try { |
40 | | - connection = DatabaseUrl.extract().getConnection(); |
41 | | - |
42 | | - Statement stmt = connection.createStatement(); |
43 | | - stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)"); |
44 | | - stmt.executeUpdate("INSERT INTO ticks VALUES (now())"); |
45 | | - ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks"); |
46 | | - |
47 | | - ArrayList<String> output = new ArrayList<String>(); |
48 | | - while (rs.next()) { |
49 | | - output.add( "Read from DB: " + rs.getTimestamp("tick")); |
50 | | - } |
51 | | - |
52 | | - attributes.put("results", output); |
53 | | - return new ModelAndView(attributes, "db.ftl"); |
54 | | - } catch (Exception e) { |
55 | | - attributes.put("message", "There was an error: " + e); |
56 | | - return new ModelAndView(attributes, "error.ftl"); |
57 | | - } finally { |
58 | | - if (connection != null) try{connection.close();} catch(SQLException e){} |
59 | | - } |
60 | | - }, new FreeMarkerEngine()); |
61 | | - |
62 | | - } |
63 | | - |
64 | | -} |
| 1 | +import java.sql.*; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import javax.measure.quantity.Energy; |
| 7 | +import javax.measure.quantity.Mass; |
| 8 | +import javax.measure.unit.NonSI; |
| 9 | +import javax.measure.unit.SI; |
| 10 | + |
| 11 | +import org.jscience.physics.amount.Amount; |
| 12 | +import org.jscience.physics.model.RelativisticModel; |
| 13 | + |
| 14 | +import java.net.URI; |
| 15 | +import java.net.URISyntaxException; |
| 16 | + |
| 17 | +import static spark.Spark.*; |
| 18 | +import spark.template.freemarker.FreeMarkerEngine; |
| 19 | +import spark.ModelAndView; |
| 20 | +import static spark.Spark.get; |
| 21 | + |
| 22 | +import com.heroku.sdk.jdbc.DatabaseUrl; |
| 23 | + |
| 24 | +public class Main { |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + |
| 28 | + port(Integer.valueOf(System.getenv("PORT"))); |
| 29 | + staticFileLocation("/public"); |
| 30 | + |
| 31 | + get("/hello", (request, response) -> { |
| 32 | + Map<String, Object> attributes = new HashMap<>(); |
| 33 | + // attributes.put("message", "Hello World!"); |
| 34 | + |
| 35 | + attributes.put("message", "Spark i Freemaker w akcji!"); |
| 36 | + RelativisticModel.select(); |
| 37 | + Amount<Mass> m = Amount.valueOf("12 GeV").to(SI.KILOGRAM); |
| 38 | + String massMessage = "E=mc^2: 12 GeV = " + m.toString(); |
| 39 | + attributes.put("massMessage", massMessage); |
| 40 | + Amount<Energy> e = Amount.valueOf("100 kg").to(SI.GIGA(NonSI.ELECTRON_VOLT)); |
| 41 | + String energyMessage = "E=mc^2: 100 kg = " + e.toString(); |
| 42 | + attributes.put("energyMessage", energyMessage); |
| 43 | + |
| 44 | + return new ModelAndView(attributes, "hello.ftl"); |
| 45 | + }, new FreeMarkerEngine()); |
| 46 | + |
| 47 | + get("/", (request, response) -> { |
| 48 | + Map<String, Object> attributes = new HashMap<>(); |
| 49 | + attributes.put("message", "Hello World!"); |
| 50 | + |
| 51 | + return new ModelAndView(attributes, "index.ftl"); |
| 52 | + }, new FreeMarkerEngine()); |
| 53 | + get("/db", (req, res) -> { |
| 54 | + Connection connection = null; |
| 55 | + Map<String, Object> attributes = new HashMap<>(); |
| 56 | + try { |
| 57 | + connection = DatabaseUrl.extract().getConnection(); |
| 58 | + |
| 59 | + Statement stmt = connection.createStatement(); |
| 60 | + stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)"); |
| 61 | + stmt.executeUpdate("INSERT INTO ticks VALUES (now())"); |
| 62 | + ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks"); |
| 63 | + |
| 64 | + ArrayList<String> output = new ArrayList<String>(); |
| 65 | + while (rs.next()) { |
| 66 | + output.add( "Read from DB: " + rs.getTimestamp("tick")); |
| 67 | + } |
| 68 | + |
| 69 | + attributes.put("results", output); |
| 70 | + return new ModelAndView(attributes, "db.ftl"); |
| 71 | + } catch (Exception e) { |
| 72 | + attributes.put("message", "There was an error: " + e); |
| 73 | + return new ModelAndView(attributes, "error.ftl"); |
| 74 | + } finally { |
| 75 | + if (connection != null) try{connection.close();} catch(SQLException e){} |
| 76 | + } |
| 77 | + }, new FreeMarkerEngine()); |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments