// Generated by CoffeeScript 1.4.0 (function() { var LocalStrategy, app, bcrypt, config, db, engines, express, http, passport, path, route, _fn, _i, _len, _ref; express = require('express'); http = require('http'); path = require('path'); engines = require('consolidate'); passport = require('passport'); bcrypt = require('bcrypt'); db = require("redis").createClient(); config = require('./config'); app = express(); LocalStrategy = require('passport-local').Strategy; passport.use(new LocalStrategy(function(username, password, done) { var user; user = { username: 'soltysa', password: 'adam' }; return done(null, user); })); passport.serializeUser(function(user, done) { return done(null, user.username); }); passport.deserializeUser(function(id, done) { var user; user = { username: 'soltysa', password: 'adam' }; return done(null, user); }); app.enable('trust proxy'); app.engine('html', require('mmm').__express); app.set('view engine', 'html'); app.set('views', __dirname + '/views'); app.use(express["static"](__dirname + '/public')); app.use(require('connect-assets')({ src: 'public' })); app.use(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ secret: 'weareallmadeofstars' })); app.use(passport.initialize()); app.use(passport.session()); app.use(app.router); app.use(function(err, req, res, next) { res.status(500); console.log(err); return res.end(); }); _ref = ['', 'about', 'exchangers', 'merchants']; _fn = function(route) { return app.get("/" + route, function(req, res) { if (route === '') { route = 'index'; } return res.render(route, { js: (function() { return global.js; }), css: (function() { return global.css; }), layout: 'layout' }); }); }; for (_i = 0, _len = _ref.length; _i < _len; _i++) { route = _ref[_i]; _fn(route); } app.get('/setup', function(req, res) { return res.render('setup', { js: (function() { return global.js; }), css: (function() { return global.css; }) }); }); app.get('/:user/report', function(req, res) { return res.render('report', { user: req.params.user, js: (function() { return global.js; }), css: (function() { return global.css; }) }); }); app.get('/:user.json', function(req, res) { return db.hgetall(req.params.user, function(err, obj) { res.write(JSON.stringify(obj)); return res.end(); }); }); app.get('/:user/transactions', function(req, res) { var r, user; user = req.params.user; r = { 'transactions': [] }; return db.lrange("" + user + ":transactions", 0, -1, function(err, transactions) { var i, process; process = function(err, t) { r.transactions.push(t); if (i >= transactions.length) { res.write(JSON.stringify(r)); return res.end(); } else { return db.hgetall("" + user + ":transactions:" + transactions[i++], process); } }; i = 0; return db.hgetall("" + user + ":transactions:" + transactions[i++], process); }); }); app.get('/ticker', function(req, res) { var options; options = { host: 'bitcoincharts.com', path: '/t/depthcalc.json?symbol=' + req.query.symbol + '&type=ask&amount=1000¤cy=true' }; return require('http').get(options, function(r) { r.setEncoding('utf-8'); return r.on('data', function(chunk) { var exchange; exchange = 1000 / JSON.parse(chunk).out; exchange = (Math.ceil(exchange * 100) / 100).toString(); res.writeHead(200, { 'Content-Length': exchange.length, 'Content-Type': 'text/plain' }); res.write(exchange); return res.end(); }); }); }); app.get('/login', function(req, res) { return res.render('login', { js: (function() { return global.js; }), css: (function() { return global.css; }) }); }); app.post('/login', function(req, res, next) { return passport.authenticate('local', function(err, user, info) { if (err) { return next(err); } if (!user) { return res.redirect('/login'); } return req.logIn(user, function(err) { if (err) { return next(err); } return res.redirect('/' + user.username); }); })(req, res, next); }); app.post('/users', function(req, res) { var k, params, v, _ref1; if (req.body.login) { if (req.body.password) { return bcrypt.hash(req.body.password, 12, function(err, hash) { console.log(req) return db.hget(req.body.login, 'password', function(err, password) { return bcrypt.compare(req.body.password, password, function(err, match) { if (match) { req.body.password = password; return db.hmset(req.body.login, req.body, function() { return res.redirect(req.body.login); }); } else { return res.redirect(req.body.login); } }); }); }); } } else { params = []; _ref1 = req.body; for (k in _ref1) { v = _ref1[k]; params.push(encodeURIComponent(k), '=', encodeURIComponent(v), '&'); } if (params.length) { params.pop(); } return res.redirect('calculator?' + params.join('')); } }); app.post('/:user/transactions', function(req, res) { var user; user = req.params.user; return db.incr('transactions', function(err, id) { return db.hmset("" + user + ":transactions:" + id, req.body, function() { return db.rpush("" + user + ":transactions", id, function() { res.write(JSON.stringify(req.body)); return res.end(); }); }); }); }); app.get('/calculator', function(req, res) { return res.render('calculator', { js: (function() { return global.js; }), css: (function() { return global.css; }) }); }); app.get('/:user/edit', function(req, res) { return res.render('index', { user: req.params.user, js: (function() { return global.js; }), css: (function() { return global.css; }) }); }); app.get('/:user', function(req, res) { return res.render('calculator', { user: req.params.user, js: (function() { return global.js; }), css: (function() { return global.css; }) }); }); app.listen(3000); }).call(this);