From 6bd947554e5f657015964fdbf2908c11472a97dc Mon Sep 17 00:00:00 2001 From: Matthias Pfeil Date: Wed, 4 Jan 2023 18:06:24 +0100 Subject: [PATCH 1/3] Serve static files and add iconUrl property to sensor --- .gitignore | 1 + config/default.js | 6 +++++- packages/api/lib/routes.js | 14 +++++++++++++- packages/models/src/sensor/sensor.js | 4 ++++ 4 files changed, 23 insertions(+), 2 deletions(-) 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/default.js b/config/default.js index 70ba3f3a..b7c13593 100644 --- a/config/default.js +++ b/config/default.js @@ -13,13 +13,17 @@ const defaults = { api_url: '', // if not set, generated from api_protocol and api_base_domain honeybadger_apikey: '', slack_url: '', - mattermost_url: '', + mattermost_url: 'https://chat.reedu.de/hooks/kehtgn3n1bfj9ka9kjgkuyyh3a', management_role: 'admin', routes: { boxes: '/boxes', 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/routes.js b/packages/api/lib/routes.js index cf8742f6..829c0cf4 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' }, @@ -129,6 +134,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/sensor/sensor.js b/packages/models/src/sensor/sensor.js index c265f5f1..7b31af07 100644 --- a/packages/models/src/sensor/sensor.js +++ b/packages/models/src/sensor/sensor.js @@ -26,6 +26,10 @@ const sensorSchema = new mongoose.Schema({ required: false, trim: true }, + iconUrl: { + type: String, + required: false + }, lastMeasurement: { type: mongoose.Schema.Types.ObjectId, ref: 'Measurement' From e47adac780cd5ed696d35351962a778b7c408cfc Mon Sep 17 00:00:00 2001 From: Matthias Pfeil Date: Wed, 4 Jan 2023 18:51:16 +0100 Subject: [PATCH 2/3] clean up and lint --- config/default.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/default.js b/config/default.js index b7c13593..49c140a1 100644 --- a/config/default.js +++ b/config/default.js @@ -13,7 +13,7 @@ const defaults = { api_url: '', // if not set, generated from api_protocol and api_base_domain honeybadger_apikey: '', slack_url: '', - mattermost_url: 'https://chat.reedu.de/hooks/kehtgn3n1bfj9ka9kjgkuyyh3a', + mattermost_url: '', management_role: 'admin', routes: { boxes: '/boxes', From e27c60f566a37921501ccf7ea5b1887066c34bae Mon Sep 17 00:00:00 2001 From: Matthias Pfeil Date: Thu, 5 Jan 2023 11:16:16 +0100 Subject: [PATCH 3/3] Include iconUrl as virtual in sensor schema --- config/config.example.json | 7 +++++++ packages/api/lib/controllers/boxesController.js | 2 +- packages/models/src/box/box.js | 1 + packages/models/src/sensor/sensor.js | 12 ++++++++---- 4 files changed, 17 insertions(+), 5 deletions(-) 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/packages/api/lib/controllers/boxesController.js b/packages/api/lib/controllers/boxesController.js index 7ef90bfc..77364d2c 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/models/src/box/box.js b/packages/models/src/box/box.js index 1710f603..38cd8627 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 7b31af07..e91f0472 100644 --- a/packages/models/src/sensor/sensor.js +++ b/packages/models/src/sensor/sensor.js @@ -26,16 +26,16 @@ const sensorSchema = new mongoose.Schema({ required: false, trim: true }, - iconUrl: { - type: String, - required: false - }, lastMeasurement: { type: mongoose.Schema.Types.ObjectId, ref: 'Measurement' } }, { usePushEach: true }); +sensorSchema.set('toJSON', { + virtuals: true +}); + sensorSchema.methods.equals = function equals ({ unit, sensorType, title, _id }) { if (_id) { return this._id.equals(_id); @@ -114,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 = {