forked from Bitcoin-com/rest.bitcoin.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
214 lines (177 loc) · 5.53 KB
/
Copy pathapp.js
File metadata and controls
214 lines (177 loc) · 5.53 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict"
const express = require("express")
const path = require("path")
const favicon = require("serve-favicon")
const logger = require("morgan")
const cookieParser = require("cookie-parser")
const bodyParser = require("body-parser")
const basicAuth = require("express-basic-auth")
const helmet = require("helmet")
const RateLimit = require("express-rate-limit")
const axios = require("axios")
const debug = require("debug")("rest-cloud:server")
const http = require("http")
const BitcoinCashZMQDecoder = require("bitcoincash-zmq-decoder")
let zmq = require("zeromq"),
sock = zmq.socket("sub")
const swStats = require("swagger-stats")
const apiSpec = require("./public/bitcoin-com-rest-v1.json")
require("dotenv").config()
const app = express()
const index = require("./routes/index")
const healthCheck = require("./routes/health-check")
const address = require("./routes/address")
const block = require("./routes/block")
const blockchain = require("./routes/blockchain")
const control = require("./routes/control")
const generating = require("./routes/generating")
const mining = require("./routes/mining")
const network = require("./routes/network")
const rawtransactions = require("./routes/rawtransactions")
const transaction = require("./routes/transaction")
const util = require("./routes/util")
const dataRetrieval = require("./routes/dataRetrieval")
const payloadCreation = require("./routes/payloadCreation")
app.use(swStats.getMiddleware({ swaggerSpec: apiSpec }))
app.use(helmet())
const cors = require("cors")
app.use(cors())
app.enable("trust proxy")
// view engine setup
app.set("views", path.join(__dirname, "views"))
app.set("view engine", "jade")
app.use("/public", express.static(`${__dirname}/public`))
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger("dev"))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, "public")))
//
// let username = process.env.USERNAME;
// let password = process.env.PASSWORD;
//
// app.use(basicAuth(
// {
// users: { username: password }
// }
// ));
// Make io accessible to our router
app.use(function(req, res, next) {
req.io = io
next()
})
const prefix = "v1"
app.use("/", index)
app.use(`/${prefix}/` + `health-check`, healthCheck)
app.use(`/${prefix}/` + `address`, address.router)
app.use(`/${prefix}/` + `blockchain`, blockchain)
app.use(`/${prefix}/` + `block`, block)
app.use(`/${prefix}/` + `control`, control)
app.use(`/${prefix}/` + `generating`, generating)
app.use(`/${prefix}/` + `mining`, mining)
app.use(`/${prefix}/` + `network`, network)
app.use(`/${prefix}/` + `rawtransactions`, rawtransactions)
app.use(`/${prefix}/` + `transaction`, transaction)
app.use(`/${prefix}/` + `util`, util)
app.use(`/${prefix}/` + `dataRetrieval`, dataRetrieval)
app.use(`/${prefix}/` + `payloadCreation`, payloadCreation)
// catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error("Not Found")
err.status = 404
next(err)
})
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get("env") === "development" ? err : {}
// render the error page
res.status(err.status || 500)
res.json({
status: 500,
message: err.message
})
})
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || "3000")
app.set("port", port)
/**
* Create HTTP server.
*/
const server = http.createServer(app)
const io = require("socket.io").listen(server)
io.on("connection", socket => {
console.log("Socket Connected")
socket.on("disconnect", () => {
console.log("Socket Disconnected")
})
})
const bitcoincashZmqDecoder = new BitcoinCashZMQDecoder(process.env.NETWORK)
sock.connect(`tcp://${process.env.ZEROMQ_URL}:${process.env.ZEROMQ_PORT}`)
sock.subscribe("raw")
sock.on("message", (topic, message) => {
const decoded = topic.toString("ascii")
if (decoded === "rawtx") {
const txd = bitcoincashZmqDecoder.decodeTransaction(message)
io.emit("transactions", JSON.stringify(txd, null, 2))
} else if (decoded === "rawblock") {
const blck = bitcoincashZmqDecoder.decodeBlock(message)
io.emit("blocks", JSON.stringify(blck, null, 2))
}
})
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port)
server.on("error", onError)
server.on("listening", onListening)
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
const port = parseInt(val, 10)
if (isNaN(port)) {
// named pipe
return val
}
if (port >= 0) {
// port number
return port
}
return false
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== "listen") throw error
const bind = typeof port === "string" ? `Pipe ${port}` : `Port ${port}`
// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(`${bind} requires elevated privileges`)
process.exit(1)
break
case "EADDRINUSE":
console.error(`${bind} is already in use`)
process.exit(1)
break
default:
throw error
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
const addr = server.address()
const bind = typeof addr === "string" ? `pipe ${addr}` : `port ${addr.port}`
debug(`Listening on ${bind}`)
}
//
// module.exports = app;