aboutsummaryrefslogtreecommitdiffstats
path: root/app.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2012-11-29 03:04:26 -0800
committerAdam Soltys <[email protected]>2012-11-29 03:04:26 -0800
commitc5d381e23a31c6ba2b6d00490ba2f6b68d32572e (patch)
tree73651f4868784dfaa1fc2badeaebf2d27d0eceaa /app.js
parent819629392fbdd2d56b5ab1cef3681b133de54099 (diff)
downloadcoinos-server-c5d381e23a31c6ba2b6d00490ba2f6b68d32572e.tar.gz
coinos-server-c5d381e23a31c6ba2b6d00490ba2f6b68d32572e.zip
Refactoring
Diffstat (limited to 'app.js')
-rw-r--r--app.js289
1 files changed, 289 insertions, 0 deletions
diff --git a/app.js b/app.js
new file mode 100644
index 00000000..5a8a7912
--- /dev/null
+++ b/app.js
@@ -0,0 +1,289 @@
+// 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&currency=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);