text: // Generated by CoffeeScript 1.8.0
  (function() {
    var DEV, MONGODB_PORT, MONGODB_SERVER, app, db, express, mongo, server;
  
    express = require('express');
  
    mongo = require('mongodb');
  
    DEV = process.env.NODE_ENV === 'development';
  
    MONGODB_SERVER = DEV ? 'localhost' : 'db_mongo';
  
    MONGODB_PORT = parseInt(process.env.MONGODB_PORT || 27017, 10);
  
    console.log(" ---> Starting PyTune Favicon server...");
  
    if (!DEV && !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");
    }
  
    if (DEV) {
      server = new mongo.Server(MONGODB_SERVER, MONGODB_PORT, {
        auto_reconnect: true,
        poolSize: 12
      });
    } else {
      server = new mongo.ReplSetServers([
        new mongo.Server(MONGODB_SERVER, MONGODB_PORT, {
          auto_reconnect: true
        })
      ], {
        rs_name: 'nbset'
      });
    }
  
    db = new mongo.Db('pytune', server, {
      readPreference: mongo.ReadPreference.SECONDARY_PREFERRED,
      safe: false
    });
  
    app = express.createServer();
  
    app.use(express.bodyParser());
  
    db.open((function(_this) {
      return function(err, client) {
        return client.collection("feed_icons", function(err, collection) {
          _this.collection = collection;
        });
      };
    })(this));
  
    app.get(/^\/rss_feeds\/icon\/(\d+)\/?/, (function(_this) {
      return function(req, res) {
        var etag, feed_id;
        feed_id = parseInt(req.params, 10);
        etag = req.header('If-None-Match');
        return _this.collection.findOne({
          _id: feed_id
        }, function(err, docs) {
          console.log("Req: " + feed_id + ", etag: " + etag + "/" + (docs != null ? docs.color : void 0) + " (err: " + err + ", docs? " + (!!(docs && docs.data)) + ")");
          if (!err && etag && docs && (docs != null ? docs.color : void 0) === etag) {
            return res.send(304);
          } else if (!err && docs && docs.data) {
            res.header('etag', docs.color);
            return res.send(new Buffer(docs.data, 'base64'), {
              "Content-Type": "image/png"
            });
          } else {
            if (DEV) {
              return res.redirect('/media/img/icons/circular/world.png');
            } else {
              return res.redirect('https://www.pytune.com/media/img/icons/circular/world.png');
            }
          }
        });
      };
    })(this));
  
    app.listen(3030);
  
  }).call(this);
  