synclounge/webapp.js

171 lines
5.3 KiB
JavaScript
Raw Normal View History

// ABOUT
2017-12-13 12:58:15 +00:00
// Runs the SyncLounge Web App - handles serving the static web content and link shortening services
2017-06-04 07:05:30 +00:00
// Port defaults to 8088
// REQUIRED: Access URL must be set. See documentation for how to set this.
2019-04-20 08:03:44 +00:00
const express = require('express');
const path = require('path');
const cors = require('cors');
const bodyParser = require('body-parser');
const Waterline = require('waterline');
const WaterlineMysql = require('waterline-mysql');
const SailsDisk = require('sails-disk');
2018-07-20 04:31:44 +00:00
const { readSettings } = require('./SettingsHelper');
2020-05-26 22:04:27 +00:00
const settings = readSettings();
2020-03-24 17:08:49 +00:00
let PORT = 8088;
2019-04-20 08:03:44 +00:00
2020-05-26 22:04:27 +00:00
const bootstrap = () =>
new Promise(async (resolve, reject) => {
if (!settings.accessUrl) {
console.log(
'Missing required argument `accessUrl`. This URL is used for redirecting invite links. See documentation for how to set this',
);
return reject(new Error('Missing URL for invite links'));
2018-07-20 04:31:44 +00:00
}
2020-05-26 22:04:27 +00:00
if (!settings.webapp_port) {
console.log('Defaulting webapp to port 8088');
} else {
PORT = settings.webapp_port;
2018-07-20 04:31:44 +00:00
}
2020-05-26 22:04:27 +00:00
PORT = parseInt(PORT, 10);
const baseSettings = require('./waterline_settings.json');
// console.log('Basesettings', baseSettings);
baseSettings.waterline.adapters = {
'waterline-mysql': WaterlineMysql,
'sails-disk': SailsDisk,
};
baseSettings.waterline.datastores = baseSettings.database.datastores;
baseSettings.waterline.models.invite.beforeCreate = async (data, cb) => {
console.log('Creating Invite', data);
const params = {
server: data.server,
room: data.room,
owner: data.owner,
};
if (data.password) {
params.password = data.password;
}
const query = Object.entries(params)
2020-05-26 23:21:19 +00:00
.map(([key, value]) => `${encodeURIComponent(key)}=${value}`)
2020-05-26 22:04:27 +00:00
.join('&');
const fullUrl = `${settings.accessUrl}/#/join?${query}`;
data.fullUrl = fullUrl;
data.code = (0 | (Math.random() * 9e6)).toString(36);
cb();
};
Waterline.start(baseSettings.waterline, (err, orm) => {
if (err) {
return reject(err);
}
resolve(orm);
});
2019-04-20 08:03:44 +00:00
});
2018-07-20 04:31:44 +00:00
const app = async (orm) => {
2019-04-20 08:03:44 +00:00
const root = express();
// Setup our web app
2019-04-20 08:03:44 +00:00
root.use(cors());
2020-03-22 03:28:51 +00:00
root.use(bodyParser.json());
2020-05-26 22:04:27 +00:00
root.use(
bodyParser.urlencoded({
extended: true,
}),
);
2019-04-20 08:03:44 +00:00
root.use(`${settings.webroot}/`, express.static(path.join(__dirname, 'dist')));
// Invite handling
2019-04-20 08:03:44 +00:00
root.get(`${settings.webroot}/invite/:id`, async (req, res) => {
console.log('handling an invite', req.params.id);
const shortObj = await Waterline.getModel('invite', orm).findOne({ code: req.params.id });
console.log('Invite data', shortObj);
2018-07-20 04:31:44 +00:00
if (!shortObj) {
return res.redirect(settings.accessUrl + settings.webroot);
}
2019-04-20 08:03:44 +00:00
console.log('Redirecting an invite link', shortObj);
return res.redirect(shortObj.fullUrl);
});
root.post(`${settings.webroot}/invite`, async (req, res) => {
res.setHeader('Content-Type', 'application/json');
2017-12-02 01:16:41 +00:00
if (!req.body) {
2020-05-26 22:04:27 +00:00
return res
.send({
success: false,
msg: 'ERR: You did not send any POST data',
})
.end();
2017-12-02 01:16:41 +00:00
}
2019-04-20 08:03:44 +00:00
const data = {};
const fields = ['server', 'room', 'password', 'owner'];
2017-12-02 01:16:41 +00:00
for (let i = 0; i < fields.length; i++) {
2018-07-20 04:31:44 +00:00
if (req.body[fields[i]] === undefined) {
2020-05-26 22:04:27 +00:00
return res
.send({
success: false,
msg: `ERR: You did not specify ${fields[i]}`,
field: fields[i],
})
.end();
2018-07-20 04:31:44 +00:00
}
2019-04-20 08:03:44 +00:00
data[fields[i]] = req.body[fields[i]];
2017-12-02 01:16:41 +00:00
}
2020-05-26 22:04:27 +00:00
const result = await Waterline.getModel('invite', orm)
.create({ ...data })
.fetch();
return res
.send({
url: `${settings.accessUrl}/invite/${result.code}`,
success: true,
generatedAt: new Date().getTime(),
details: result,
})
.end();
2019-04-20 08:03:44 +00:00
});
// Config handling
root.get(`${settings.webroot}/config`, (req, res) => {
2019-07-07 07:27:40 +00:00
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1');
res.header('Pragma', 'no-cache');
res.send(settings);
2019-07-07 07:27:40 +00:00
});
// Catch anything else and redirect to the base URL
2018-07-20 04:31:44 +00:00
root.get('*', (req, res) => {
console.log('Catch all:', req.url);
return res.redirect(`${settings.webroot}/`);
2019-04-20 08:03:44 +00:00
});
2019-04-20 08:03:44 +00:00
const rootserver = require('http').createServer(root);
rootserver.listen(PORT);
console.log(`SyncLounge WebApp successfully started on port ${PORT}`);
if (settings.webroot) {
console.log(`Running with base URL: ${settings.webroot}`);
}
if (settings.accessUrl) {
console.log(`Access URL is ${settings.accessUrl}`);
if (settings.webroot && !settings.accessUrl.includes(settings.webroot)) {
2020-05-26 22:04:27 +00:00
console.log(
`- WARNING: Your Access URL does not contain your webroot/WEBROOT setting: '${settings.webroot}'. Invite URLs may not work properly.`,
);
}
}
if (settings.authentication && settings.authentication.mechanism != 'none') {
console.log('Authentication:', settings.authentication);
}
if (settings.servers) {
console.log('Servers List:', settings.servers);
2020-05-26 22:04:27 +00:00
} else if (settings.custom_server) {
console.log('Custom Server List:', settings.custom_server);
}
2019-04-20 08:03:44 +00:00
};
2017-12-30 02:23:08 +00:00
2020-05-26 22:04:27 +00:00
bootstrap()
.then((orm) => {
app(orm);
})
.catch((e) => {
console.log('Error bootstrapping webapp:', e);
});