-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
104 lines (91 loc) · 3.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"use strict";
const config = require('./config')
const DeparturesBoard = require('./lib/darwinDeparturesBoard')
const TFLAPI = require('./lib/tflAPI')
const fs = require('fs')
const path = require('path')
const bunyan = require('bunyan')
const bunyanRequest = require('bunyan-request');
const express = require("express");
const bodyParser = require('body-parser')
const log = bunyan.createLogger({
name: 'slms-dashboard'
});
const app = express();
app.use(bodyParser.json())
const server = require('http').createServer(app);
const io = require('socket.io')(server);
var router = express.Router();
var viewPath = __dirname + '/views/';
var activeWidgets = []
//socketio stuff
io.on('connection', function(client) {
console.log('Client connected...');
client.on('join', function(data) {
// client connected
});
})
function sendAlert(message)
{
io.emit('alert', {
messageType: 'alert-warning',
message: message,
messageTimeout: 5000
})
}
router.get("/", (req, res) => {
res.sendFile(path.join(viewPath, "index.html"));
});
// load all of the available widgets
fs.readdir(path.join(__dirname, '/widgets'), (err, entries) => {
if (err) {
log.error("Can't load widgets: " + err)
process.exit(5)
}
entries.forEach((entry) => {
var widgetName = entry
fs.stat(path.join(__dirname, '/widgets/', entry), (err, stats) => {
if (err) {
log.error("Couldn't stat entry " + entry + ": " + err)
}
if (stats.isDirectory()) {
var widgetPath = path.join(__dirname, '/widgets/', entry)
log.info('Found widget:', widgetName, widgetPath)
require(path.join(widgetPath, 'routes.js'))(router)
router.get('/' + widgetName + '/widget.html', function(req, res) {
res.sendFile(path.join(widgetPath, 'widget.html'))
})
router.use('/' + entry + '/', express.static(path.join(widgetPath, 'public')))
router.get('/' + entry + '/.*', express.static(path.join(widgetPath, 'public')))
activeWidgets.push(widgetName)
}
})
})
})
// provide a list of all widgets
router.get('/widgets', function(req, res) {
res.end(JSON.stringify(activeWidgets))
})
router.get('/layout', function(req, res) {
//TODO: make this actually do something :)
// serve a layout structure for the page to render
})
router.post('/event', function (req, res) {
console.log('alert', req.body)
res.end(sendAlert(req.body.name + " has " + req.body.action))
})
/*app.use(bunyanRequest({
logger: bunyan.createLogger({
name: "slms-dashboard-http",
headerName: 'x-request-id'
})
}));*/
app.use('/bower_components', express.static(path.join(__dirname, 'bower_components')))
app.use("/", router);
app.use(express.static('public'))
app.use("*", (req, res) => {
res.sendFile(__dirname + "404.html");
});
server.listen(config.httpPort, () => {
console.log("Live at Port " + config.httpPort);
});