-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.js
121 lines (104 loc) · 3.04 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('dotenv').config();
const fs = require('fs');
const express = require('express');
const next = require('next');
const bodyParser = require('body-parser');
const fm = require('front-matter');
const moment = require('moment');
const getSlug = require('speakingurl');
const axios = require('axios');
const getMembersCount = require('./bot');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
let state = {
posts: []
};
getMembersCount(process.env.BOT_TOKEN)
.then(groups => state.groups = groups)
.catch(err => console.log(err));
setInterval(async () => {
state.groups = await getMembersCount(process.env.BOT_TOKEN);
}, 1000 * 60 * 60);
fs.readdir('./posts', 'utf8', (err, files) => {
if (err) throw err;
files.forEach(file => {
fs.readFile(`./posts/${file}`, 'utf8', (err, data) => {
let post = fm(data);
let date = post.attributes.date.split(', ');
date[1] = date[1] - 1;
date = moment(date)
post.attributes.date = date;
post.attributes.displayDate = date.format('MMM D')
state.posts.push(post);
if (state.posts.length === files.length) {
state.posts.sort((a, b) => b.attributes.date - a.attributes.date);
}
});
});
});
const subscribe = (email) => {
const list = process.env.MAILCHIMP_LIST;
const token = process.env.MAILCHIMP_TOKEN;
return new Promise((resolve, reject) => {
if (!email) {
reject('No email address has provided');
}
if (!list || !token) {
reject('API has not been provided.')
}
axios({
method: 'post',
url: `https://us16.api.mailchimp.com/3.0/lists/${list}/members/`,
headers: {
'Content-Type': 'application/json',
'Authorization': `Basic ${token}`,
},
data: {
email_address: email,
status: 'subscribed',
},
})
.then(res => resolve(res))
.catch(err => reject(err))
});
}
app.prepare()
.then(() => {
const server = express();
server.use(bodyParser.urlencoded({
extended: true
}));
server.use(bodyParser.json());
server.use('/static', express.static('static'));
server.use('/.well-known', express.static('.well-known'));
server.get('/', (req, res) => {
return app.render(req, res, '/', state.groups);
});
server.get('/blog/', (req, res) => {
return app.render(req, res, '/blog', state.posts);
});
server.get('/blog/:post', (req, res) => {
const post = state.posts.filter(item =>
getSlug(item.attributes.title) === getSlug(req.params.post)
);
if (post.length < 1) return res.redirect('/404');
return app.render(req, res, '/post', ...post);
});
server.post('/subscription', (req, res) => {
subscribe(req.body.email)
.then(response => res.status(200).send('subsribed!'))
.catch(err => res.status(400).send({message: err}));
});
server.get('/mailto/:mail', (req, res) => {
const { mail } = req.params;
res.redirect(`mailto:${mail}`);
});
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(8168, (err) => {
if (err) throw err;
console.log('> Ready on http://localhost:8168');
});
});