mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-28 12:12:20 +00:00
Separate server core from Electron.
This commit is contained in:
parent
0441b9fb50
commit
a34a5c4db0
@ -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`
|
3. Give the installer permission to run: `sudo chmod +x install.sh`
|
||||||
4. Start the installer: `sudo ./install.sh`
|
4. Start the installer: `sudo ./install.sh`
|
||||||
|
|
||||||
|
|
||||||
#### Manual Installation
|
#### Manual Installation
|
||||||
|
|
||||||
1. Download the latest Node.js version:
|
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.
|
**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
|
## Configuration
|
||||||
|
|
||||||
1. Duplicate `config/config.js.sample` to `config/config.js`.
|
1. Duplicate `config/config.js.sample` to `config/config.js`.
|
||||||
|
133
js/app.js
Normal file
133
js/app.js
Normal file
@ -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();
|
@ -1,13 +1,8 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
//load modules
|
|
||||||
const walk = require("walk");
|
|
||||||
const fs = require("fs");
|
|
||||||
const Server = require(__dirname + "/server.js");
|
const Server = require(__dirname + "/server.js");
|
||||||
const spawn = require("child_process").spawn;
|
|
||||||
const electron = require("electron");
|
const electron = require("electron");
|
||||||
const defaultModules = require(__dirname + "/../modules/default/defaultmodules.js");
|
const core = require(__dirname + "/app.js");
|
||||||
const path = require("path");
|
|
||||||
|
|
||||||
// Config
|
// Config
|
||||||
var config = {};
|
var config = {};
|
||||||
@ -16,8 +11,6 @@ const app = electron.app;
|
|||||||
// Module to create native browser window.
|
// Module to create native browser window.
|
||||||
const BrowserWindow = electron.BrowserWindow;
|
const BrowserWindow = electron.BrowserWindow;
|
||||||
|
|
||||||
var nodeHelpers = [];
|
|
||||||
|
|
||||||
// Keep a global reference of the window object, if you don't, the window will
|
// 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.
|
// be closed automatically when the JavaScript object is garbage collected.
|
||||||
let mainWindow;
|
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
|
// This method will be called when Electron has finished
|
||||||
// initialization and is ready to create browser windows.
|
// initialization and is ready to create browser windows.
|
||||||
app.on("ready", function() {
|
app.on("ready", function() {
|
||||||
@ -148,3 +58,9 @@ app.on("activate", function() {
|
|||||||
createWindow();
|
createWindow();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Start the core application.
|
||||||
|
// This starts all node helpers and starts the webserver.
|
||||||
|
core.start(function(c) {
|
||||||
|
config = c;
|
||||||
|
});
|
||||||
|
5
serveronly/index.js
Normal file
5
serveronly/index.js
Normal file
@ -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);
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user