MagicMirror/js/server.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

/* Magic Mirror
* Server
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
2016-04-05 14:35:11 -04:00
var express = require("express");
var app = require("express")();
var server = require("http").Server(app);
var io = require("socket.io")(server);
var path = require("path");
2016-09-29 17:26:32 +02:00
var ipfilter = require("express-ipfilter").IpFilter;
var fs = require("fs");
var helmet = require("helmet");
var Server = function(config, callback) {
console.log("Starting server op port " + config.port + " ... ");
2016-11-25 20:14:34 -03:00
server.listen(config.port, config.address ? config.address : null);
2016-09-29 17:07:22 +02:00
app.use(function(req, res, next) {
2016-09-29 17:26:32 +02:00
var result = ipfilter(config.ipWhitelist, {mode: "allow", log: false})(req, res, function(err) {
2016-09-29 17:07:22 +02:00
if (err === undefined) {
return next();
}
2016-09-29 17:55:32 +02:00
console.log(err.message);
2016-09-29 17:07:22 +02:00
res.status(403).send("This device is not allowed to access your mirror. <br> Please check your config.js or config.js.sample to change this.");
});
});
app.use(helmet());
2016-09-29 17:07:22 +02:00
2016-04-05 14:35:11 -04:00
app.use("/js", express.static(__dirname));
app.use("/config", express.static(path.resolve(__dirname + "/../config")));
app.use("/css", express.static(path.resolve(__dirname + "/../css")));
app.use("/fonts", express.static(path.resolve(__dirname + "/../fonts")));
app.use("/modules", express.static(path.resolve(__dirname + "/../modules")));
app.use("/vendor", express.static(path.resolve(__dirname + "/../vendor")));
2016-05-11 12:38:41 +02:00
app.use("/translations", express.static(path.resolve(__dirname + "/../translations")));
app.get("/version", function(req,res) {
res.send(global.version);
});
2016-04-05 14:35:11 -04:00
app.get("/", function(req, res) {
var html = fs.readFileSync(path.resolve(__dirname + "/../index.html"), {encoding: "utf8"});
html = html.replace("#VERSION#", global.version);
res.send(html);
});
2016-04-05 14:35:11 -04:00
if (typeof callback === "function") {
callback(app, io);
}
};
module.exports = Server;