-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
65 lines (61 loc) · 1.34 KB
/
example.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
const express = require('express')
const request = require('superagent')
const proxycache = require('./')
// Create Redis/Cloud Storage proxycache client
const config = {
store: {
client: 'redis',
connection: {}
},
cache: {
client: 'gcloud',
connection: {
keyFilename: './gcloud-key.json',
projectId: 'development'
},
options: {
bucket: 'images-13nfd7sdf23fds73nfsdf'
}
}
}
let cache
proxycache(config).then(client => {
cache = client
})
// Image server to proxy for
const imgsrv = express()
imgsrv.get('/images/:id', (req, res) => {
const id = req.params.id
request.get(`http://www.fillmurray.com/g/${id}/${id}`).pipe(res)
})
imgsrv.listen(3888)
console.log('Image server listening on 3888')
const render = src => `
<html>
<head>
<title>bfm</title>
</head>
<body>
<img src='${src}' height=500 width=500/>
</body>
</html>
`
// Proxy Server
const app = express()
app.get('/images/:id', (req, res) => {
const id = req.params.id
// query the cache
cache.get(id).then(result => {
// cache hit
if (result) {
return res.end(render(result))
}
const uri = `http://localhost:3888/images/${id}`
// cache miss; send the default url
res.end(render(uri))
// cache the file
cache.set(id, uri, 60)
})
})
app.listen(8000)
console.log('Proxy server listening on 8000')