-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (116 loc) · 3.58 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
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
130
131
132
133
134
135
136
137
138
139
var koa = require('koa'),
logger = require('koa-logger'),
path = require('path'),
router = require('koa-router'),
staticCache = require('koa-static-cache'),
error = require('koa-error'),
views = require('co-views'),
parse = require('co-body'),
reds = require('reds'),
search = reds.createSearch('blogposts'),
each = require('co-each'),
querystring = require('querystring'),
db = require('./lib/db'),
render = require('./lib/render'),
message = require('./lib/message'),
app = koa();
// Wrap subsequent middleware in a logger
app.use(logger());
// Wrap subsequent middleware in a logger
app.use(message(app));
// Page wrapper
app.use(function *(next){
yield next;
if (this.body && 'text/html' === this.response.type && 200 === this.response.status) this.body = yield render.assemble(this.body, {messages: this.messages.get()});
});
// Timer
app.use(function *(next){
var start = new Date;
yield next;
var ms = new Date - start;
if (this.body && 'text/html' === this.response.type) this.body = this.body + '<pre>Query took ' + ms + 'ms</pre>';
});
app.use(error({template: __dirname + '/views/error.html'}));
app.use(staticCache(path.join(__dirname, 'public'), {
maxAge: 7 * 24 * 60 * 60,
buffer: true
}));
app.use(router(app));
// Homepage
app.all('/', function *(next) {
this.body = yield render('home');
});
app.resource('posts', {
// GET /posts
index: function *(next) {
var posts = yield db.list();
this.body = yield render('list', {posts: posts });
},
// GET /posts/new
new: function *(next) {
this.body = yield render('new');
},
// POST /posts
create: function *(next) {
var submitted = yield parse(this);
var inserted = yield db.insert(submitted.title, submitted.body);
this.redirect('/posts');
this.messages.add('Your new post was created');
search.index([submitted.title, submitted.body].join(' '), inserted.guid);
},
// GET /posts/:post
show: function *(next) {
var guid = this.params.post;
var post = yield db.find(guid);
if (!post) this.throw(404, 'invalid post id');
this.body = yield render('show', { post: post });
},
// GET /posts/:post/edit
edit: function *(next) {
var guid = this.params.post;
var post = yield db.find(guid);
if (!post) this.throw(404, 'invalid post id');
this.body = yield render('edit', { post: post });
},
// PUT /posts/:post
update: function *(next) {
var submitted = yield parse(this);
if (!submitted.guid) this.throw(404, 'invalid post id');
var post = yield db.find(submitted.guid);
if (!post) this.throw(404, 'invalid post id');
for (var i in submitted) {
post[i] = submitted[i];
}
yield db.update(post);
this.messages.add('Your post was updated');
this.response.status = 200;
search.remove(guid);
search.index([submitted.title, submitted.body].join(' '), submitted.guid);
},
// DELETE /posts/:post
destroy: function *(next) {
var guid = this.params.post;
yield db.remove(guid);
this.messages.add('Your post was deleted');
this.response.status = 200;
search.remove(guid);
}
});
function searchQuery(query) {
return function(cb) {
search.query(query)
.end(cb, 'or');
}
}
app.get('/posts/search/:query', function *(next) {
var ids = yield searchQuery(querystring.unescape(this.params.query));
var matches = yield each(ids, function(guid) {
return db.find(guid);
})
this.type = 'json';
this.body = JSON.stringify(matches);
});
// Serve the app
var port = process.env.PORT || 3000;
app.listen(port);
console.log('listening on port ' + port);