From a34a5c4db087ae7a17261e2053d0ba2bdc8fc08f Mon Sep 17 00:00:00 2001 From: Michael Teeuw Date: Fri, 8 Apr 2016 22:16:22 +0200 Subject: [PATCH] Separate server core from Electron. --- README.md | 5 +- js/app.js | 133 ++++++++++++++++++++++++++++++++++++++++++++ js/electron.js | 98 +++----------------------------- serveronly/index.js | 5 ++ 4 files changed, 149 insertions(+), 92 deletions(-) create mode 100644 js/app.js create mode 100644 serveronly/index.js diff --git a/README.md b/README.md index aea20319..f2c88720 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ This version of the Magic Mirror software focusses on a modular plugin system. B 3. Give the installer permission to run: `sudo chmod +x install.sh` 4. Start the installer: `sudo ./install.sh` - #### Manual Installation 1. Download the latest Node.js version: @@ -32,6 +31,10 @@ This version of the Magic Mirror software focusses on a modular plugin system. B **Important:** `npm start` does **not** work via SSH, use `DISPLAY=:0 nohup npm start &` instead. This starts the mirror on the remote display. +#### Server only + +In some cases, you want to start the application without an actual app window. In this case, exectute the following command from the MagicMirror folder: `node serveronly`. This will start the server, after which you can open the application in your browser of choice. + ## Configuration 1. Duplicate `config/config.js.sample` to `config/config.js`. diff --git a/js/app.js b/js/app.js new file mode 100644 index 00000000..8257bcd0 --- /dev/null +++ b/js/app.js @@ -0,0 +1,133 @@ +/* Magic Mirror + * The Core App (Server) + * + * By Michael Teeuw http://michaelteeuw.nl + * MIT Licensed. + */ + +var fs = require("fs"); +var Server = require(__dirname + "/server.js"); +var defaultModules = require(__dirname + "/../modules/default/defaultmodules.js"); +var path = require("path"); + +/* App - The core app. + */ +var App = function() { + var nodeHelpers = []; + + /* loadConfig(callback) + * Loads the config file. combines it with the defaults, + * and runs the callback with the found config as argument. + * + * argument callback function - The callback function. + */ + + var loadConfig = function(callback) { + console.log("Loading config ..."); + var defaults = require(__dirname + "/defaults.js"); + var configFilename = path.resolve(__dirname + "/../config/config.js"); + try { + fs.accessSync(configFilename, fs.F_OK); + var c = require(configFilename); + var config = Object.assign(defaults, c); + callback(config); + } catch (e) { + console.error('WARNING! Could not find config. Please create one.'); + callback(defaults); + } + }; + + /* loadModule(module) + * Loads a specific module. + * + * argument module string - The name of the module (including subpath). + */ + var loadModule = function(module) { + + var elements = module.split("/"); + var moduleName = elements[elements.length - 1]; + var moduleFolder = __dirname + "/../modules/" + module; + + if (defaultModules.indexOf(moduleName) !== -1) { + moduleFolder = __dirname + "/../modules/default/" + module; + } + + var helperPath = moduleFolder + "/node_helper.js"; + + var loadModule = true; + try { + fs.accessSync(helperPath, fs.R_OK); + } catch (e) { + loadModule = false; + console.log("No helper found for module: " + moduleName + "."); + } + + if (loadModule) { + var Module = require(helperPath); + var m = new Module(); + m.setName(moduleName); + m.setPath(path.resolve(moduleFolder)); + nodeHelpers.push(m); + } + }; + + /* loadModules(modules) + * Loads all modules. + * + * argument module string - The name of the module (including subpath). + */ + var loadModules = function(modules) { + console.log("Loading module helpers ..."); + + for (var m in modules) { + loadModule(modules[m]); + } + + console.log("All module helpers loaded."); + }; + + /* start(callback) + * This methods starts the core app. + * It loads the config, then it loads all modules. + * When it's done it executs the callback with the config as argument. + * + * argument callback function - The callback function. + */ + this.start = function(callback) { + + loadConfig(function(c) { + config = c; + + var modules = []; + + for (var m in config.modules) { + var module = config.modules[m]; + if (modules.indexOf(module.module) === -1) { + modules.push(module.module); + } + } + + loadModules(modules); + + var server = new Server(config, function(app, io) { + console.log("Server started ..."); + + for (var h in nodeHelpers) { + var nodeHelper = nodeHelpers[h]; + nodeHelper.setExpressApp(app); + nodeHelper.setSocketIO(io); + nodeHelper.start(); + } + + console.log("Sockets connected & modules started ..."); + + if (typeof callback === 'function') { + callback(config); + } + + }); + }); + }; +}; + +module.exports = new App(); \ No newline at end of file diff --git a/js/electron.js b/js/electron.js index 883a35bc..5de41003 100755 --- a/js/electron.js +++ b/js/electron.js @@ -1,13 +1,8 @@ "use strict"; -//load modules -const walk = require("walk"); -const fs = require("fs"); const Server = require(__dirname + "/server.js"); -const spawn = require("child_process").spawn; const electron = require("electron"); -const defaultModules = require(__dirname + "/../modules/default/defaultmodules.js"); -const path = require("path"); +const core = require(__dirname + "/app.js"); // Config var config = {}; @@ -16,8 +11,6 @@ const app = electron.app; // Module to create native browser window. const BrowserWindow = electron.BrowserWindow; -var nodeHelpers = []; - // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. let mainWindow; @@ -42,89 +35,6 @@ function createWindow() { }); } -function loadConfig(callback) { - console.log("Loading config ..."); - var defaults = require(__dirname + "/defaults.js"); - var configFilename = __dirname + "/../config/config.js"; - - try { - fs.accessSync(configFilename, fs.R_OK); - var c = require(configFilename); - var config = Object.assign(defaults, c); - callback(config); - } catch (e) { - callback(defaults); - } -} - -function loadModule(module) { - - var elements = module.split("/"); - var moduleName = elements[elements.length - 1]; - var moduleFolder = __dirname + "/../modules/" + module; - - if (defaultModules.indexOf(moduleName) !== -1) { - moduleFolder = __dirname + "/../modules/default/" + module; - } - - var helperPath = moduleFolder + "/node_helper.js"; - - var loadModule = true; - try { - fs.accessSync(helperPath, fs.R_OK); - } catch (e) { - loadModule = false; - console.log("No helper found for module: " + moduleName + "."); - } - - if (loadModule) { - var Module = require(helperPath); - var m = new Module(); - m.setName(moduleName); - m.setPath(path.resolve(moduleFolder)); - nodeHelpers.push(m); - } -} - -function loadModules(modules) { - console.log("Loading module helpers ..."); - - for (var m in modules) { - loadModule(modules[m]); - } - - console.log("All module helpers loaded."); -} - -loadConfig(function(c) { - config = c; - - var modules = []; - - for (var m in config.modules) { - var module = config.modules[m]; - if (modules.indexOf(module.module) === -1) { - modules.push(module.module); - } - } - - loadModules(modules); - - var server = new Server(config, function(app, io) { - console.log("Server started ..."); - - for (var h in nodeHelpers) { - var nodeHelper = nodeHelpers[h]; - nodeHelper.setExpressApp(app); - nodeHelper.setSocketIO(io); - nodeHelper.start(); - } - - console.log("Sockets connected & modules started ..."); - - }); -}); - // This method will be called when Electron has finished // initialization and is ready to create browser windows. app.on("ready", function() { @@ -148,3 +58,9 @@ app.on("activate", function() { createWindow(); } }); + +// Start the core application. +// This starts all node helpers and starts the webserver. +core.start(function(c) { + config = c; +}); diff --git a/serveronly/index.js b/serveronly/index.js new file mode 100644 index 00000000..3b8e0f16 --- /dev/null +++ b/serveronly/index.js @@ -0,0 +1,5 @@ +var app = require('../js/app.js'); +app.start(function(config) { + console.log(''); + console.log('Ready to go! Please point your browser to: http://localhost:' + config.port); +});