Permalink
Browse files

Add /api prefix to API URLs

Prefix the API URLs with "/api", so we can use the server to serve
static web content, too.

Make sure we don't create multiple connections to the Postgresql geodata
server.
  • Loading branch information...
1 parent f140809 commit 42293373f3e1af485933e178be0bd61fbdf1e142 @prashtx prashtx committed Aug 9, 2012
Showing with 86 additions and 85 deletions.
  1. +9 −9 collectors.js
  2. +12 −12 forms.js
  3. +11 −10 parcels.js
  4. +26 −26 responses.js
  5. +19 −19 scans.js
  6. +8 −8 surveys.js
  7. +1 −1 web.js
View
@@ -23,9 +23,9 @@ function setup(app, db, idgen, collectionName) {
}
// Get a collector
- // GET http://localhost:3000/surveys/{SURVEY ID}/collectors/{COLLECTOR ID}
- // GET http://localhost:3000/surveys/1/collectors/2ec140e0-827f-11e1-83d8-bf682a6ee038
- app.get('/surveys/:sid/collectors/:cid', function(req, response) {
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/collectors/{COLLECTOR ID}
+ // GET http://localhost:3000/api/surveys/1/collectors/2ec140e0-827f-11e1-83d8-bf682a6ee038
+ app.get('/api/surveys/:sid/collectors/:cid', function(req, response) {
var surveyid = req.params.sid;
var cid = req.params.cid;
getCollection(function(err, collection) {
@@ -49,9 +49,9 @@ function setup(app, db, idgen, collectionName) {
});
// Add collectors for a survey.
- // POST http://localhost:3000/surveys/{SURVEY ID}/collectors
- // POST http://localhost:3000/surveys/1/collectors
- app.post('/surveys/:sid/collectors', function(req, response) {
+ // POST http://localhost:3000/api/surveys/{SURVEY ID}/collectors
+ // POST http://localhost:3000/api/surveys/1/collectors
+ app.post('/api/surveys/:sid/collectors', function(req, response) {
var colls = req.body.collectors;
var total = colls.length;
console.log('Adding ' + total + ' collectors to the database.');
@@ -76,9 +76,9 @@ function setup(app, db, idgen, collectionName) {
});
// Update a collector
- // PUT http://localhost:3000/surveys/{SURVEY ID}/collectors/{COLLECTOR ID}
- // PUT http://localhost:3000/surveys/1/collectors/2ec140e0-827f-11e1-83d8-bf682a6ee038
- app.put('/surveys/:sid/collectors/:cid', function(req, response) {
+ // PUT http://localhost:3000/api/surveys/{SURVEY ID}/collectors/{COLLECTOR ID}
+ // PUT http://localhost:3000/api/surveys/1/collectors/2ec140e0-827f-11e1-83d8-bf682a6ee038
+ app.put('/api/surveys/:sid/collectors/:cid', function(req, response) {
var coll = req.body.collector;
console.log('Updating a collector');
getCollection(function(err, collection) {
View
@@ -56,8 +56,8 @@ function setup(app, db, idgen, FORMS) {
return db.collection(FORMS, cb);
}
-// GET http://localhost:3000/surveys/1/forms/2ec140e0-827f-11e1-83d8-bf682a6ee038
-app.get('/surveys/:surveyid/forms/:formid', function(req, response) {
+// GET http://localhost:3000/api/surveys/1/forms/2ec140e0-827f-11e1-83d8-bf682a6ee038
+app.get('/api/surveys/:surveyid/forms/:formid', function(req, response) {
var surveyid = req.params.surveyid;
var formid = req.params.formid;
console.log('Getting form ' + formid + ' of survey ' + surveyid);
@@ -85,8 +85,8 @@ app.get('/surveys/:surveyid/forms/:formid', function(req, response) {
});
// Get all the forms for a survey.
-// GET http://localhost:3000/surveys/{SURVEY ID}/forms
-app.get('/surveys/:sid/forms', function(req, response) {
+// GET http://localhost:3000/api/surveys/{SURVEY ID}/forms
+app.get('/api/surveys/:sid/forms', function(req, response) {
console.log('Returning all forms for survey ' + req.params.sid);
db.collection(FORMS, function(err, collection) {
collection.find({'survey': req.params.sid}, function(err, cursor) {
@@ -110,9 +110,9 @@ app.get('/surveys/:sid/forms', function(req, response) {
/*
* Add forms to a survey.
* This is done before a survey begins, as a setup task.
- * POST http://localhost:3000/surveys/{SURVEY ID}/forms
+ * POST http://localhost:3000/api/surveys/{SURVEY ID}/forms
*/
-app.post('/surveys/:sid/forms', function(req, response) {
+app.post('/api/surveys/:sid/forms', function(req, response) {
var forms = req.body.forms;
var total = forms.length;
console.log('Adding ' + total + ' forms to the database.');
@@ -139,9 +139,9 @@ app.post('/surveys/:sid/forms', function(req, response) {
/*
* Delete all forms from a survey.
* This is maintainence functionality. Regular clients should not delete forms.
- * POST http://localhost:3000/surveys/{SURVEY ID}/forms
+ * POST http://localhost:3000/api/surveys/{SURVEY ID}/forms
*/
-app.del('/surveys/:sid/forms', function(req, response) {
+app.del('/api/surveys/:sid/forms', function(req, response) {
var survey = req.params.sid;
console.log('!!! Deleting forms for survey ' + survey + ' from the database.');
db.collection(FORMS, function(err, collection) {
@@ -157,8 +157,8 @@ app.del('/surveys/:sid/forms', function(req, response) {
});
// Delete a single form from a survey
-// DELETE http://localhost:3000/surveys/{SURVEY_ID}/forms/{FORM_ID}
-app.del('/surveys/:sid/forms/:id', function(req, response) {
+// DELETE http://localhost:3000/api/surveys/{SURVEY_ID}/forms/{FORM_ID}
+app.del('/api/surveys/:sid/forms/:id', function(req, response) {
var survey = req.params.sid;
var id = req.params.id;
@@ -179,8 +179,8 @@ app.del('/surveys/:sid/forms/:id', function(req, response) {
});
// Get all forms that reference the specified parcel ID
-// GET http://localhost:3000/surveys/{SURVEY ID}/parcels/{PARCEL ID}/forms
-app.get('/surveys/:sid/parcels/:pid/forms', function(req, response) {
+// GET http://localhost:3000/api/surveys/{SURVEY ID}/parcels/{PARCEL ID}/forms
+app.get('/api/surveys/:sid/parcels/:pid/forms', function(req, response) {
var handleError = util.makeErrorHandler(response);
var sid = String(req.params.sid);
var pid = String(req.params.pid);
View
@@ -11,6 +11,8 @@
var pg = require('pg');
+var client = null;
+
function bboxToPolygon(bbox) {
var polygon = 'POLYGON((' +
bbox[0] + ' ' + bbox[1] + ', ' +
@@ -29,22 +31,21 @@ function setup(app, settings) {
var connectionString =
'tcp://' + settings.psqlUser + ':' + settings.psqlPass +
'@' + settings.psqlHost + '/' + settings.psqlName;
- var client = new pg.Client(connectionString);
- client.connect();
- app.on('close', function () {
- client.end();
- });
+ if (client === null) {
+ client = new pg.Client(connectionString);
+ client.connect();
+ }
// Get parcels
// Filter to include only parcels inside a bounding box or only parcels that
// intersect a point.
// We do not allow filtering by both, and we require one of the filters.
- // GET http://localhost:3000/parcels?bbox=-{SW_LON},{SW_LAT},{NE_LON},{NE_LAT}
- // GET http://localhost:3000/parcels?bbox=-83.0805,42.336,-83.08,42.34
- // GET http://localhost:3000/parcels?lon={LONGITUDE}&lat={LATITUDE}
- // GET http://localhost:3000/parcels?lon=-83.08076&lat=42.338
- app.get('/parcels', function(req, response) {
+ // GET http://localhost:3000/api/parcels?bbox=-{SW_LON},{SW_LAT},{NE_LON},{NE_LAT}
+ // GET http://localhost:3000/api/parcels?bbox=-83.0805,42.336,-83.08,42.34
+ // GET http://localhost:3000/api/parcels?lon={LONGITUDE}&lat={LATITUDE}
+ // GET http://localhost:3000/api/parcels?lon=-83.08076&lat=42.338
+ app.get('/api/parcels', function(req, response) {
var bbox = req.query.bbox;
var lon = req.query.lon;
var lat = req.query.lat;
View
@@ -250,9 +250,9 @@ function setup(app, db, idgen, collectionName) {
}
// Get all responses for a survey.
- // GET http://localhost:3000/surveys/{SURVEY ID}/responses
- // GET http://localhost:3000/surveys/1/responses
- app.get('/surveys/:sid/responses', function(req, response) {
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/responses
+ // GET http://localhost:3000/api/surveys/1/responses
+ app.get('/api/surveys/:sid/responses', function(req, response) {
var surveyid = req.params.sid;
getCollection(function(err, collection) {
collection.find({'survey': surveyid}, function(err, cursor) {
@@ -270,9 +270,9 @@ function setup(app, db, idgen, collectionName) {
// Get all responses for a specific parcel.
// TODO: At some point, parcel should become a generic geographic object ID.
- // GET http://localhost:3000/surveys/{SURVEY ID}/parcels/{PARCEL ID}/responses
- // GET http://localhost:3000/surveys/1/parcels/3728048/responses
- app.get('/surveys/:sid/parcels/:parcel_id/responses', function(req, response) {
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/parcels/{PARCEL ID}/responses
+ // GET http://localhost:3000/api/surveys/1/parcels/3728048/responses
+ app.get('/api/surveys/:sid/parcels/:parcel_id/responses', function(req, response) {
var surveyid = req.params.sid;
var parcel_id = req.params.parcel_id;
getCollection(function(err, collection) {
@@ -290,9 +290,9 @@ function setup(app, db, idgen, collectionName) {
});
// Get a response for a survey.
- // GET http://localhost:3000/surveys/{SURVEY ID}/responses/{RESPONSE ID}
- // GET http://localhost:3000/surveys/1/responses/2ec140e0-827f-11e1-83d8-bf682a6ee038
- app.get('/surveys/:sid/responses/:rid', function(req, response) {
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/responses/{RESPONSE ID}
+ // GET http://localhost:3000/api/surveys/1/responses/2ec140e0-827f-11e1-83d8-bf682a6ee038
+ app.get('/api/surveys/:sid/responses/:rid', function(req, response) {
var surveyid = req.params.sid;
var responseid = req.params.rid;
getCollection(function(err, collection) {
@@ -316,8 +316,8 @@ function setup(app, db, idgen, collectionName) {
});
// Delete a response from a survey
- // DELETE http://localhost:3000/surveys/{SURVEY ID}/responses/{RESPONSE ID}
- app.del('/surveys/:sid/responses/:rid', function(req, response) {
+ // DELETE http://localhost:3000/api/surveys/{SURVEY ID}/responses/{RESPONSE ID}
+ app.del('/api/surveys/:sid/responses/:rid', function(req, response) {
var survey = req.params.sid;
var id = req.params.rid;
console.log('Removing response ' + id + ' from the database.');
@@ -337,12 +337,12 @@ function setup(app, db, idgen, collectionName) {
});
// Add responses for a survey.
- // POST http://localhost:3000/surveys/{SURVEY ID}/reponses
- // POST http://localhost:3000/surveys/1/reponses
+ // POST http://localhost:3000/api/surveys/{SURVEY ID}/reponses
+ // POST http://localhost:3000/api/surveys/1/reponses
// Expects data in the format:
// responses: [
// { parcels: [ {parcel_id: '10', responses: {'Q0': 0, 'Q1': 3}} ]}, ...]
- app.post('/surveys/:sid/responses', function(req, response) {
+ app.post('/api/surveys/:sid/responses', function(req, response) {
var resps = req.body.responses;
var total = resps.length;
@@ -387,9 +387,9 @@ function setup(app, db, idgen, collectionName) {
// Delete all responses for a survey.
// This is maintainence functionality. Regular clients should not delete forms.
- // DELETE http://localhost:3000/surveys/{SURVEY ID}/reponses
- // DELETE http://localhost:3000/surveys/1/reponses
- app.del('/surveys/:sid/responses', function(req, response) {
+ // DELETE http://localhost:3000/api/surveys/{SURVEY ID}/reponses
+ // DELETE http://localhost:3000/api/surveys/1/reponses
+ app.del('/api/surveys/:sid/responses', function(req, response) {
var survey = req.params.sid;
console.log('!!! Deleting responses for survey ' + survey + ' from the database.');
getCollection(function(err, collection) {
@@ -406,9 +406,9 @@ function setup(app, db, idgen, collectionName) {
// Get all responses in a bounding box
- // GET http://localhost:3000/surveys/{SURVEY ID}/reponses/in/lower-left lat,lower-left lng, upper-right lat, upper-right lng
- // GET http://localhost:3000/surveys/{SURVEY ID}/reponses/in/1,2,3,4
- app.get('/surveys/:sid/responses/in/:bounds', function(req, response) {
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/reponses/in/lower-left lat,lower-left lng, upper-right lat, upper-right lng
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/reponses/in/1,2,3,4
+ app.get('/api/surveys/:sid/responses/in/:bounds', function(req, response) {
var surveyid = req.params.sid;
var bounds = req.params.bounds;
var coords = bounds.split(",");
@@ -584,22 +584,22 @@ function setup(app, db, idgen, collectionName) {
};
// Return response data as CSV
- // GET http://localhost:5000/surveys/{SURVEY ID}/csv
- app.get('/surveys/:sid/csv', function(req, response) {
+ // GET http://localhost:5000/api/surveys/{SURVEY ID}/csv
+ app.get('/api/surveys/:sid/csv', function(req, response) {
var sid = req.params.sid;
exportSurveyAs(response, sid, [], CSVWriter);
});
// Return CSV for WSU use
- // GET http://localhost:5000/surveys/{SURVEY ID}/csv-recent-peruse
- app.get('/surveys/:sid/csv-recent-peruse', function(req, response) {
+ // GET http://localhost:5000/api/surveys/{SURVEY ID}/csv-recent-peruse
+ app.get('/api/surveys/:sid/csv-recent-peruse', function(req, response) {
var sid = req.params.sid;
exportSurveyAs(response, sid, [filterToMostRecent, filterToOneRowPerUse], CSVWriter);
});
// Return response data as KML
- // GET http://localhost:5000/surveys/{SURVEY ID}/kml
- app.get('/surveys/:sid/kml', function(req, response) {
+ // GET http://localhost:5000/api/surveys/{SURVEY ID}/kml
+ app.get('/api/surveys/:sid/kml', function(req, response) {
var sid = req.params.sid;
exportSurveyAs(response, sid, [], KMLWriter);
});
View
@@ -10,7 +10,7 @@
survey: '1',
filename: 'img001.tif',
mimetype: 'image/tiff'
- url: 'http://localhost:3000/uploaded_files/e4043210-84db-11e1-b34b-dd2b6e24c3e7',
+ url: 'http://localhost:3000/api/uploaded_files/e4043210-84db-11e1-b34b-dd2b6e24c3e7',
status: 'pending'
}
* status can be 'pending', 'working', or 'complete'
@@ -64,9 +64,9 @@ function setup(app, db, idgen, collectionName, settings) {
// Add scanned form for a survey.
// This can be tested using the upload page:
// http://localhost:3000/static/upload.html
- // POST http://localhost:3000/surveys/{SURVEY ID}/scans
- // POST http://localhost:3000/surveys/1/scans
- app.post('/surveys/:sid/scans', function(req, response) {
+ // POST http://localhost:3000/api/surveys/{SURVEY ID}/scans
+ // POST http://localhost:3000/api/surveys/1/scans
+ app.post('/api/surveys/:sid/scans', function(req, response) {
console.log('Client is uploading a file');
var filename = req.headers['x-file-name'];
var id = idgen();
@@ -135,9 +135,9 @@ function setup(app, db, idgen, collectionName, settings) {
});
// Get data for a scanned form
- // GET http://localhost:3000/surveys/{SURVEY ID}/scans/{SCAN ID}
- // GET http://localhost:3000/surveys/1/scans/234
- app.get('/surveys/:sid/scans/:id', function(req, response) {
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/scans/{SCAN ID}
+ // GET http://localhost:3000/api/surveys/1/scans/234
+ app.get('/api/surveys/:sid/scans/:id', function(req, response) {
console.log('Getting data for a scanned image');
var handleError = util.makeErrorHandler(response);
var id = req.params.id;
@@ -166,12 +166,12 @@ function setup(app, db, idgen, collectionName, settings) {
});
// Get a scanned image
- // GET http://localhost:3000/uploaded_files/{SCAN ID}
- // GET http://localhost:3000/uploaded_files/234
+ // GET http://localhost:3000/api/uploaded_files/{SCAN ID}
+ // GET http://localhost:3000/api/uploaded_files/234
// TODO: add an extension to the stored filename, so that we don't have to
// hit the server to determine MIME type
// TODO: make the S3 object publicly accessible and just redirect to its URL
- app.get('/' + UPLOAD_DIR + '/:id', function(req, response) {
+ app.get('/api/' + UPLOAD_DIR + '/:id', function(req, response) {
console.log('Sending image file to client');
var handleError = util.makeErrorHandler(response);
var id = req.params.id;
@@ -208,11 +208,11 @@ function setup(app, db, idgen, collectionName, settings) {
// Get all the scanned form data for a survey
// Optionally filter according to the status
- // GET http://localhost:3000/surveys/{SURVEY ID}/scans
- // GET http://localhost:3000/surveys/1/scans
- // GET http://localhost:3000/surveys/1/scans?status=pending
- // GET http://localhost:3000/surveys/1/scans?status=complete
- app.get('/surveys/:sid/scans', function(req, response) {
+ // GET http://localhost:3000/api/surveys/{SURVEY ID}/scans
+ // GET http://localhost:3000/api/surveys/1/scans
+ // GET http://localhost:3000/api/surveys/1/scans?status=pending
+ // GET http://localhost:3000/api/surveys/1/scans?status=complete
+ app.get('/api/surveys/:sid/scans', function(req, response) {
var handleError = util.makeErrorHandler(response);
var sid = req.params.sid;
var status = req.query.status;
@@ -234,8 +234,8 @@ function setup(app, db, idgen, collectionName, settings) {
});
// Update scanned form data
- // PUT http://localhost:3000/surveys/{SURVEY_ID}/scans/{SCAN_ID}
- app.put('/surveys/:sid/scans/:id', function(req, response) {
+ // PUT http://localhost:3000/api/surveys/{SURVEY_ID}/scans/{SCAN_ID}
+ app.put('/api/surveys/:sid/scans/:id', function(req, response) {
var id = req.params.id;
var survey = req.params.sid;
var status = req.body.scan.status;
@@ -263,9 +263,9 @@ function setup(app, db, idgen, collectionName, settings) {
});
// Delete a single scanned form entry from a survey
- // DELETE http://localhost:3000/surveys/{SURVEY_ID}/forms/{FORM_ID}
+ // DELETE http://localhost:3000/api/surveys/{SURVEY_ID}/forms/{FORM_ID}
// TODO: remove the corresponding S3 object
- app.del('/surveys/:sid/scans/:id', function(req, response) {
+ app.del('/api/surveys/:sid/scans/:id', function(req, response) {
var survey = req.params.sid;
var id = req.params.id;
Oops, something went wrong.

0 comments on commit 4229337

Please sign in to comment.