Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve static files and add iconUrl property to sensor #690

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ userimages*

# exclude everything
dumps/*
assets/*

# exception to the rule
!dumps/.gitkeep
7 changes: 7 additions & 0 deletions config/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 4 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/api/lib/controllers/boxesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 13 additions & 1 deletion packages/api/lib/routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const restify = require('restify');
const { usersController,
statisticsController,
boxesController,
Expand All @@ -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++) {
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/models/src/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down
8 changes: 8 additions & 0 deletions packages/models/src/sensor/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = {
Expand Down