-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
91 lines (75 loc) · 3 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
'use strict';
var _ = require('underscore'),
ccb = require('./lib/ccb'),
express = require('express'),
kraken = require('kraken-js');
var options, app;
/*
* Create and configure application. Also exports application instance for use by tests.
* See https://github.com/krakenjs/kraken-js#options for additional configuration options.
*/
options = {
onconfig: function (config, next) {
/*
* Add any additional config setup or overrides here. `config` is an initialized
* `confit` (https://github.com/krakenjs/confit/) configuration object.
*/
next(null, config);
}
};
app = module.exports = express();
app.use(kraken(options));
if(process.env.NODE_ENV === 'production') {
app.use(function (req, res, next) { //check requests come from a valid Slack integration
if (req.query.token !== process.env.slack_token) {
return res.send("It doesn't look like you're authorized");
} else {
next();
}
});
}
app.use(function (req, res, next) { //check the parameters are valid
if(!req.query.command || !req.query.text) { //we don't care what command slack integration, typicaly its /ccb
return res.send("Something is missing"); //should only happen if the server is hit by anyone but slack
}
//ex: people since last week {"delimiter":"," , "columns":["email"]}
var text = decodeURI(req.query.text).split(/ (.+)/); //split between the first space in the text to get the function
res.locals.function = text[0];
if(!res.locals.function || ccb.function.indexOf(res.locals.function) === -1) {
return res.send("Sorry I don't understand " + res.locals.function);
}
//ex: since last week {"delimiter":"," , "columns":["email"]}
text = text[1] ? text[1].split(/({.*})/) : {}; //split out any options that were passed in the message
res.locals.options = {
break: '\n', //the character ending each person when turned to a string
delimiter: ' - ' //the character between each field in a person when turned to a string
};
if(text.length > 1) { //if options were provided
try {
_.extend(res.locals.options, JSON.parse(text[1])); //attempt to parse the json and override the default options
}
catch(e) {
return res.send("Your options aren't quite right");
}
}
//ex: since last week
res.locals.data = text[0] ? text[0].trim() : ""; //after everything else is parsed out, save the data
if(!res.locals.data) {
return res.send("Did you forget to type something?");
}
next();
});
app.use(function (req, res, next) { //for outputting kml as a file
res.kml = function (code, object, filename) {
this.header('Content-Type', 'application/vnd.google-earth.kml+xml');
this.header('Content-Disposition', 'attachment; filename="' + filename + '.kml"');
this.header('Content-Length', Buffer.byteLength(object));
// 200's must be returned for the data to be processed
return this.status(200).send(object);
};
return next();
});
app.on('start', function () {
console.log('Application ready to serve requests.');
console.log('Environment: %s', app.kraken.get('env:env'));
});