1111 * For now, our app creates an HTTP server that can only get and add data.
1212 */
1313public 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