-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (69 loc) · 2.24 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
const SlackDatabase = require('./db');
const SlackMessageBot = require('./bot');
const express = require('express')
const morgan = require('morgan')
const app = express()
const process = require('process')
const db = new SlackDatabase();
const cspDirectives = [
"default-src 'self' cdnjs.cloudflare.com fonts.googleapis.com fonts.gstatic.com",
"img-src * data:",
"style-src * data:",
]
const cspString = cspDirectives.join('; ')
app.use(morgan('combined'));
app.use((req,res,next) => {
res.append('Access-Control-Allow-Methods', 'GET');
res.append('Content-Security-Policy', cspString);
next();
})
const api = express.Router();
api.use('/api', api);
api.get('/users', async (req, res) => {
const users = await db.getUsers();
res.append('Cache-Control', 'public, max-age=600')
res.json(users);
});
api.get('/channels', async (req, res) => {
const channels = await db.getChannels();
res.append('Cache-Control', 'public, max-age=600')
res.json(channels);
});
api.get('/channel/:channel/:page', async (req, res) => {
let {channel, page} = req.params;
page = parseInt(page);
const [messages, isFullPage] = await db.getPageMessages(channel, page);
if (isFullPage) {
res.append('Cache-Control', 'public, max-age=3600')
}
res.json(messages);
});
api.get('/search/', async (req, res) => {
let {q} = req.query;
const messages = await db.searchForMessages(q);
res.append('Cache-Control', 'public, max-age=600')
res.json(messages);
});
api.get('/ts/:channel/:ts', async (req, res) => {
let {channel, ts} = req.params;
ts = parseFloat(ts);
const page = await db.tsToPage(channel, ts);
res.json(page);
});
app.use('/api', api);
app.use(express.static('public', {
maxage: '1d',
}))
app.get('/*', (req, res) => {
res.append('Cache-Control', 'public, max-age=86400');
res.sendFile(__dirname + '/public/index.html');
});
(async function () {
await db.connect();
const botToken = process.env.SLACK_BOT_TOKEN;
if (botToken) {
const bot = new SlackMessageBot(db, botToken);
bot.start();
}
app.listen(3000, () => console.log('Listening on port 3000'));
})();