2016-02-05 15:26:10 -08:00
|
|
|
// Generated by CoffeeScript 1.8.0
|
2012-04-07 14:40:04 -07:00
|
|
|
(function() {
|
2019-04-13 15:28:56 -04:00
|
|
|
var DEV, MONGODB_PORT, MONGODB_SERVER, app, log, mongo, server, url;
|
2012-04-07 14:40:04 -07:00
|
|
|
|
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');
|
|
|
|
|
2019-04-13 15:28:56 -04:00
|
|
|
log = require('./log.js');
|
|
|
|
|
2012-12-24 21:18:40 -08:00
|
|
|
DEV = process.env.NODE_ENV === 'development';
|
|
|
|
|
2013-12-01 10:18:17 -05:00
|
|
|
MONGODB_SERVER = DEV ? 'localhost' : 'db_mongo';
|
2012-04-07 14:40:04 -07:00
|
|
|
|
2012-10-29 12:25:28 -07:00
|
|
|
MONGODB_PORT = parseInt(process.env.MONGODB_PORT || 27017, 10);
|
|
|
|
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug("Starting NewsBlur Favicon server...");
|
2016-02-05 15:26:10 -08:00
|
|
|
|
|
|
|
if (!DEV && !process.env.NODE_ENV) {
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug("Specify NODE_ENV=<development,production>");
|
2016-02-05 15:26:10 -08:00
|
|
|
return;
|
|
|
|
} else if (DEV) {
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug("Running as development server");
|
2016-02-05 15:26:10 -08:00
|
|
|
} else {
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug("Running as production server");
|
2016-02-05 15:26:10 -08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2012-04-07 14:59:12 -07:00
|
|
|
|
2016-11-30 12:29:24 -08:00
|
|
|
mongo.MongoClient.connect(url, (function(_this) {
|
|
|
|
return function(err, db) {
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug("Connected to " + (db != null ? db.serverConfig.s.host : void 0) + ":" + (db != null ? db.serverConfig.s.port : void 0) + " / " + err);
|
2016-11-30 13:45:54 -08:00
|
|
|
return _this.collection = db != null ? db.collection("feed_icons") : void 0;
|
2016-02-05 15:26:10 -08:00
|
|
|
};
|
|
|
|
})(this));
|
2012-04-07 14:40:04 -07:00
|
|
|
|
2016-11-30 12:09:49 -08:00
|
|
|
app.get(/\/rss_feeds\/icon\/(\d+)\/?/, (function(_this) {
|
2016-02-05 15:26:10 -08:00
|
|
|
return function(req, res) {
|
|
|
|
var etag, feed_id;
|
2016-11-30 12:09:49 -08:00
|
|
|
feed_id = parseInt(req.params[0], 10);
|
2016-02-05 15:26:10 -08:00
|
|
|
etag = req.header('If-None-Match');
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug(("Feed: " + feed_id + " ") + (etag ? " / " + etag : ""));
|
2016-02-05 15:26:10 -08:00
|
|
|
return _this.collection.findOne({
|
|
|
|
_id: feed_id
|
|
|
|
}, function(err, docs) {
|
2016-11-30 12:09:49 -08:00
|
|
|
var body;
|
2016-02-05 15:26:10 -08:00
|
|
|
if (!err && etag && docs && (docs != null ? docs.color : void 0) === etag) {
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug(("Cached: " + feed_id + ", etag: " + etag + "/" + (docs != null ? docs.color : void 0) + " ") + (err ? "(err: " + err + ")" : ""));
|
2016-11-30 12:38:31 -08:00
|
|
|
return res.sendStatus(304);
|
2016-02-05 15:26:10 -08:00
|
|
|
} else if (!err && docs && docs.data) {
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug(("Req: " + feed_id + ", etag: " + etag + "/" + (docs != null ? docs.color : void 0) + " ") + (err ? "(err: " + err + ")" : ""));
|
2016-02-05 15:26:10 -08:00
|
|
|
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");
|
|
|
|
return res.status(200).send(body);
|
2014-08-10 12:56:09 -07:00
|
|
|
} else {
|
2019-04-13 15:28:56 -04:00
|
|
|
log.debug(("Redirect: " + feed_id + ", etag: " + etag + "/" + (docs != null ? docs.color : void 0) + " ") + (err ? "(err: " + err + ")" : ""));
|
2016-02-05 15:26:10 -08:00
|
|
|
if (DEV) {
|
|
|
|
return res.redirect('/media/img/icons/circular/world.png');
|
|
|
|
} else {
|
|
|
|
return res.redirect('https://www.newsblur.com/media/img/icons/circular/world.png');
|
|
|
|
}
|
2014-08-10 12:56:09 -07:00
|
|
|
}
|
2016-02-05 15:26:10 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
})(this));
|
2012-04-07 14:40:04 -07:00
|
|
|
|
|
|
|
app.listen(3030);
|
|
|
|
|
|
|
|
}).call(this);
|