-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
109 lines (85 loc) · 2.87 KB
/
index.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
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
const _ = require('lodash')
const Sentry = require('@sentry/node')
const {
PORT,
NODE_ENV,
ELECTRS_URL,
CONCURRENCY,
SENTRY_DSN
} = process.env
if (NODE_ENV === 'production' && SENTRY_DSN) {
Sentry.init({ dsn: SENTRY_DSN })
}
const Bluebird = require('bluebird')
const axios = require('axios')
const express = require('express')
const helmet = require('helmet')
const compression = require('compression')
const bodyParser = require('body-parser')
const asyncHandler = require('express-async-handler')
const httpError = require('./http-error')
if (!PORT) throw new Error('Invalid PORT')
if (!ELECTRS_URL) throw new Error('Invalid ELECTRS_URL')
if (!CONCURRENCY) throw new Error('Invalid CONCURRENCY')
const app = express()
const electrs = axios.create({ baseURL: ELECTRS_URL })
if (NODE_ENV === 'production') {
app.use(Sentry.Handlers.requestHandler())
}
app.use(helmet())
app.use(compression())
app.use(bodyParser.json({ limit: '5mb' }))
app.set('etag', false)
app.post('/addresses', asyncHandler(async (req, res, next) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}
addresses = _.uniq(addresses)
const response = await Bluebird.map(addresses, address => {
return electrs.get(`/address/${address}`).then(response => response.data)
}, { concurrency: Number(CONCURRENCY) })
res.json(response)
}))
app.post('/addresses/utxo', asyncHandler(async (req, res, next) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}
addresses = _.uniq(addresses)
const response = await Bluebird.map(addresses, address => {
return electrs.get(`/address/${address}/utxo`).then(response => ({
address,
utxo: response.data
}))
}, { concurrency: Number(CONCURRENCY) })
res.json(response)
}))
app.post('/addresses/transactions', asyncHandler(async (req, res, next) => {
let { addresses } = req.body
if (!addresses || !_.isArray(addresses) || addresses.length === 0) {
return res.status(400).json({ error: 'Invalid "addresses" field' })
}
addresses = _.uniq(addresses)
const response = await Bluebird.map(addresses, address => {
return electrs.get(`/address/${address}/txs/chain`).then(response => ({
address,
transaction: response.data
}))
}, { concurrency: Number(CONCURRENCY) })
res.json(response)
}))
app.all('/*', function (req, res) {
res.status(404).json({
error: '404'
})
})
app.use((err, req, res, next) => {
const status = err.statusCode || 500
const message = err.message || err.toString()
if (NODE_ENV !== 'production') {
console.error(err)
}
return httpError(req, res, status, message)
})
app.listen(PORT, () => console.log(`API is running on ${PORT}`))