diff --git a/src/server/app.js b/src/server/app.js index 986b54d..86a9b07 100644 --- a/src/server/app.js +++ b/src/server/app.js @@ -15,9 +15,6 @@ const cookieParser = require( 'cookie-parser' ); const http = require( 'http' ); const fs = require( 'fs' ); const token = require( './backend/token.js' ); -// const pm = require( './backend/plugins/manager.js' ); -// const pluginManager = new pm(); - console.log( ` @@ -38,20 +35,18 @@ _ _ _ _ ==> You are running Version V1.0.0 - ==> You should deploy this on a server! - Below you can see all important things that happen during operation. libreevent logs all errors in the console such that they appear in the -log files when running it with an output pipe. +log files when running it with an output pipe (which you should definitely do) + +To do this run the following command when starting libreevent: +'node app.js > libreevent_log.txt' ` ); console.log( '[ Server ] loading settings' ); const settings = JSON.parse( fs.readFileSync( path.join( __dirname + '/config/settings.config.json' ) ) ); -// const mail = require( './backend/mail/mailSender.js' ); -// const mailManager = new mail(); - console.log( '[ Server ] Setting up static routes' ); if ( settings.init ) { app.use( express.static( 'webapp/main/dist' ) ); @@ -72,8 +67,6 @@ app.use( expressSession( { } } ) ); -// app.use( bodyParser.urlencoded( { extended: false } ) ); -// app.use( bodyParser.json() ); app.use( cookieParser() ); let file = path.join( __dirname + '/webapp/main/dist/index.html' ); @@ -85,17 +78,12 @@ if ( settings.init ) { require( './backend/userAPIRoutes.js' )( app, settings ); // admin api routes require( './backend/userRoutes.js' )( app, settings ); // user routes require( './backend/payments/paymentRoutes.js' )( app, settings ); // payment routes - console.log( '[ Server ] loading plugins' ); - require( './backend/plugins/pluginLoader.js' )( app, settings ); + require( './backend/plugins/pluginLoader.js' )( app, settings ); // plugin loader } else { require( './setup/setupRoutes.js' )( app, settings ); // setup routes file = path.join( __dirname + '/webapp/setup/dist/index.html' ); } -// TODO: load dynamically -// require( './backend/plugins/payments/stripe/stripeRoutes.js' )( app, settings ); // stripe routes -// require( './backend/plugins/payments/payrexx/payrexxRoutes.js' )( app, settings ); // payrexx routes - app.use( ( request, response ) => { response.sendFile( file ); } ); diff --git a/src/server/backend/db/jsondb.js b/src/server/backend/db/jsondb.js index cd0b804..73c2eb2 100644 --- a/src/server/backend/db/jsondb.js +++ b/src/server/backend/db/jsondb.js @@ -15,8 +15,9 @@ class JSONDB { this.db = {}; } - connect ( ) { + connect () { this.db = JSON.parse( fs.readFileSync( path.join( __dirname + '/data/db.json' ) ) ); + this.db[ 'libreevent_temp' ] = {}; setInterval( async () => { fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) ); }, 10000 ); @@ -24,6 +25,16 @@ class JSONDB { return 'connection'; } + async resetDB () { + this.db = {}; + fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) ); + } + + async setupDB () { + this.db = { 'libreevent_temp': {}, 'libreevent_admin': {}, 'libreevent_orders': {}, 'libreevent_users': {} }; + fs.writeFile( path.join( __dirname + '/data/db.json' ), JSON.stringify( this.db ) ); + } + query ( operation, table ) { return new Promise( ( resolve, reject ) => { /* @@ -67,17 +78,18 @@ class JSONDB { - checkDataAvailability: - operation.property (the column to search for the value), - operation.searchQuery (the value to search for [will be sanitised by method]) - - - fullCustomCommand: - - operation.query (the SQL instruction to be executed) --> NOTE: This command will not be sanitised, so use only with proper sanitisation! */ if ( operation.command === 'getAllData' ) { resolve( this.db[ table ] ); } else if ( operation.command === 'getFilteredData' ) { - // - } else if ( operation.command === 'fullCustomCommand' ) { - // + let ret = {}; + for ( let entry in this.db[ table ] ) { + if ( this.db[ table ][ entry ][ operation.property ] == operation.searchQuery ) { + ret[ entry ] = this.db[ table ][ entry ]; + } + } + return ret; } else if ( operation.command === 'addData' ) { // } else if ( operation.command === 'updateData' ) { @@ -101,4 +113,6 @@ class JSONDB { } } ); } -} \ No newline at end of file +} + +module.exports = JSONDB; \ No newline at end of file diff --git a/src/server/backend/db/mysqldb.js b/src/server/backend/db/mysqldb.js index 6ddce97..db942c3 100644 --- a/src/server/backend/db/mysqldb.js +++ b/src/server/backend/db/mysqldb.js @@ -20,12 +20,20 @@ class SQLDB { } connect ( ) { + const self = this; this.sqlConnection.connect( function( err ) { if ( err ) { console.error( '[ SQL ]: An error ocurred whilst connecting: ' + err.stack ); return; } console.log( '[ SQL ] Connected to database successfully' ); + self.sqlConnection.query( 'TRUNCATE libreevent_temp;', error => { + if ( error ) { + console.error( '[ SQL ] Unable to truncate libreevent_temp table due to the following error: ' + error.code ); + } else { + console.log( '[ SQL ] Truncated temporary data table successfully' ); + } + } ); return 'connection'; } ); } diff --git a/src/server/backend/plugins/manager.js b/src/server/backend/plugins/manager.js index 9c691f0..43673bf 100644 --- a/src/server/backend/plugins/manager.js +++ b/src/server/backend/plugins/manager.js @@ -14,13 +14,11 @@ class PluginManager { constructor () {} - install ( plugin ) { + loadSettings ( plugin ) { } - update () {} - - uninstall ( plugin ) { + saveSettings ( plugin, settings ) { } } diff --git a/src/server/backend/plugins/pluginLoader.js b/src/server/backend/plugins/pluginLoader.js index 04a29f5..c7b07a2 100644 --- a/src/server/backend/plugins/pluginLoader.js +++ b/src/server/backend/plugins/pluginLoader.js @@ -11,11 +11,11 @@ const fs = require( 'fs' ); const path = require( 'path' ); module.exports = ( app, settings ) => { + console.log( '\n\n[ Plugin Loader ] Loading plugins\n' ); let otherPlugins = fs.readdirSync( path.join( __dirname + '/others' ) ); - console.log( '\n\n' ); for ( let plugin in otherPlugins ) { - console.log( '[ Plugin Loader ] Loaded plugin "' + otherPlugins[ plugin ] + '"' ); require( './others/' + otherPlugins[ plugin ] + '/' + otherPlugins[ plugin ] + 'Routes.js' )( app, settings ); + console.log( '[ Plugin Loader ] Loaded plugin "' + otherPlugins[ plugin ] + '"' ); } require( './payments/' + settings.payments + '/' + settings.payments + 'Routes.js' )( app, settings );