forked from harshjv/ethereum-scraper
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.js
47 lines (36 loc) · 1.13 KB
/
api.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
const Sentry = require('@sentry/node')
const { ethers } = require('ethers')
const express = require('express')
const helmet = require('helmet')
const compression = require('compression')
const {
PORT,
WEB3_URI,
NODE_ENV
} = process.env
if (!PORT) throw new Error('Invalid PORT')
const app = express()
const ethersProvider = WEB3_URI.startsWith('ws') ? new ethers.providers.WebSocketProvider(WEB3_URI) : new ethers.providers.StaticJsonRpcProvider(WEB3_URI)
if (NODE_ENV === 'production') {
app.use(Sentry.Handlers.requestHandler())
}
app.use(helmet())
app.use(compression())
app.use(require('./middlewares/httpHelpers'))
app.set('etag', false)
app.set('ethers', ethersProvider)
app.use('/status', require('./routes/status'))
app.use('/txs', require('./routes/txs'))
app.use('/events', require('./routes/events'))
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 res.notOk(status, message)
})
ethersProvider.ready.then(() => {
app.listen(PORT)
console.log(`API is running on ${PORT}`)
})