-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
95 lines (80 loc) · 2.97 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
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
// add http module and client
var http = require('http');
var express = require('express');
var request = require('request');
var tokens = require('./tokens');
var callback = require('./callback');
var settings = require('./settings');
var ua = require('universal-analytics');
//set the global variable to hold the time to next match, last phrase that Moubot said etc.
global.nextMatch = null;
global.nextOpponent = null;
global.lastPhrase = null;
global.lastServerStart = null;
//let the server port be configurable. it really doesn't matter since this
//is a listening port. Moubot v1 does not listen.
var PORT = settings.serverPort;
//initialize Google Analytics
var visitor = ua(settings.GA);
//initiate the express web app
var app = express();
app.use(express.static(__dirname + '/web'));
app.use(ua.middleware(settings.GA));
app.use(allowCrossDomain);
//function to help with cross-site scripting
function allowCrossDomain(req, res, next) {
res.setHeader('Access-Control-Allow-Methods', '*');
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
if (req.method === 'OPTIONS') {
res.send(200);
} else {
next();
}
}
app.all('/', allowCrossDomain);
//api call that returns the infomation about the next match. this needs to
//be tidyed up.
app.get('/api', function(req, res){
//google pageview tracking
visitor.pageview("/api").send();
res.send('Next Manchester United match is ' + (global.nextMatch || 'not scheduled') + '.');
});
//early slack api. only knows how to respond with the time left until the next match
app.get('/api/slack', function(req, res){
var originHost = req.headers.host;
//exclude local api requests from Google Analytics tracking
if(settings.gaIgnoreHosts.indexOf(req.header.host)!=-1){
console.log('tracking request in analytics.');
visitor.pageview("/api/slack").send();
}
//construc the response
var jsonResponse = {};
jsonResponse.text = 'Next Manchester United match is ' + (global.nextMatch || 'not scheduled' + '.');
jsonResponse.nextMatchDate = global.nextMatch;
jsonResponse.nextOpponent = global.nextOpponent;
jsonResponse.lastPhrase = global.lastPhrase;
jsonResponse.lastServerStart = global.lastServerStart;
//console.log(req);
res.send(jsonResponse);
});
//let's define options for our recurrent http request
var options = {
url: settings.resultsEndpoint,
headers: {
'X-Auth-Token' : process.env.resultsToken
}
};
var server = app.listen(process.env.PORT || PORT, function(){
console.log("Server started at localhost:%s", PORT);
global.lastServerStart = new Date();
//keep a track of last matchday commented, to avoid duplicates.
//this needs refactoring
global.lastPreMatchComment = null;
global.lastPostMatchComment = null;
//set up a timer.
setInterval(function(){
console.log("Tick...next match is " + (nextMatch || "not scheduled."));
request(options, callback.callback);
}, settings.pingInteralInMilliseconds);
});