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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import fs from 'fs'
import path from 'path'
import request from 'request'
import express from 'express'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
import passport from './passport'
import config from './config'
import proxyMiddleware from 'http-proxy-middleware'
import transactions from './routes/transactions'
import session from 'express-session'
const sessions = require('./routes/sessions')(passport)
const RedisStore = require('connect-redis')(session)
const users = require('./routes/users')(sessions)
;(async () => {
const indexOf = [].indexOf ||
function (item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i } return -1 }
const sessionStore = new RedisStore(require('./redis').host, {
ttl: 172800,
})
const proxyContext = '/blockcypher'
const proxyOptions = {
target: 'https://api.blockcypher.com',
changeOrigin: true,
pathRewrite: {
'^/blockcypher/': '/',
},
onProxyReq: function (proxyReq, req, res) {
let symbol = indexOf.call(proxyReq.path, '?') >= 0 ? '&' : '?'
proxyReq.path += symbol + 'token=' + config.blockcypher_token
},
}
const proxy = proxyMiddleware(proxyContext, proxyOptions)
const app = express()
app.enable('trust proxy')
app.engine('html', require('hogan-express'))
app.set('view engine', 'html')
app.set('views', path.resolve(__dirname, '/views'))
app.use(express['static'](path.resolve(__dirname, '/public')))
app.use(proxy)
app.use(bodyParser.urlencoded({
extended: true,
}))
app.use(bodyParser.json())
app.use(bodyParser.json({
type: 'application/vnd.api+json',
}))
app.use(cookieParser(config.secret))
app.use(session({
resave: true,
saveUninitialized: true,
secret: config.secret,
store: sessionStore,
cookie: { maxAge: 1209600000 },
key: 'coinos.sid',
}))
app.use(passport.initialize())
app.use(passport.session())
const authorize = function (req, res, next) {
var ref, ref1
if (req.params.user === ((ref = req.user) != null ? ref.username : void 0) || ((ref1 = req.user) != null ? ref1.username : void 0) === 'admin') {
return next()
}
req.session.redirect = req.path
return res.redirect('/login')
}
const cache = function (req, res, next) {
if (req.path !== '/login') {
res.setHeader('Cache-Control', 'public, max-age=900')
}
return next()
}
let fetchRates
(fetchRates = () => {
request('https://api.bitcoinaverage.com/exchanges/all', (err, res, body) => {
if (err) return
try {
require('util').isDate(JSON.parse(body).timestamp)
app.set('rates', body)
} catch (e) { console.log(e) }
})
return setTimeout(fetchRates, 120000)
})()
app.get('/', cache, sessions['new'])
app.get('/rates', cache, (req, res) => {
res.write(app.get('rates'))
})
app.get('/ticker', cache, function(req, res) {
let params = {
currency: 'CAD',
symbol: 'quadrigacx',
type: 'bid',
}
try {
exchange = JSON.parse(app.get('rates'))[req.query.currency][req.query.symbol]['rates'][req.query.type].toString()
} catch (err) {
exchange = 0
}
res.writeHead(200, {
'Content-Length': exchange.length,
'Content-Type': 'text/plain'
})
res.write(exchange)
res.end()
})
app.get('/tips', cache, function(req, res) {
return res.render('tips', {
notice: true,
layout: 'layout',
js: (function() {
return global.js
}),
css: (function() {
return global.css
})
})
})
app.get('/login', cache, sessions["new"])
app.post('/login', sessions.create)
app.get('/logout', sessions.destroy)
app.get('/users.json', users.index)
app.get('/register', cache, users["new"])
app.get('/users/new', cache, users["new"])
app.post('/users', users.create)
app.get('/verify/:token', users.verify)
app.post('/:user', authorize, users.update)
app.get('/:user/edit', authorize, users.edit)
app.get('/:user/profile', authorize, users.profile)
app.get('/:user/wallet', authorize, users.wallet)
app.get('/:user/transactions.json', authorize, transactions.json)
app.post('/:user/transactions', transactions.create)
app.post('/transactions/:txid', transactions.update)
app["delete"]('/:user/transactions/:txid', transactions["delete"])
app.get('/:user/report', authorize, transactions.index)
app.get('/:user.json', users.json)
app.get('/:user', cache, users.show)
app.use(function(err, req, res, next) {
res.status(500)
res.send('An error occurred')
console.error(err.stack)
return res.end()
})
app.listen(3000)
}).call(this)
|