aboutsummaryrefslogtreecommitdiffstats
path: root/app.coffee
blob: e74bbdb335181e80e7f7288f84980b9b294c27df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
express = require('express')
http = require('http')
path = require('path')
engines = require('consolidate')
config = require('./config')
app = express()

app.engine('html', engines.hogan)
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((err, req, res, next) ->
  res.status(500)
  res.render('error', error: err )
)

app.get('/', (req, res) ->
  res.render('index',  js: (-> global.js), css: (-> global.css))
)

app.get('/report', (req, res) ->
  res.render('report',  js: (-> global.js), css: (-> global.css))
)

app.get('/client/:client', (req, res) ->
  db = require("redis").createClient()
  db.hgetall(req.params.client, (err, obj) ->
    res.write(JSON.stringify(obj))
    res.end()
  )
)

app.get('/transactions/:login', (req, res) ->
  connection = require('mysql').createConnection(config.database)

  connection.connect()
  connection.query("SELECT * FROM transactions", 
    [req.params.login], (err, rows) ->
      res.write(JSON.stringify(rows))
      connection.end()
      res.end()
  )
)

app.get('/ticker', (req, res) ->
  symbol = req.params.symbol
  symbol or= 'virtexCAD'

  options = 
    host: 'bitcoincharts.com', 
    path: '/t/depthcalc.json?symbol=' + symbol + '&type=bid&amount=1000&currency=true'
  
  require('http').get(options, (r) ->
    r.setEncoding('utf-8')
    r.on('data', (chunk) ->
      res.send(chunk)
    ) 
  )
)

app.post('/create', (req, res) ->
  db = require("redis").createClient()
  db.hmset(req.body.login, req.body, ->
    res.writeHead(302, 'Location': '/' + req.body.login)
    res.end()
  )
)

app.get('/:client', (req, res) ->
  res.render('calculator', 
    client: req.params.client, 
    js: (-> global.js), 
    css: (-> global.css) 
  )
)

app.use((err, req, res, next) ->
  console.log(err)
)

app.listen(3000)