summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2017-10-21 17:15:32 +0300
committerYuval Adam <_@yuv.al>2017-10-21 17:15:32 +0300
commitf89b88d28b0f6a9284ac814a79eeb96943992d0e (patch)
treefbe5e1fb57c9176dcdd7ece8ffaf5bc89ed763dd
parent2bd9dea6e0dbf22af1893f8963a6e8d872daa1d4 (diff)
Proper image fetching with ACL and caching
-rw-r--r--server.js106
1 files changed, 64 insertions, 42 deletions
diff --git a/server.js b/server.js
index 7fd73f0..b3fe747 100644
--- a/server.js
+++ b/server.js
@@ -9,46 +9,58 @@ const aws = require('aws-sdk')
const s3 = new aws.S3()
function fetchImages() {
+ console.log('Running fetchImages()')
request('http://map.govmap.gov.il/rainradar/radar.json', (error, response, body) => {
- var res = JSON.parse(body)
- for (let size of ['images140', 'images280']) {
- var imgs = res[size]
- Object.keys(imgs).forEach(function (img) {
- const ts = img.split(':')
- const url = 'http://' + imgs[img]
- const key = `fetched:${url}`
- client.get(key, (err, reply) => {
- if (!reply && reply != 'fetching') {
- client.set(key, 'fetching')
- request({ url: url, encoding: null }, (error, response, body) => {
- if (err) {
- console.log(err)
- client.del(key)
- }
- else {
- const d = ts.slice(0, 3).join('')
- const t = ts.slice(3, 5).join('')
- console.log(`Uploading ${url} to bucket`)
- s3.putObject({
- Bucket: 'imgs.geshem.space',
- Key: `${d}_${t}.png`,
- ContentType: response.headers['content-type'],
- ContentLength: response.headers['content-length'],
- Body: body
- }, (err, data) => {
- if (err) {
- console.log(err)
- client.del(key)
- }
- else {
- client.set(key, data.ETag.slice(1, -1))
- }
- })
- }
- })
- }
+ if (error) {
+ console.log(error)
+ }
+ else {
+ var res = JSON.parse(body)
+ for (let size of ['images140', 'images280']) {
+ var imgs = res[size]
+ Object.keys(imgs).forEach(function (img) {
+ const ts = img.split(':')
+ const url = 'http://' + imgs[img]
+ const key = `fetched:${url}`
+ client.get(key, (err, reply) => {
+ if (!reply && reply != 'fetching') {
+ console.log(`Fetching ${url}`)
+ client.set(key, 'fetching')
+ request({ url: url, encoding: null }, (error, response, body) => {
+ if (err) {
+ console.log(err)
+ client.del(key)
+ }
+ else {
+ const r = size.slice(-3)
+ const d = ts.slice(0, 3).join('')
+ const t = ts.slice(3, 5).join('')
+ console.log(`Uploading to bucket`)
+ s3.putObject({
+ Bucket: 'imgs.geshem.space',
+ Key: `${r}/${d}/${t}.png`,
+ ContentType: response.headers['content-type'],
+ ContentLength: response.headers['content-length'],
+ Body: body,
+ ACL: 'public-read'
+ }, (err, data) => {
+ if (err) {
+ console.log(err)
+ client.del(key)
+ }
+ else {
+ client.set(key, data.ETag.slice(1, -1))
+ }
+ })
+ }
+ })
+ }
+ else {
+ console.log(`No need to fetch ${url}`)
+ }
+ })
})
- })
+ }
}
})
}
@@ -57,9 +69,20 @@ app.set('view engine', 'pug')
app.use('/static', express.static('static'))
-app.use('/', function(req, res, next){
+app.use('/', function (req, res, next){
res.on('finish', function(){
- console.log('hello world!')
+ client.get('fresh', (err, reply) => {
+ if (err) {
+ console.log(err)
+ }
+ else if (!reply) {
+ client.setex('fresh', 60, true)
+ fetchImages()
+ }
+ else {
+ console.log('Skipping fetch, waiting 60 seconds max')
+ }
+ })
})
next()
});
@@ -69,6 +92,5 @@ app.get('/', function (req, res) {
})
app.listen(3000, function () {
- console.log('Example app listening on port 3000!')
- fetchImages()
+ console.log('Starting geshem.space server')
})