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
// REQUIRED: --url argument
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 ) ;
let accessIp = '' ;
let PORT = 8088 ;
const bootstrap = ( ) => new Promise ( async ( resolve , reject ) => {
const args = require ( 'args-parser' ) ( process . argv ) ;
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.' ) ;
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 ( args . webapp _port || process . env . webapp _port ) {
PORT = args . webapp _port || process . env . webapp _port ;
} else {
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
}
2019-04-20 08:03:44 +00:00
fullUrl = ` ${ accessIp } /#/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 ) ;
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 ( ) ;
root . use ( cors ( ) ) ;
root . use ( bodyParser ( ) ) ;
2018-07-20 04:31:44 +00:00
// Setup our web app
2019-04-20 08:03:44 +00:00
root . use ( ` ${ settings . webroot } / ` , express . static ( path . join ( _ _dirname , 'dist' ) ) ) ;
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 ) {
2019-04-20 08:03:44 +00:00
return res . redirect ( accessIp + 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 ( {
2019-04-20 08:03:44 +00:00
url : ` ${ accessIp } /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 ( ) ;
} ) ;
root . get ( '/config' , ( req , res ) => res . send ( SettingsHelper ( ) ) ) ;
root . use ( '/' , express . static ( path . join ( _ _dirname , 'dist' ) ) ) ;
2018-07-20 04:31:44 +00:00
root . get ( '*' , ( req , res ) => {
2019-04-20 08:03:44 +00:00
console . log ( 'Catch all' ) ;
return res . redirect ( '/' ) ;
} ) ;
root . use ( cors ( ) ) ;
const rootserver = require ( 'http' ) . createServer ( root ) ;
rootserver . listen ( PORT ) ;
console . log ( ` SyncLounge WebApp successfully started on port ${ PORT } ` ) ;
} ;
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 ) ;
} ) ;