-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (98 loc) · 3.62 KB
/
Copy pathindex.js
File metadata and controls
120 lines (98 loc) · 3.62 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
/* Import Libraries */
const request = require('request');
const express = require('express');
const bodyParser = require('body-parser');
const swaggerUi = require('swagger-ui-express');
const swaggerJsDoc = require('swagger-jsdoc');
/* Import P2P Server */
const P2PServer = require('./app/network/P2PServer');
/* Import Blockchain */
const { Blockchain } = require('./app/blockchain/blockchain');
/* Server Initialization */
const app = express();
const HTTP_PORT = process.env.HTTP_PORT || 3000;
/* Swagger Configuration */
const swaggerOptions = {
swaggerDefinition: {
info: {
title: "Blockchain API",
description: "Blockchain API Information",
contact: {
name: "Rikimarutsui"
},
servers: ["http://localhost:" + HTTP_PORT]
}
},
apis: ["./index.js", "./routes/blockchain.js", "./routes/transaction.js", "./routes/wallet.js"]
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
/* Initialize API routes */
app.use(express.static(__dirname + "/express/public")); // Public files
app.use(express.static("public")); // Public files
app.set("view engine", "ejs"); // Express Engine HTML files
app.use(bodyParser.urlencoded({ extended: true })); // Body Parser
app.use(bodyParser.json()); // Body Parser
app.use('/jsdoc', express.static('out')); // JSDoc
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs)); // Swagger document
app.use('/api/blockchain', require('./routes/blockchain'));
app.use('/api/transaction', require('./routes/transaction'));
app.use('/api/wallet', require('./routes/wallet'));
/* Create a Blockchain */
let blockchain = new Blockchain().getInstance();
/* Create P2P Server */
const p2pServer = new P2PServer().getInstance();
/* Create Socket List */
const sockets = [];
/***************
* Form Actions
***************/
app.get("/createWallet", (req, res) => {
res.render("createWallet", { address: null, privateKey: null });
})
app.post("/wallet/create", (req, res) => {
return new Promise((resolve, reject) => {
request(req.protocol + '://' + req.get('host') + '/api/wallet/create', { json: true }, (err, res, body) => {
if (err) { reject(err); }
resolve(body);
});
}).then((body) => {
res.render("createWallet", { address: body.address, privateKey: body.privateKey });
}).catch((err) => {
console.log(err);
});
});
app.get("/transaction", (req, res) => {
res.render("transaction");
});
app.post("/transaction/create", (req, res) => {
const { sender, privateKey, recipient, amount, message } = req.body;
return new Promise((resolve, reject) => {
request.post(req.protocol + '://' + req.get('host') + '/api/wallet/send',
{ json: true,
body: { sender, recipient, amount, privateKey, message }
}, (err, res, body) => {
if (err) { reject(err); }
resolve(body);
});
}).then((body) => {
res.render("transactionDetail", {
sender: sender,
recipient: recipient,
amount: amount,
message: message,
tx: body.tx,
signature: body.signature
});
}).catch((err) => {
console.log(err);
});
});
/***************
* Server Startup
**************/
/* HTTP Server start up method */
app.listen(HTTP_PORT, () => {
console.log(`Server is running on port ${HTTP_PORT}`);
});
const peers = process.env.PEERS ? process.env.PEERS.split(',') : [];
peers.forEach(peer => p2pServer.connectToPeer(peer));