diff --git a/app.js b/app.js index 661f7dffd..61b9422fc 100644 --- a/app.js +++ b/app.js @@ -72,38 +72,4 @@ require('./services/sync'); // triggers backup timer require('./services/backup'); -if (utils.isElectron()) { - const ipcMain = require('electron').ipcMain; - - ipcMain.on('server-request', (event, arg) => { - const req = {}; - req.url = arg.url; - req.method = arg.method; - req.body = arg.data; - req.headers = arg.headers; - - const res = { - statusCode: 200 - }; - - res.setHeader = function() { - - }; - - res.status = function(statusCode) { - res.statusCode = statusCode; - }; - - res.send = function(obj) { - event.sender.send('server-response', { - requestId: arg.requestId, - statusCode: res.statusCode, - body: obj - }); - }; - - return app._router.handle(req, res, () => {}); - }); -} - module.exports = app; \ No newline at end of file diff --git a/bin/www b/bin/www index d24928e78..7c6a49396 100755 --- a/bin/www +++ b/bin/www @@ -17,6 +17,7 @@ const config = require('../services/config'); const log = require('../services/log'); const app_info = require('../services/app_info'); const messaging = require('../services/messaging'); +const utils = require('../services/utils'); const port = normalizePort(config['Network']['port'] || '3000'); app.set('port', port); @@ -54,6 +55,11 @@ httpServer.on('listening', onListening); messaging.init(httpServer); +if (utils.isElectron()) { + const electronRouting = require('../routes/electron'); + electronRouting(app); +} + /** * Normalize a port into a number, string, or false. */ diff --git a/routes/api/migration.js b/routes/api/migration.js index b41c9f4b4..cae0c1c4d 100644 --- a/routes/api/migration.js +++ b/routes/api/migration.js @@ -6,14 +6,14 @@ const auth = require('../../services/auth'); const options = require('../../services/options'); const migration = require('../../services/migration'); -router.get('', auth.checkApiAuthWithoutMigration, async (req, res, next) => { +router.get('', auth.checkApiAuthForMigrationPage, async (req, res, next) => { res.send({ db_version: parseInt(await options.getOption('db_version')), app_db_version: migration.APP_DB_VERSION }); }); -router.post('', auth.checkApiAuthWithoutMigration, async (req, res, next) => { +router.post('', auth.checkApiAuthForMigrationPage, async (req, res, next) => { const migrations = await migration.migrate(); res.send({ diff --git a/routes/electron.js b/routes/electron.js new file mode 100644 index 000000000..8082c9b6d --- /dev/null +++ b/routes/electron.js @@ -0,0 +1,33 @@ +const ipcMain = require('electron').ipcMain; + +function init(app) { + ipcMain.on('server-request', (event, arg) => { + const req = {}; + req.url = arg.url; + req.method = arg.method; + req.body = arg.data; + req.headers = arg.headers; + + const res = { + statusCode: 200 + }; + + res.setHeader = function() {}; + + res.status = function(statusCode) { + res.statusCode = statusCode; + }; + + res.send = function(obj) { + event.sender.send('server-response', { + requestId: arg.requestId, + statusCode: res.statusCode, + body: obj + }); + }; + + return app._router.handle(req, res, () => {}); + }); +} + +module.exports = init; \ No newline at end of file diff --git a/routes/migration.js b/routes/migration.js index 00bfbf7a2..7f1b4e0e0 100644 --- a/routes/migration.js +++ b/routes/migration.js @@ -4,7 +4,7 @@ const express = require('express'); const router = express.Router(); const auth = require('../services/auth'); -router.get('', auth.checkAuthWithoutMigration, (req, res, next) => { +router.get('', auth.checkAuthForMigrationPage, (req, res, next) => { res.render('migration', {}); }); diff --git a/services/auth.js b/services/auth.js index 722ee243d..f97f39922 100644 --- a/services/auth.js +++ b/services/auth.js @@ -15,7 +15,7 @@ async function checkAuth(req, res, next) { } } -async function checkAuthWithoutMigration(req, res, next) { +async function checkAuthForMigrationPage(req, res, next) { if (!req.session.loggedIn && !utils.isElectron()) { res.redirect("login"); } @@ -36,7 +36,7 @@ async function checkApiAuth(req, res, next) { } } -async function checkApiAuthWithoutMigration(req, res, next) { +async function checkApiAuthForMigrationPage(req, res, next) { if (!req.session.loggedIn && !utils.isElectron()) { res.status(401).send("Not authorized"); } @@ -47,7 +47,7 @@ async function checkApiAuthWithoutMigration(req, res, next) { module.exports = { checkAuth, - checkAuthWithoutMigration, + checkAuthForMigrationPage, checkApiAuth, - checkApiAuthWithoutMigration + checkApiAuthForMigrationPage }; \ No newline at end of file