-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from hotosm/develop
tms endpoint + very basic auth resolves #20
- Loading branch information
Showing
17 changed files
with
734 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
var async = require('async'); | ||
var Model = require('../models/tms.js'); | ||
var meta = require('./meta.js'); | ||
|
||
/** | ||
* Query TMS model. Implements all protocols supported by /meta endpoint | ||
* | ||
* @param {Object} payload - Payload contains query paramters and their values | ||
* @param {recordsCallback} cb - The callback that returns the records | ||
*/ | ||
module.exports.query = function (payload, page, limit, cb) { | ||
// bounding box search | looks for bbox in payload | ||
|
||
var skip = limit * (page - 1); | ||
|
||
// Execute the search and return the result via callback | ||
Model.count(payload, function (err, count) { | ||
if (err) { | ||
return cb(err, null, null); | ||
} | ||
Model.find(payload, null, { skip: skip, limit: limit }).exec(function (err, records) { | ||
cb(err, records, count); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports.addUpdate = function (payload, cb) { | ||
|
||
var images = []; | ||
|
||
async.each(payload.images, function (image, callback) { | ||
meta.addUpdateTms(image.uuid, payload.uri, function (err, meta) { | ||
images.push(meta); | ||
return callback(err); | ||
}); | ||
}, function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
var options = { upsert: true, new: true }; | ||
var query = { uuid: payload.uri }; | ||
payload.images = images; | ||
Model.findOneAndUpdate(query, payload, options, function (err, record) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
|
||
cb(err, record); | ||
}); | ||
|
||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
var mongoose = require('mongoose'); | ||
|
||
var tmsSchema = new mongoose.Schema({ | ||
uri: {type: String, unique: true, required: true }, // The URI of TMS | ||
images: [{ | ||
uuid: {type: String, required: true} | ||
}], | ||
created_at: Date | ||
}); | ||
|
||
module.exports = mongoose.model('tms', tmsSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
var _ = require('lodash'); | ||
var Boom = require('boom'); | ||
var tms = require('../controllers/tms.js'); | ||
|
||
module.exports = [ | ||
{ | ||
method: 'POST', | ||
path: '/tms', | ||
handler: function (request, reply) { | ||
if (!_.isEmpty(request.payload) && _.has(request.payload, 'uri') && _.has(request.payload, 'images')) { | ||
tms.addUpdate(request.payload, function (err, record) { | ||
if (err) { | ||
return reply(Boom.badRequest(err)); | ||
} | ||
|
||
return reply(record); | ||
}); | ||
} else { | ||
var err = Boom.create( | ||
400, | ||
'There is an Error. Fields missing.', | ||
{ timestamp: Date.now() } | ||
); | ||
return reply(Boom.badRequest(err)); | ||
} | ||
}, | ||
config: { auth: 'simple' } | ||
}, | ||
{ | ||
method: 'GET', | ||
path: '/tms', | ||
handler: function (request, reply) { | ||
var payload = {}; | ||
|
||
if (request.query) { | ||
payload = request.query; | ||
} | ||
|
||
tms.query(payload, request.page, request.limit, function (err, records, count) { | ||
if (err) { | ||
return reply(err.message); | ||
} | ||
|
||
request.count = count; | ||
return reply(records); | ||
}); | ||
} | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
// This is a place holder with a basic token authentication | ||
// More advanced authentiction will be added later | ||
|
||
module.exports = function (token, callback) { | ||
var secretToken = process.env.SECRET_TOKEN || 'insecuretoken'; | ||
|
||
if (token === secretToken) { | ||
callback(null, true, {token: token}); | ||
} else { | ||
callback(null, false, { token: token }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.