-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebserver.js
129 lines (111 loc) · 3.16 KB
/
webserver.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
122
123
124
125
126
127
128
129
'use strict';
import async from 'async';
import lodash from 'lodash';
import express from 'express';
import bunyan from 'bunyan';
import React from 'react';
import redisModule from 'redis';
import redisPromisified from './services/redisPromisified';
import postService from './services/postService';
import Post from './components/Post';
import Posts from './components/Posts';
import PostSummary from './components/PostSummary';
const log = bunyan.createLogger({name: 'blogdemo:webserver'});
const app = express();
const redisClient = redisModule.createClient();
const marked = require('marked');
redisClient.on('error', err => {
log.error('error', err);
});
global.log = log;
global.redisClient = redisClient;
function handleError(res, error) {
log.error('error', error);
if (error instanceof Error) {
log.error('error stack', error.stack);
}
res.status(500).send(error);
}
function getHelp(req, res) {
try {
res.set('Content-Type', "text/html");
fs.readFile('README.md', function (err, content) {
if (content) {
res.send(marked(content.toString()));
} else {
res.send('no help');
}
});
} catch (error) {
handleError(res, error);
}
}
function appLogger(req, res, next) {
log.debug('app', req.url);
next();
}
function start() {
app.use(appLogger);
app.get('/posts', getPosts);
app.get('/post/:id', getPostId);
app.listen(process.env.APP_PORT);
}
var PostPage = require('./components/PostPage');
function getPostId(req, res) {
postService.getPostPromise(req.params.id).
then(function(post) {
var html = React.renderToString(
React.createElement(PostPage, {post: post}));
res.set('Content-Type', 'text/html');
res.send(html);
});
}
const { zrevrange, lrange } = redisPromisified;
async function getPosts(req, res) {
if (!req.query.limit) {
req.query.limit = 10;
}
var ids;
if (req.query.q === 'sorted') {
ids = await zrevrange(
'post:sorted', 0, req.query.limit);
} else {
ids = await lrange(
'post:list', 0, req.query.limit);
}
sendPosts(res, ids);
}
const { hgetall } = redisPromisified;
async function sendPosts(res, ids) {
log.info('sendPosts', ids);
let posts = await* ids.map(async (id) =>
hgetall('post:table:' + id));
let html = React.renderToString(
React.createElement(Posts, {posts}));
res.set('Content-Type', 'text/html');
res.send(html);
}
function getPostsSorted(req, res) {
redisClient.zrevrange(
'post:sorted:published', 0, req.query.limit || 10,
(err, ids) => {
if (err) {
res.status(500).send(err);
} else {
async.map(ids, (id, cb) => {
redisClient.hgetall('post:table:' + id, cb);
}, (err, posts) => {
if (err) {
res.status(500).send(err);
} else {
let html = React.renderToString(
React.createElement(Posts, {posts}));
res.set('Content-Type', 'text/html');
res.send(html);
}
});
}
}
);
}
start(process.env);