-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathWebServer.js
104 lines (87 loc) · 2.94 KB
/
WebServer.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';
var portNum = 24123,
_ = require('lodash'),
express = require('express'),
swig = require('swig'),
http = require('http'),
socketio = require('socket.io'),
gadgetCatalogService = require('./modules/gadgetCatalogService.js'),
app = express(),
apiList = [
"./api/gadgetCatalog.js",
"./api/connections.js"
],
exports = {
onClose: _.noop
};
module.exports = exports;
// Changing swig variables to {- ... -} instead of {{ ... }} since AngularJS uses these characters.
swig.setDefaults({
varControls: [ "{-", "-}" ]
});
app.set('port', portNum);
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view cache', false);
swig.setDefaults({ cache: false });
app.use(express.logger());
app.use(express.compress());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.errorHandler());
app.use('/bower', express.static(__dirname + '/bower_components'));
app.use(app.router);
app.get("/", function(req, res) {
res.render("../app/index.html", {});
});
app.use(express.static(__dirname + '/app'));
gadgetCatalogService
.getCatalog(function(err, catalog) {
if(err) {
throw err.errorThrown;
}
else {
_.each(catalog, function(item) {
var list = item.apiList || [];
apiList = apiList.concat(_.map(list, function(fn) {
return item.basePath + "/" + fn;
}));
app.get("/" + item.identifier, function(req, res) {
res.render(
[
"..",
"sql",
item.identifier,
"client",
"index.html"
].join("/"),
{});
});
app.use("/" + item.identifier, express.static([
__dirname,
item.basePath,
'client'
].join('/')));
});
var handlerList = [];
_.each(apiList, function(apiFileName) {
var handler = require(apiFileName).register(app);
if(handler) {
handlerList.push(handler);
}
});
var server = http.createServer(app),
io = socketio.listen(server);
io.set('log level', 1);
server.listen(app.get('port'), function() {
console.log("Listening on port " + app.get('port'));
console.log("http://localhost:" + app.get('port') + "/");
}).on('close', exports.onClose);
_.each(handlerList, function(handler) {
if(_.isFunction(handler.initializeSockets)) {
handler.initializeSockets(io);
}
});
}
});