forked from steemit/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_render.jsx
92 lines (84 loc) · 3.46 KB
/
app_render.jsx
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
import React from 'react';
import { renderToString } from 'react-dom/server';
import ServerHTML from './server-html';
import universalRender from '../shared/UniversalRender';
import models from 'db/models';
const DB_RECONNECT_TIMEOUT = process.env.NODE_ENV === 'development' ? 1000 * 60 * 60 : 1000 * 60 * 10;
async function appRender(ctx) {
const store = {};
try {
const offchain = {
csrf: ctx.csrf,
flash: ctx.flash,
new_visit: ctx.session.new_visit,
account: ctx.session.a,
config: $STM_Config
};
const user_id = ctx.session.user;
if (user_id) {
let user = null;
if (appRender.dbStatus.ok || (new Date() - appRender.dbStatus.lastAttempt) > DB_RECONNECT_TIMEOUT) {
try {
user = await models.User.findOne({
attributes: ['name', 'email', 'picture_small'],
where: {id: user_id},
include: [{model: models.Account, attributes: ['name']}],
logging: false
});
appRender.dbStatus = {ok: true};
} catch (e) {
appRender.dbStatus = {ok: false, lastAttempt: new Date()};
console.error('WARNING! mysql query failed: ', e.toString());
offchain.serverBusy = true;
}
} else {
offchain.serverBusy = true;
}
if (user) {
let account = null;
if (user.Accounts && user.Accounts.length > 0) {
account = user.Accounts[user.Accounts.length - 1].name;
}
offchain.user = {
id: user_id,
name: user.name,
email: user.email,
picture: user.picture_small,
account
}
}
}
if (ctx.session.arec) {
const account_recovery_record = await models.AccountRecoveryRequest.findOne({
attributes: ['id', 'account_name', 'status', 'provider'],
where: {id: ctx.session.arec, status: 'confirmed'}
});
if (account_recovery_record) {
offchain.recover_account = account_recovery_record.account_name;
}
}
const { body, title, statusCode, meta } = await universalRender({location: ctx.request.url, store, offchain});
// Assets name are found in `webpack-stats` file
const assets_filename = process.env.NODE_ENV === 'production' ? 'tmp/webpack-stats-prod.json' : 'tmp/webpack-stats-dev.json';
const assets = require(assets_filename);
// Don't cache assets name on dev
if (process.env.NODE_ENV === 'development') {
delete require.cache[require.resolve(assets_filename)];
}
const props = {body, assets, title, meta};
ctx.status = statusCode;
ctx.body = '<!DOCTYPE html>' + renderToString(<ServerHTML { ...props } />);
} catch (err) {
// Render 500 error page from server
const { error, redirect } = err;
if (error) throw error;
// Handle component `onEnter` transition
if (redirect) {
const { pathname, search } = redirect;
ctx.redirect(pathname + search);
}
throw err;
}
}
appRender.dbStatus = {ok: true};
module.exports = appRender;