Skip to content

Commit 4fc1593

Browse files
author
merhoo
committed
backend is now on heroku postgres
1 parent f251eb7 commit 4fc1593

File tree

19 files changed

+477
-16
lines changed

19 files changed

+477
-16
lines changed
File renamed without changes.

backend/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
<artifactId>gson</artifactId>
2525
<version>2.8.1</version>
2626
</dependency>
27+
<dependency>
28+
<groupId>com.heroku.sdk</groupId>
29+
<artifactId>heroku-maven-plugin</artifactId>
30+
<version>2.0.2</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>postgresql</groupId>
34+
<artifactId>postgresql</artifactId>
35+
<version>9.1-901-1.jdbc4</version>
36+
</dependency>
2737
</dependencies>
2838
<build>
2939
<plugins>
@@ -44,6 +54,39 @@
4454
<mainClass>merhoo.backend.App</mainClass>
4555
</configuration>
4656
</plugin>
57+
<plugin>
58+
<groupId>com.heroku.sdk</groupId>
59+
<artifactId>heroku-maven-plugin</artifactId>
60+
<version>2.0.2</version>
61+
<configuration>
62+
<jdkVersion>1.8</jdkVersion>
63+
<appName>merhoo</appName>
64+
<processTypes>
65+
<web>java -jar ./target/backend-1.0-SNAPSHOT-jar-with-dependencies.jar</web>
66+
</processTypes>
67+
</configuration>
68+
</plugin>
69+
<plugin>
70+
<artifactId>maven-assembly-plugin</artifactId>
71+
<executions>
72+
<execution>
73+
<phase>package</phase>
74+
<goals>
75+
<goal>single</goal>
76+
</goals>
77+
</execution>
78+
</executions>
79+
<configuration>
80+
<descriptorRefs>
81+
<descriptorRef>jar-with-dependencies</descriptorRef>
82+
</descriptorRefs>
83+
<archive>
84+
<manifest>
85+
<mainClass>merhoo.backend.App</mainClass>
86+
</manifest>
87+
</archive>
88+
</configuration>
89+
</plugin>
4790
</plugins>
4891
</build>
4992
</project>

backend/src/main/java/merhoo/backend/App.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111
* For now, our app creates an HTTP server that can only get and add data.
1212
*/
1313
public class App {
14+
15+
/**
16+
* Get an integer environment varible if it exists, and otherwise return the
17+
* default value.
18+
*
19+
* @envar The name of the environment variable to get.
20+
* @defaultVal The integer value to use as the default if envar isn't found
21+
*
22+
* @returns The best answer we could come up with for a value for envar
23+
*/
24+
static int getIntFromEnv(String envar, int defaultVal) {
25+
ProcessBuilder processBuilder = new ProcessBuilder();
26+
if (processBuilder.environment().get(envar) != null) {
27+
return Integer.parseInt(processBuilder.environment().get(envar));
28+
}
29+
return defaultVal;
30+
}
31+
1432
public static void main(String[] args) {
1533

1634
// gson provides us with a way to turn JSON into objects, and objects
@@ -21,14 +39,13 @@ public static void main(String[] args) {
2139
// NB: Gson is thread-safe. See
2240
// https://stackoverflow.com/questions/10380835/is-it-ok-to-use-gson-instance-as-a-static-field-in-a-model-bean-reuse
2341
final Gson gson = new Gson();
42+
final Database db = Database.getDatabase();
43+
if (db == null) {
44+
return;
45+
}
2446

25-
// dataStore holds all of the data that has been provided via HTTP
26-
// requests
27-
//
28-
// NB: every time we shut down the server, we will lose all data, and
29-
// every time we start the server, we'll have an empty dataStore,
30-
// with IDs starting over from 0.
31-
final DataStore dataStore = new DataStore();
47+
// Get the port on which to listen for requests
48+
Spark.port(getIntFromEnv("PORT", 4567));
3249

3350
// Set up the location for serving static files
3451
Spark.staticFileLocation("/web");
@@ -47,10 +64,10 @@ public static void main(String[] args) {
4764
// ensure status 200 OK, with a MIME type of JSON
4865
response.status(200);
4966
response.type("application/json");
50-
return gson.toJson(new StructuredResponse("ok", null, dataStore.readAll()));
67+
return gson.toJson(new StructuredResponse("ok", null, db.readAll()));
5168
});
5269

53-
// GET route that returns everything for a single row in the DataStore.
70+
// GET route that returns everything for a single row in the db.
5471
// The ":id" suffix in the first parameter to get() becomes
5572
// request.params("id"), so that we can get the requested row ID. If
5673
// ":id" isn't a number, Spark will reply with a status 500 Internal
@@ -61,15 +78,15 @@ public static void main(String[] args) {
6178
// ensure status 200 OK, with a MIME type of JSON
6279
response.status(200);
6380
response.type("application/json");
64-
DataRow data = dataStore.readOne(idx);
81+
DataRow data = db.readOne(idx);
6582
if (data == null) {
6683
return gson.toJson(new StructuredResponse("error", idx + " not found", null));
6784
} else {
6885
return gson.toJson(new StructuredResponse("ok", null, data));
6986
}
7087
});
7188

72-
// POST route for adding a new element to the DataStore. This will read
89+
// POST route for adding a new element to the db. This will read
7390
// JSON from the body of the request, turn it into a SimpleRequest
7491
// object, extract the title and message, insert them, and return the
7592
// ID of the newly created row.
@@ -83,15 +100,15 @@ public static void main(String[] args) {
83100
response.status(200);
84101
response.type("application/json");
85102
// NB: createEntry checks for null title and message
86-
int newId = dataStore.createEntry(req.mTitle, req.mMessage);
103+
int newId = db.createEntry(req.mTitle, req.mMessage);
87104
if (newId == -1) {
88105
return gson.toJson(new StructuredResponse("error", "error performing insertion", null));
89106
} else {
90107
return gson.toJson(new StructuredResponse("ok", "" + newId, null));
91108
}
92109
});
93110

94-
// PUT route for updating a row in the DataStore. This is almost
111+
// PUT route for updating a row in the db. This is almost
95112
// exactly the same as POST
96113
Spark.put("/messages/:id", (request, response) -> {
97114
// If we can't get an ID or can't parse the JSON, Spark will send
@@ -101,15 +118,15 @@ public static void main(String[] args) {
101118
// ensure status 200 OK, with a MIME type of JSON
102119
response.status(200);
103120
response.type("application/json");
104-
DataRow result = dataStore.updateOne(idx, req.mTitle, req.mMessage);
121+
DataRow result = db.updateOne(idx, req.mTitle, req.mMessage);
105122
if (result == null) {
106123
return gson.toJson(new StructuredResponse("error", "unable to update row " + idx, null));
107124
} else {
108125
return gson.toJson(new StructuredResponse("ok", null, result));
109126
}
110127
});
111128

112-
// DELETE route for removing a row from the DataStore
129+
// DELETE route for removing a row from the db
113130
Spark.delete("/messages/:id", (request, response) -> {
114131
// If we can't get an ID, Spark will send a status 500
115132
int idx = Integer.parseInt(request.params("id"));
@@ -118,7 +135,7 @@ public static void main(String[] args) {
118135
response.type("application/json");
119136
// NB: we won't concern ourselves too much with the quality of the
120137
// message sent on a successful delete
121-
boolean result = dataStore.deleteOne(idx);
138+
boolean result = db.deleteOne(idx);
122139
if (!result) {
123140
return gson.toJson(new StructuredResponse("error", "unable to delete row " + idx, null));
124141
} else {

0 commit comments

Comments
 (0)