mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 03:39:55 +00:00
New server route to fetch config
Added a new route to the Express server to supply client with config. Removed the original 'cookie' hack
This commit is contained in:
parent
66f93ee541
commit
1590693547
@ -6,7 +6,7 @@
|
|||||||
(function () {
|
(function () {
|
||||||
const cookie = require("cookie");
|
const cookie = require("cookie");
|
||||||
|
|
||||||
var config = { };
|
var config = {};
|
||||||
|
|
||||||
// Parse command line arguments, if any
|
// Parse command line arguments, if any
|
||||||
var addressIndex = process.argv.indexOf("--address");
|
var addressIndex = process.argv.indexOf("--address");
|
||||||
@ -38,41 +38,38 @@
|
|||||||
// Select http or https module, depending on reqested url
|
// Select http or https module, depending on reqested url
|
||||||
const lib = url.startsWith("https") ? require("https") : require("http");
|
const lib = url.startsWith("https") ? require("https") : require("http");
|
||||||
const request = lib.get(url, (response) => {
|
const request = lib.get(url, (response) => {
|
||||||
// Handle http errors
|
var configData = "";
|
||||||
if (response.statusCode < 200 || response.statusCode > 299) {
|
|
||||||
reject(new Error(`Failed to load page, status code: ${response.statusCode}`));
|
// Gather incomming data
|
||||||
}
|
response.on("data", function(chunk) {
|
||||||
if (response.headers["set-cookie"]) {
|
configData += chunk;
|
||||||
response.headers["set-cookie"].forEach(
|
});
|
||||||
function (cookiestr) {
|
// Resolve promise at the end of the HTTP/HTTPS stream
|
||||||
if (cookiestr.startsWith("config")) {
|
response.on("end", function() {
|
||||||
var cookieString = JSON.parse(cookie.parse(cookiestr)["config"]);
|
resolve(JSON.parse(configData));
|
||||||
resolve(cookieString);
|
});
|
||||||
}
|
});
|
||||||
}
|
|
||||||
);
|
request.on("error", function(error) {
|
||||||
};
|
reject(new Error(`Unable to read config from server (${url} (${error.message}`));
|
||||||
reject(new Error(`Unable to read config cookie from server (${url}`));
|
|
||||||
});
|
});
|
||||||
// Handle connection errors of the request
|
|
||||||
request.on("error", (err) => reject(new Error(`Failed to load page, error message: ${err}`)));
|
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only start the client if a non-local server was provided
|
// Only start the client if a non-local server was provided
|
||||||
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) {
|
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].indexOf(config.address) === -1) {
|
||||||
getServerConfig(`http://${config.address}:${config.port}/`)
|
getServerConfig(`http://${config.address}:${config.port}/config/`)
|
||||||
.then(function (cookieConfig) {
|
.then(function (config) {
|
||||||
// Pass along the server config via an environment variable
|
// Pass along the server config via an environment variable
|
||||||
var env = Object.create( process.env );
|
var env = Object.create(process.env);
|
||||||
var options = { env: env };
|
var options = { env: env };
|
||||||
cookieConfig.address = config.address;
|
config.address = config.address;
|
||||||
cookieConfig.port = config.port;
|
config.port = config.port;
|
||||||
env.config = JSON.stringify(cookieConfig);
|
env.config = JSON.stringify(config);
|
||||||
|
|
||||||
// Spawn electron application
|
// Spawn electron application
|
||||||
const electron = require("electron");
|
const electron = require("electron");
|
||||||
const child = require("child_process").spawn(electron, ["js/electron.js"], options );
|
const child = require("child_process").spawn(electron, ["js/electron.js"], options);
|
||||||
|
|
||||||
// Pipe all child process output to current stdout
|
// Pipe all child process output to current stdout
|
||||||
child.stdout.on("data", function (buf) {
|
child.stdout.on("data", function (buf) {
|
||||||
|
@ -52,6 +52,10 @@ var Server = function(config, callback) {
|
|||||||
res.send(global.version);
|
res.send(global.version);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get("/config", function(req,res) {
|
||||||
|
res.send(config);
|
||||||
|
});
|
||||||
|
|
||||||
app.get("/", function(req, res) {
|
app.get("/", function(req, res) {
|
||||||
var html = fs.readFileSync(path.resolve(global.root_path + "/index.html"), {encoding: "utf8"});
|
var html = fs.readFileSync(path.resolve(global.root_path + "/index.html"), {encoding: "utf8"});
|
||||||
html = html.replace("#VERSION#", global.version);
|
html = html.replace("#VERSION#", global.version);
|
||||||
@ -62,9 +66,6 @@ var Server = function(config, callback) {
|
|||||||
}
|
}
|
||||||
html = html.replace("#CONFIG_FILE#", configFile);
|
html = html.replace("#CONFIG_FILE#", configFile);
|
||||||
|
|
||||||
// Set a temporary cookie called "config" to the JSON encoded config object
|
|
||||||
res.cookie("config", JSON.stringify(config));
|
|
||||||
|
|
||||||
res.send(html);
|
res.send(html);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user