2017-04-16 15:49:53 +00:00
// 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
2020-03-04 04:25:43 +00:00
// REQUIRED: Access URL must be set using --accessUrl=<URL> or accessUrl ENV variable
2017-04-16 15:49:53 +00:00
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
2019-04-20 08:03:44 +00:00
const SettingsHelper = require ( './SettingsHelper' ) ;
2017-04-16 15:49:53 +00:00
2019-04-20 08:03:44 +00:00
const settings = new SettingsHelper ( ) ;
console . log ( 'Settings' , settings ) ;
2020-03-04 04:25:43 +00:00
let PORT = settings . webapp _port || 8088 ;
2019-04-20 08:03:44 +00:00
const bootstrap = ( ) => new Promise ( async ( resolve , reject ) => {
if ( ! settings . accessUrl ) {
2020-03-04 04:25:43 +00:00
console . log ( 'Missing required argument `accessUrl`. EG. "node webapp.js -accessUrl=http://sl.example.com". This URL is used for redirecting invite links.' ) ;
2019-04-20 08:03:44 +00:00
return reject ( new Error ( 'Missing URL for invite links' ) ) ;
}
2020-03-04 04:25:43 +00:00
if ( ! settings . webapp _port ) {
2019-04-20 08:03:44 +00:00
console . log ( 'Defaulting webapp to port 8088' ) ;
}
PORT = parseInt ( PORT ) ;
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 ) ;
let fullUrl ;
const params = {
server : data . server ,
room : data . room ,
owner : data . owner ,
} ;
if ( data . password ) {
params . password = data . password ;
2018-07-20 04:31:44 +00:00
}
2019-04-20 08:03:44 +00:00
let query = '' ;
for ( const key in params ) {
query += ` ${ encodeURIComponent ( key ) } = ${ params [ key ] } & ` ;
2018-07-20 04:31:44 +00:00
}
2020-03-04 04:25:43 +00:00
fullUrl = ` ${ settings . accessUrl } /#/join? ${ query } ` ;
2019-04-20 08:03:44 +00:00
data . fullUrl = fullUrl ;
data . code = ( 0 | Math . random ( ) * 9e6 ) . toString ( 36 ) ;
cb ( ) ;
} ;
Waterline . start ( baseSettings . waterline , ( err , orm ) => {
if ( err ) {
return reject ( err ) ;
2018-07-20 04:31:44 +00:00
}
2019-04-20 08:03:44 +00:00
resolve ( orm ) ;
} ) ;
} ) ;
2017-04-16 15:49:53 +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 ( ) ;
2020-03-04 04:25:43 +00:00
// Setup our web app
2019-04-20 08:03:44 +00:00
root . use ( cors ( ) ) ;
root . use ( bodyParser ( ) ) ;
root . use ( ` ${ settings . webroot } / ` , express . static ( path . join ( _ _dirname , 'dist' ) ) ) ;
2020-03-04 04:25:43 +00:00
// 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 ) {
2020-03-04 04:25:43 +00:00
return res . redirect ( settings . accessUrl + settings . webroot ) ;
2017-04-16 15:49:53 +00:00
}
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 ) {
2018-07-20 04:31:44 +00:00
return res . send ( {
success : false ,
2019-04-20 08:03:44 +00:00
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 ) {
return res . send ( {
success : false ,
2019-04-20 08:03:44 +00:00
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
}
2019-04-20 08:03:44 +00:00
const result = await Waterline . getModel ( 'invite' , orm ) . create ( { ... data } ) . fetch ( ) ;
2017-12-02 01:16:41 +00:00
return res . send ( {
2020-03-04 04:25:43 +00:00
url : ` ${ settings . accessUrl } /invite/ ${ result . code } ` ,
2018-07-20 04:31:44 +00:00
success : true ,
generatedAt : new Date ( ) . getTime ( ) ,
2019-04-20 08:03:44 +00:00
details : result ,
} ) . end ( ) ;
} ) ;
2020-03-04 04:25:43 +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' ) ;
2020-03-04 04:25:43 +00:00
res . send ( settings ) ;
2019-07-07 07:27:40 +00:00
} ) ;
2020-03-04 04:25:43 +00:00
// Catch anything else and redirect to the base URL
2018-07-20 04:31:44 +00:00
root . get ( '*' , ( req , res ) => {
2020-03-04 04:25:43 +00:00
console . log ( 'Catch all:' , req . url ) ;
return res . redirect ( ` ${ settings . webroot } / ` ) ;
2019-04-20 08:03:44 +00:00
} ) ;
2020-03-04 04:25:43 +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 } ` ) ;
2020-03-04 04:25:43 +00:00
if ( settings . webroot ) {
console . log ( ` Running with base URL: ${ settings . webroot } ` ) ;
}
console . log ( ` Access URL is ${ settings . accessUrl } ` ) ;
2019-04-20 08:03:44 +00:00
} ;
2017-12-30 02:23:08 +00:00
2018-07-20 04:31:44 +00:00
bootstrap ( ) . then ( ( orm ) => {
2019-04-20 08:03:44 +00:00
app ( orm ) ;
2018-07-20 04:31:44 +00:00
} ) . catch ( ( e ) => {
2019-04-20 08:03:44 +00:00
console . log ( 'Error bootstrapping webapp:' , e ) ;
} ) ;