This repository has been archived by the owner on Feb 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (50 loc) · 1.81 KB
/
server.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
#!/usr/bin/env node
var express = require("express"),
https = require("https"),
fs = require("fs"),
path = require("path"),
bodyParser = require("body-parser"),
nuve = require("./nuve");
var config = require("./config"),
db = require("./db"),
api = require("./api");
var app = express();
nuve.API.init(config.NUVE_SERVICE_ID, config.NUVE_SERVICE_KEY, config.NUVE_HOST);
// Set useful globals
global.cq = {
db: db,
config: config,
nuve: nuve
};
// Configure application
app.use(function(req, res, next) {
// Lock down domains
if (config.ALLOWED_ORIGINS.indexOf(req.headers["host"]) < 0) {
console.log("Blocked connection from: %s. User agent: %s", req.headers["host"], req.headers["user-agent"]);
return res.status(403).send("Invalid origin");
}
console.log("Allowing connection from: %s. User agent: %s", req.headers["host"], req.headers["user-agent"]);
res.header('Access-Control-Allow-Origin', req.headers.host);
res.header('Access-Control-Allow-Methods', 'GET, POST');
res.header('Access-Control-Allow-Headers', 'Content-Type');
return next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/", express.static(path.join(__dirname, 'static')));
// Set https options
var https_options = {
key: fs.readFileSync(config.SSL_KEY),
cert: fs.readFileSync(config.SSL_CERT),
passphrase: config.SSL_PASSPHRASE
};
// Start the secure server
var server = https.createServer(https_options, app).listen(config.APP_PORT, config.APP_IP, function () {
console.log("Starting Conqueror secure server...");
console.log("Listening on %s:%d", config.APP_IP, config.APP_PORT);
});
var io = require("socket.io").listen(server);
io.set("log level", 1);
io.sockets.on("connection", function (socket) {
api(socket);
});