-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
150 lines (116 loc) · 3.7 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
var redis = require('redis')
var multer = require('multer')
var express = require('express')
var fs = require('fs')
var http = require('http')
var httpProxy = require('http-proxy')
var app = express()
// REDIS
var REDISPORT = args[0];
var client = redis.createClient(REDISPORT, '127.0.0.1', {});
var MIRROR_TO = args[1];
var mirrorClient = undefined;
if (MIRROR_TO) {
mirrorClient = redis.createClient(MIRROR_TO, '127.0.0.1', {}) ;
}
///////////// WEB ROUTES
// Add hook to make it easier to get all visited URLS.
app.use(function(req, res, next)
{
console.log(req.method, req.url);
client.lpush("urllist", req.url);
client.ltrim("urllist", 0, 4);
next(); // Passing the request to the next handler in the stack.
});
app.post('/upload',[ multer({ dest: './uploads/'}), function(req, res){
console.log(req.body) // form fields
console.log(req.files) // form files
if( req.files.image )
{
fs.readFile( req.files.image.path, function (err, data) {
if (err) throw err;
var img = new Buffer(data).toString('base64');
console.log("Pushing image");
client.rpush("imageList", img);
if (mirrorClient) {
mirrorClient.rpush("imageList",img);
}
});
}
res.status(204).end()
}]);
app.get('/meow', function(req, res) {
client.rpop("imageList", function(err, imagedata) {
if (err) throw err
res.writeHead(200, {'content-type':'text/html'});
res.write("<h1>\n<img src='data:my_pic.jpg;base64,"+imagedata+"'/>");
res.end();
});
});
app.get('/', function(req, res) {
client.get("number of visits", function(err, value) {
res.send('hello world '+value+'<form action="upload" method="post" enctype="multipart/form-data">Select image to upload:<br/><input type="file" name="image" id="image"><input type="submit" value="Upload Image" name="submit"></form>');
client.incr("number of visits");
});
})
app.get('/recent', function(req, res) {
client.lrange("urllist", 0,10, function(err, value) {
res.send('hello world '+value);
});
})
app.get('/get', function(req, res) {
client.get("mykey", function(err, value) {
if (err) {
console.log(err);
}
if (value) {
client.ttl("mykey", function(err, value){
if (err) {
console.log(err);
}
res.send("This message will self-destruct in "+value +" seconds");
});
} else {
res.send('The clock has begun');
client.set("mykey", "SOMETHING");
client.expire("mykey", 10);
}
});
})
// HTTP SERVER
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});
// HTTP SERVER
var server2 = app.listen(3001, function () {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
var addresses = [
{
host: '127.0.0.1',
port: 3000
},
{
host: '127.0.0.1',
port: 3001
}
];
var proxy = httpProxy.createServer();
http.createServer(function (req, res) {
//
// On each request, get the index of which address to use
// and increment it
client.incr("addressIndex", function(err, value){
console.log(value);
var target = { target: addresses[value % addresses.length] };
//
// ...then proxy to the server whose 'turn' it is...
//
console.log('balancing request to: ', target);
proxy.web(req, res, target);
});
}).listen(80);