forked from Overtorment/Cashier-BTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cashier-btc.js
67 lines (53 loc) · 1.81 KB
/
cashier-btc.js
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
/**
* Cashier-BTC
* -----------
* Self-hosted bitcoin payment gateway
*
* https://github.com/Overtorment/Cashier-BTC
*
**/
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
let express = require('express')
let morgan = require('morgan')
let uuid = require('node-uuid')
let logger = require('./utils/logger')
morgan.token('id', function getId (req) {
return req.id
})
let app = express()
app.use(function (req, res, next) {
req.id = uuid.v4()
next()
})
app.use(morgan(':id :remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"'))
app.set('trust proxy', 'loopback')
let bodyParser = require('body-parser')
let config = require('./config')
let rp = require('request-promise')
app.use(bodyParser.urlencoded({ extended: false })) // parse application/x-www-form-urlencoded
app.use(bodyParser.json(null)) // parse application/json
global.btcUsd = 7000 // initial
global.btcEur = 6000
app.use('/qr', express.static('qr'))
app.use(require('./controllers/api'))
app.use(require('./controllers/website'))
let updateExchangeRate = async function (pair) {
let json
try {
json = await rp.get({url: 'https://www.bitstamp.net/api/v2/ticker/' + pair, json: true})
} catch (err) {
logger.error('updateExchangeRate', err.message)
}
switch (pair) {
case 'btceur': global.btcEur = json.ask; break
case 'btcusd': global.btcUsd = json.ask; break
}
}
updateExchangeRate('btcusd').then(updateExchangeRate('btceur'))
setInterval(() => updateExchangeRate('btcusd').then(updateExchangeRate('btceur')), 5 * 60 * 1000)
require('./smoke-test')
require('./deploy-design-docs') // checking design docs in Couchdb
let server = app.listen(config.port, function () {
logger.log('BOOTING UP', ['Listening on port %d', config.port])
})
module.exports = server