-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
53 lines (43 loc) · 1.41 KB
/
app.js
File metadata and controls
53 lines (43 loc) · 1.41 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
/*
Author: Israel Flores (https://github.com/idflores)
File: /app.js
Purpose: boots the server creating an instance of the blockchain client
*/
const app = (require('express'))()
const bodyParser = require('body-parser')
const chalk = require('chalk')
const fs = require('fs')
const https = require('https')
const path = require('path')
const WebSocket = require('ws')
const startApp = require('./lib/run.js')
const p2p = require('./lib/p2p/server.js')
const PORT = 443
// translate incling responses to JSON
app.use(bodyParser.json())
// get server routes
var routes = fs.readdirSync('routes')
for (var i = 0; i < routes.length ; i++) {
if (path.extname(routes[i]) != '.js') continue
else {
var routes = require('./routes/' + routes[i])
if (typeof routes === 'function') routes(app)
}
}
// create the server instance and attach a WebSocket server instance
var server = https.createServer(app)
var wsServer = new WebSocket.Server({ server: server })
// configure the WebSocket server
p2p(wsServer)
// boot the server
new Promise((resolve, reject) => {
server.listen(PORT, function () {
console.log(chalk.green("Blockchain HTTPS Server running..."))
resolve()
})
})
.then(() => { startApp() })
.catch(err => { console.error(chalk.red(err.stack)) })
// instantiate the genesisBlock
// TODO: instantiate the genesis block only if a blockchain cannot be polled
// create instance of the blockchain