forked from adamjodlowski/tweet-perf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
117 lines (72 loc) · 1.99 KB
/
app.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
/*
* Main app module, contains server routes and basic request-response logic.
*/
var restify = require('restify');
var options = {
serverName: 'Tweet-Perf Server',
accept: [ 'application/json' ]
}
var server = restify.createServer(options);
server.listen(3000);
var engineModule = require('./engine');
var engine = new engineModule.Engine();
//------------------------------------------------------------------------------
/*
* Experimenting with multiple server processes using https://github.com/kriszyp/multi-node
*/
/*var nodes = require("multi-node").listen({
port: 1337,
nodes: 4
}, server);*/
//------------------------------------------------------------------------------
/*
* Obtaining user's timeline, example:
* GET /statuses/user_timeline.json?screen_name=adamus
*/
server.get('/statuses/user_timeline.json', function(req, res) {
if (req.params.screen_name) {
engine.getTweets(req.params.screen_name, function(tweets) {
if (tweets) {
res.send(200, tweets);
} else {
res.send(200, []);
}
});
} else {
res.send(400);
}
});
/*
* Posting a tweet, example:
* POST /statuses/update.json?status=I%20like%20pancakes&screen_name=adamus
*/
server.post('/statuses/update.json', function(req, res) {
if (req.params.screen_name && req.params.status) {
engine.postTweet(req.params.screen_name, req.params.status, function(tweet) {
if (tweet) {
res.send(201, tweet);
} else {
res.send(500);
}
});
} else {
res.send(400);
}
});
/*
* Getting user's timeline (user's own tweets + tweets by users being followed by her/him), example:
* GET /statuses/home_timeline.json?screen_name=adamus
*/
server.get('/statuses/home_timeline.json', function(req, res) {
if (req.params.screen_name) {
engine.getTimeline(req.params.screen_name, function(tweets) {
if (tweets) {
res.send(200, tweets);
} else {
res.send(200, []);
}
});
} else {
res.send(400);
}
});