Skip to content

Commit 206897a

Browse files
vobisvobis
authored andcommitted
no dobrze
1 parent a0ec7e0 commit 206897a

3 files changed

Lines changed: 158 additions & 135 deletions

File tree

pom.xml

Lines changed: 76 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,76 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
6-
<groupId>com.example</groupId>
7-
<version>1.0-SNAPSHOT</version>
8-
<artifactId>helloworld</artifactId>
9-
<dependencies>
10-
<dependency>
11-
<groupId>com.sparkjava</groupId>
12-
<artifactId>spark-core</artifactId>
13-
<version>2.2</version>
14-
</dependency>
15-
<dependency>
16-
<groupId>com.sparkjava</groupId>
17-
<artifactId>spark-template-freemarker</artifactId>
18-
<version>2.0.0</version>
19-
</dependency>
20-
<dependency>
21-
<groupId>org.postgresql</groupId>
22-
<artifactId>postgresql</artifactId>
23-
<version>9.4-1201-jdbc4</version>
24-
</dependency>
25-
<dependency>
26-
<groupId>com.heroku.sdk</groupId>
27-
<artifactId>heroku-jdbc</artifactId>
28-
<version>0.1.1</version>
29-
</dependency>
30-
</dependencies>
31-
<build>
32-
<plugins>
33-
<plugin>
34-
<groupId>org.apache.maven.plugins</groupId>
35-
<artifactId>maven-compiler-plugin</artifactId>
36-
<version>2.5.1</version>
37-
<configuration>
38-
<source>1.8</source>
39-
<target>1.8</target>
40-
<optimize>true</optimize>
41-
<debug>true</debug>
42-
</configuration>
43-
</plugin>
44-
<plugin>
45-
<artifactId>maven-assembly-plugin</artifactId>
46-
<version>2.3</version>
47-
<configuration>
48-
<descriptorRefs>
49-
<descriptorRef>jar-with-dependencies</descriptorRef>
50-
</descriptorRefs>
51-
<finalName>helloworld</finalName>
52-
<appendAssemblyId>false</appendAssemblyId>
53-
<archive>
54-
<manifest>
55-
<mainClass>Main</mainClass>
56-
</manifest>
57-
</archive>
58-
</configuration>
59-
<executions>
60-
<execution>
61-
<id>build-jar-with-dependencies</id>
62-
<phase>package</phase>
63-
<goals>
64-
<goal>single</goal>
65-
</goals>
66-
</execution>
67-
</executions>
68-
</plugin>
69-
</plugins>
70-
</build>
71-
</project>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.example</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
<artifactId>helloworld</artifactId>
9+
<dependencies>
10+
<dependency>
11+
<groupId>com.sparkjava</groupId>
12+
<artifactId>spark-core</artifactId>
13+
<version>2.2</version>
14+
</dependency>
15+
<dependency>
16+
<groupId>com.sparkjava</groupId>
17+
<artifactId>spark-template-freemarker</artifactId>
18+
<version>2.0.0</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.postgresql</groupId>
22+
<artifactId>postgresql</artifactId>
23+
<version>9.4-1201-jdbc4</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.heroku.sdk</groupId>
27+
<artifactId>heroku-jdbc</artifactId>
28+
<version>0.1.1</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.jscience</groupId>
32+
<artifactId>jscience</artifactId>
33+
<version>4.3.1</version>
34+
</dependency>
35+
</dependencies>
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<version>2.5.1</version>
42+
<configuration>
43+
<source>1.8</source>
44+
<target>1.8</target>
45+
<optimize>true</optimize>
46+
<debug>true</debug>
47+
</configuration>
48+
</plugin>
49+
<plugin>
50+
<artifactId>maven-assembly-plugin</artifactId>
51+
<version>2.3</version>
52+
<configuration>
53+
<descriptorRefs>
54+
<descriptorRef>jar-with-dependencies</descriptorRef>
55+
</descriptorRefs>
56+
<finalName>helloworld</finalName>
57+
<appendAssemblyId>false</appendAssemblyId>
58+
<archive>
59+
<manifest>
60+
<mainClass>Main</mainClass>
61+
</manifest>
62+
</archive>
63+
</configuration>
64+
<executions>
65+
<execution>
66+
<id>build-jar-with-dependencies</id>
67+
<phase>package</phase>
68+
<goals>
69+
<goal>single</goal>
70+
</goals>
71+
</execution>
72+
</executions>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
</project>

src/main/java/Main.java

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,81 @@
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+
}

src/main/resources/spark/template/freemarker/hello.ftl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
</div>
2828
<hr>
2929

30+
${massMessage}
3031

3132
</div>
3233

0 commit comments

Comments
 (0)