-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathapp.js
More file actions
90 lines (74 loc) 路 2.68 KB
/
Copy pathapp.js
File metadata and controls
90 lines (74 loc) 路 2.68 KB
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
import fs from 'fs';
import express from 'express';
import cookieParser from 'cookie-parser';
import Handlebars from 'handlebars';
import paths from '../../scripts/paths';
import createSsrHandler from './handlers/createSsrHandler';
import createAmpHandler from './handlers/createAmpHandler';
import steemAPI from './steemAPI';
const indexPath = `${paths.templates}/index.hbs`;
const indexHtml = fs.readFileSync(indexPath, 'utf-8');
const template = Handlebars.compile(indexHtml);
const ampIndexPath = `${paths.templates}/amp_index.hbs`;
const ampIndexHtml = fs.readFileSync(ampIndexPath, 'utf-8');
const ampTemplate = Handlebars.compile(ampIndexHtml);
const ssrHandler = createSsrHandler(template);
const ampHandler = createAmpHandler(ampTemplate);
const CACHE_AGE = 1000 * 60 * 60 * 24 * 7;
const app = express();
const IS_DEV = process.env.NODE_ENV === 'development';
app.use(cookieParser());
if (IS_DEV) {
app.use(express.static(paths.publicRuntime(), { index: false }));
} else {
app.use(express.static(paths.buildPublicRuntime(), { maxAge: CACHE_AGE, index: false }));
}
app.get('/callback', (req, res) => {
const accessToken = req.query.access_token;
const expiresIn = req.query.expires_in;
const state = req.query.state;
const next = state && state[0] === '/' ? state : '/';
if (accessToken && expiresIn) {
res.cookie('access_token', accessToken, { maxAge: expiresIn * 1000 });
res.redirect(next);
} else {
res.status(401).send({ error: 'access_token or expires_in Missing' });
}
});
app.get('/i/@:referral', async (req, res) => {
try {
const { referral } = req.params;
const accounts = await steemAPI.sendAsync('get_accounts', [[referral]]);
if (accounts[0]) {
res.cookie('referral', referral, { maxAge: 86400 * 30 * 1000 });
res.redirect('/');
}
} catch (err) {
res.redirect('/');
}
});
app.get('/i/:parent/@:referral/:permlink', async (req, res) => {
try {
const { parent, referral, permlink } = req.params;
const content = await steemAPI.sendAsync('get_content', [referral, permlink]);
if (content.author) {
res.cookie('referral', referral, { maxAge: 86400 * 30 * 1000 });
res.redirect(`/${parent}/@${referral}/${permlink}`);
} else {
res.redirect('/');
}
} catch (err) {
res.redirect('/');
}
});
app.get('/@:author/:permlink/amp', ampHandler);
app.get('/:category/@:author/:permlink/amp', (req, res) => {
const { author, permlink } = req.params;
res.redirect(301, `/@${author}/${permlink}/amp`);
});
app.get('/:category/@:author/:permlink', (req, res) => {
const { author, permlink } = req.params;
res.redirect(301, `/@${author}/${permlink}`);
});
app.get('/*', ssrHandler);
export default app;