2016-11-30 12:09:49 -08:00
|
|
|
app = require('express')()
|
|
|
|
server = require('http').Server(app)
|
2012-04-07 14:40:04 -07:00
|
|
|
mongo = require 'mongodb'
|
|
|
|
|
2012-12-24 21:18:40 -08:00
|
|
|
DEV = process.env.NODE_ENV == 'development'
|
2013-12-01 10:18:17 -05:00
|
|
|
MONGODB_SERVER = if DEV then 'localhost' else 'db_mongo'
|
2012-10-29 12:25:28 -07:00
|
|
|
MONGODB_PORT = parseInt(process.env.MONGODB_PORT or 27017, 10)
|
|
|
|
|
2016-02-05 15:26:10 -08:00
|
|
|
console.log " ---> Starting NewsBlur Favicon server..."
|
|
|
|
if !DEV and !process.env.NODE_ENV
|
|
|
|
console.log " ---> Specify NODE_ENV=<development,production>"
|
|
|
|
return
|
|
|
|
else if DEV
|
|
|
|
console.log " ---> Running as development server"
|
|
|
|
else
|
|
|
|
console.log " ---> Running as production server"
|
|
|
|
|
2012-12-24 21:18:40 -08:00
|
|
|
if DEV
|
2016-11-30 12:29:24 -08:00
|
|
|
url = "mongodb://#{MONGODB_SERVER}:#{MONGODB_PORT}/newsblur"
|
2012-12-24 21:18:40 -08:00
|
|
|
else
|
2016-11-30 12:29:24 -08:00
|
|
|
url = "mongodb://#{MONGODB_SERVER}:#{MONGODB_PORT}/newsblur?replicaSet=nbset&readPreference=secondaryPreferred"
|
2012-12-24 21:18:40 -08:00
|
|
|
|
2016-11-30 12:29:24 -08:00
|
|
|
mongo.MongoClient.connect url, (err, db) =>
|
|
|
|
console.log " ---> Connected to #{db} / #{err}"
|
|
|
|
@collection = db.collection "feed_icons"
|
2012-04-07 14:40:04 -07:00
|
|
|
|
2016-11-30 12:09:49 -08:00
|
|
|
app.get /\/rss_feeds\/icon\/(\d+)\/?/, (req, res) =>
|
|
|
|
feed_id = parseInt(req.params[0], 10)
|
2012-04-07 15:33:41 -07:00
|
|
|
etag = req.header('If-None-Match')
|
2016-11-30 12:09:49 -08:00
|
|
|
console.log " ---> Feed: #{feed_id} / #{etag}"
|
2015-06-02 11:15:49 -07:00
|
|
|
@collection.findOne _id: feed_id, (err, docs) ->
|
2016-11-30 12:09:49 -08:00
|
|
|
console.log "Req #{req.params[0]}: #{feed_id}, etag: #{etag}/#{docs?.color} (err: #{err}, docs? #{!!(docs and docs.data)})"
|
2014-03-28 13:49:03 -07:00
|
|
|
if not err and etag and docs and docs?.color == etag
|
2012-04-18 22:04:13 -07:00
|
|
|
res.send 304
|
|
|
|
else if not err and docs and docs.data
|
|
|
|
res.header 'etag', docs.color
|
2016-11-30 12:09:49 -08:00
|
|
|
body = new Buffer(docs.data, 'base64')
|
|
|
|
res.set("Content-Type", "image/png")
|
|
|
|
res.status(200).send body
|
2012-04-07 14:59:12 -07:00
|
|
|
else
|
2014-08-10 12:56:09 -07:00
|
|
|
if DEV
|
|
|
|
res.redirect '/media/img/icons/circular/world.png'
|
|
|
|
else
|
|
|
|
res.redirect 'https://www.newsblur.com/media/img/icons/circular/world.png'
|
2012-04-07 15:33:41 -07:00
|
|
|
|
2012-04-07 14:40:04 -07:00
|
|
|
app.listen 3030
|