This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
index.js
70 lines (56 loc) · 2.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
/**
* Working PlusPlus++
* Like plusplus.chat, but one that actually works, because you can host it yourself! 😉
*
* @see https://github.com/tdmalone/working-plusplus
* @see https://expressjs.com/en/4x/api.html
* @author Tim Malone <[email protected]>
*/
'use strict';
const app = require( './src/app' ),
slack = require( './src/slack' );
const fs = require( 'fs' ),
mime = require( 'mime' ),
express = require( 'express' ),
bodyParser = require( 'body-parser' ),
slackClient = require( '@slack/client' );
/* eslint-disable no-process-env, no-magic-numbers */
const PORT = process.env.PORT || 80; // Let Heroku set the port.
const SLACK_OAUTH_ACCESS_TOKEN = process.env.SLACK_BOT_USER_OAUTH_ACCESS_TOKEN;
/* eslint-enable no-process-env, no-magic-numbers */
/**
* Starts the server and bootstraps the app.
*
* @param {object} options Optional. Allows passing in replacements for the default Express server
* module (`express` property) and Slack Web API client module (`slack`
* property).
* @returns {http.Server} A Node.js http.Server object as returned by Express' listen method. See
* https://expressjs.com/en/4x/api.html#app.listen and
* https://nodejs.org/api/http.html#http_class_http_server for details.
*/
const bootstrap = ( options = {}) => {
// Allow alternative implementations of both Express and Slack to be passed in.
const server = options.express || express();
slack.setSlackClient( options.slack || new slackClient.WebClient( SLACK_OAUTH_ACCESS_TOKEN ) );
server.use( bodyParser.json() );
server.enable( 'trust proxy' );
server.get( '/', app.handleGet );
server.post( '/', app.handlePost );
// Static assets.
server.get( '/assets/*', ( request, response ) => {
const path = 'src/' + request._parsedUrl.path,
type = mime.getType( path );
response.setHeader( 'Content-Type', type );
response.send( fs.readFileSync( path ) );
});
// Additional routes.
server.get( '/leaderboard', app.handleGet );
return server.listen( PORT, () => {
console.log( 'Listening on port ' + PORT + '.' );
});
}; // Bootstrap.
// If module was called directly, bootstrap now.
if ( require.main === module ) {
bootstrap();
}
module.exports = bootstrap;