Update webapp with fixes and changes

- Fix webroot issues
- Clean-up accessIp/accessUrl
- Remove duplicate CORS
- Remove duplicate static mapping
- Update comments and some outputs
This commit is contained in:
MagicalCodeMonkey 2020-03-03 23:25:43 -05:00
parent 789ce94ad0
commit fc4b30d73a
1 changed files with 20 additions and 19 deletions

View File

@ -1,7 +1,7 @@
// ABOUT // ABOUT
// Runs the SyncLounge Web App - handles serving the static web content and link shortening services // Runs the SyncLounge Web App - handles serving the static web content and link shortening services
// Port defaults to 8088 // Port defaults to 8088
// REQUIRED: --url argument // REQUIRED: Access URL must be set using --accessUrl=<URL> or accessUrl ENV variable
const express = require('express'); const express = require('express');
const path = require('path'); const path = require('path');
@ -15,19 +15,14 @@ const SettingsHelper = require('./SettingsHelper');
const settings = new SettingsHelper(); const settings = new SettingsHelper();
console.log('Settings', settings); console.log('Settings', settings);
let accessIp = ''; let PORT = settings.webapp_port || 8088;
let PORT = 8088;
const bootstrap = () => new Promise(async (resolve, reject) => { const bootstrap = () => new Promise(async (resolve, reject) => {
const args = require('args-parser')(process.argv);
if (!settings.accessUrl) { if (!settings.accessUrl) {
console.log('Missing required argument -accessUrl. EG. "node webapp.js -accessUrl=http://sl.example.com". This URL is used for redirecting invite links.'); console.log('Missing required argument `accessUrl`. EG. "node webapp.js -accessUrl=http://sl.example.com". This URL is used for redirecting invite links.');
return reject(new Error('Missing URL for invite links')); return reject(new Error('Missing URL for invite links'));
} }
accessIp = settings.accessUrl;// EG 'http://95.231.444.12:8088/slweb' or 'http://example.com/slweb' if (!settings.webapp_port) {
if (args.webapp_port || process.env.webapp_port) {
PORT = args.webapp_port || process.env.webapp_port;
} else {
console.log('Defaulting webapp to port 8088'); console.log('Defaulting webapp to port 8088');
} }
PORT = parseInt(PORT); PORT = parseInt(PORT);
@ -53,7 +48,7 @@ const bootstrap = () => new Promise(async (resolve, reject) => {
for (const key in params) { for (const key in params) {
query += `${encodeURIComponent(key)}=${params[key]}&`; query += `${encodeURIComponent(key)}=${params[key]}&`;
} }
fullUrl = `${accessIp}/#/join?${query}`; fullUrl = `${settings.accessUrl}/#/join?${query}`;
data.fullUrl = fullUrl; data.fullUrl = fullUrl;
data.code = (0 | Math.random() * 9e6).toString(36); data.code = (0 | Math.random() * 9e6).toString(36);
cb(); cb();
@ -68,16 +63,17 @@ const bootstrap = () => new Promise(async (resolve, reject) => {
const app = async (orm) => { const app = async (orm) => {
const root = express(); const root = express();
// Setup our web app
root.use(cors()); root.use(cors());
root.use(bodyParser()); root.use(bodyParser());
// Setup our web app
root.use(`${settings.webroot}/`, express.static(path.join(__dirname, 'dist'))); root.use(`${settings.webroot}/`, express.static(path.join(__dirname, 'dist')));
// Invite handling
root.get(`${settings.webroot}/invite/:id`, async (req, res) => { root.get(`${settings.webroot}/invite/:id`, async (req, res) => {
console.log('handling an invite', req.params.id); console.log('handling an invite', req.params.id);
const shortObj = await Waterline.getModel('invite', orm).findOne({ code: req.params.id }); const shortObj = await Waterline.getModel('invite', orm).findOne({ code: req.params.id });
console.log('Invite data', shortObj); console.log('Invite data', shortObj);
if (!shortObj) { if (!shortObj) {
return res.redirect(accessIp + settings.webroot); return res.redirect(settings.accessUrl + settings.webroot);
} }
console.log('Redirecting an invite link', shortObj); console.log('Redirecting an invite link', shortObj);
return res.redirect(shortObj.fullUrl); return res.redirect(shortObj.fullUrl);
@ -104,27 +100,32 @@ const app = async (orm) => {
} }
const result = await Waterline.getModel('invite', orm).create({ ...data }).fetch(); const result = await Waterline.getModel('invite', orm).create({ ...data }).fetch();
return res.send({ return res.send({
url: `${accessIp}/invite/${result.code}`, url: `${settings.accessUrl}/invite/${result.code}`,
success: true, success: true,
generatedAt: new Date().getTime(), generatedAt: new Date().getTime(),
details: result, details: result,
}).end(); }).end();
}); });
root.get('/config', (req, res) => { // Config handling
root.get(`${settings.webroot}/config`, (req, res) => {
res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
res.header('Expires', '-1'); res.header('Expires', '-1');
res.header('Pragma', 'no-cache'); res.header('Pragma', 'no-cache');
res.send(SettingsHelper()) res.send(settings);
}); });
root.use('/', express.static(path.join(__dirname, 'dist'))); // Catch anything else and redirect to the base URL
root.get('*', (req, res) => { root.get('*', (req, res) => {
console.log('Catch all'); console.log('Catch all:', req.url);
return res.redirect('/'); return res.redirect(`${settings.webroot}/`);
}); });
root.use(cors());
const rootserver = require('http').createServer(root); const rootserver = require('http').createServer(root);
rootserver.listen(PORT); rootserver.listen(PORT);
console.log(`SyncLounge WebApp successfully started on port ${PORT}`); console.log(`SyncLounge WebApp successfully started on port ${PORT}`);
if (settings.webroot) {
console.log(`Running with base URL: ${settings.webroot}`);
}
console.log(`Access URL is ${settings.accessUrl}`);
}; };
bootstrap().then((orm) => { bootstrap().then((orm) => {