diff --git a/.gitignore b/.gitignore index f4867d4a..d993857d 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ userimages* # exclude everything dumps/* +assets/* # exception to the rule !dumps/.gitkeep diff --git a/config/config.example.json b/config/config.example.json index 11692621..9a5ffc89 100644 --- a/config/config.example.json +++ b/config/config.example.json @@ -49,6 +49,13 @@ // Base route for management routes // Default: "/management" "management": "/my-custom-management-path" + // Base route for assets route + // Default path: "/assets" + // Default directory: "./assets" relative to process + "assets": { + "path": "/my-custom-management-path", + "directory": "./public" + } }, // Json Web Token configuration "jwt": { diff --git a/config/default.js b/config/default.js index 70ba3f3a..49c140a1 100644 --- a/config/default.js +++ b/config/default.js @@ -20,6 +20,10 @@ const defaults = { users: '/users', statistics: '/statistics', management: '/management', + assets: { + path: '/assets', + directory: './assets', + }, }, jwt: { secret: 'OH GOD THIS IS SO INSECURE PLS CHANGE ME', // should be at least 32 characters diff --git a/packages/api/lib/controllers/boxesController.js b/packages/api/lib/controllers/boxesController.js index 03170c17..fbf8229b 100644 --- a/packages/api/lib/controllers/boxesController.js +++ b/packages/api/lib/controllers/boxesController.js @@ -361,7 +361,7 @@ const getBox = async function getBox (req, res, next) { const { format, boxId } = req._userParams; try { - const box = await Box.findBoxById(boxId); + const box = await Box.findBoxById(boxId, { lean: false }); if (format === 'geojson') { const coordinates = box.currentLocation.coordinates; diff --git a/packages/api/lib/routes.js b/packages/api/lib/routes.js index 8a249535..5270ff62 100644 --- a/packages/api/lib/routes.js +++ b/packages/api/lib/routes.js @@ -1,5 +1,6 @@ 'use strict'; +const restify = require('restify'); const { usersController, statisticsController, boxesController, @@ -11,6 +12,7 @@ const { usersController, { verifyJwt } = require('./helpers/jwtHelpers'), { initUserParams, checkPrivilege } = require('./helpers/userParamHelpers'); + const spaces = function spaces (num) { let str = ' '; for (let i = 1; i < num; i++) { @@ -67,10 +69,13 @@ const printRoutes = function printRoutes (req, res) { res.end(lines.join('\n')); }; -const { boxes: boxesPath, users: usersPath, statistics: statisticsPath, management: managementPath } = config.get('routes'); +const { assets, boxes: boxesPath, users: usersPath, statistics: statisticsPath, management: managementPath } = config.get('routes'); // the ones matching first are used // case is ignored const routes = { + 'static': [ + { path: `${assets.path}/*`, method: 'get', directory: assets.directory, reference: 'api-Misc-getAssets' } + ], 'noauth': [ { path: '/', method: 'get', handler: printRoutes, reference: 'api-Misc-printRoutes' }, { path: '/stats', method: 'get', handler: statisticsController.getStatistics, reference: 'api-Misc-getStatistics' }, @@ -130,6 +135,13 @@ const initRoutes = function initRoutes (server) { // attach a function for user parameters server.use(initUserParams); + for (const route of routes.static) { + server[route.method]({ path: route.path }, restify.plugins.serveStatic({ + appendRequestPath: false, + directory: route.directory + })); + } + // attach the routes for (const route of routes.noauth) { server[route.method]({ path: route.path }, route.handler); diff --git a/packages/models/src/box/box.js b/packages/models/src/box/box.js index 32200502..fbbd1699 100644 --- a/packages/models/src/box/box.js +++ b/packages/models/src/box/box.js @@ -158,6 +158,7 @@ const BOX_SUB_PROPS_FOR_POPULATION = [ boxSchema.set('toJSON', { version: false, + virtuals: true, transform: function transform (doc, ret, options) { const box = {}; diff --git a/packages/models/src/sensor/sensor.js b/packages/models/src/sensor/sensor.js index c265f5f1..e91f0472 100644 --- a/packages/models/src/sensor/sensor.js +++ b/packages/models/src/sensor/sensor.js @@ -32,6 +32,10 @@ const sensorSchema = new mongoose.Schema({ } }, { usePushEach: true }); +sensorSchema.set('toJSON', { + virtuals: true +}); + sensorSchema.methods.equals = function equals ({ unit, sensorType, title, _id }) { if (_id) { return this._id.equals(_id); @@ -110,6 +114,10 @@ sensorSchema.methods.deleteMeasurements = function deleteMeasurements (createdAt }; +sensorSchema.virtual('iconUrl').get(function () { + return `http://localhost:8000/assets/${this.icon}.png`; +}); + const sensorModel = mongoose.model('Sensor', sensorSchema); module.exports = {