-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·322 lines (296 loc) · 8.69 KB
/
server.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const web3 = require("./ethereum/test");
const Mail = require("./ethereum/build/Mail.json");
const Ship = require("./ethereum/build/Shipping.json");
const factory = require("./ethereum/factory");
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = process.env.PORT || 5000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const data = fs.readFileSync("./database.json");
const conf = JSON.parse(data);
const mysql = require("mysql");
const connection = mysql.createConnection({
host: conf.host,
user: conf.user,
password: conf.password,
port: conf.port,
database: conf.database,
});
connection.connect();
const multer = require("multer");
const upload = multer({ dest: "./upload" });
app.get("/api/users", (req, res) => {
connection.query("SELECT * FROM USER", (err, rows, fields) => {
res.send(rows);
});
});
app.post("/api/getmail", upload.single("image"), async (req, res) => {
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
const senderLength = await mail.methods.senderLength().call();
console.log(senderLength);
if (senderLength == 0) {
console.log("현재 데이터 0");
res.send({
senderLength: senderLength,
});
} else {
console.log("현재 데이터" + senderLength + "개");
let datas = [];
for (var i = 0; i < senderLength; i++) {
await Promise.all([
mail.methods.senderInfos(i).call(),
mail.methods.mailInfos(i).call(),
]).then((value) => {
var newObj = Object.assign({}, value[0], value[1]);
datas.push(newObj);
});
}
console.log(datas);
res.send({
datas: datas,
length: senderLength,
});
}
});
app.post("/api/complete", upload.single("image"), async (req, res) => {
try {
const accounts = await web3.eth.getAccounts();
console.log(accounts);
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
await mail.methods
.mailComplete(req.body.index, req.body.password)
.send({
from: accounts[0],
gas: 2000000,
});
const result = await mail.methods.mailInfos(req.body.index).call();
if (result.complete == true) {
res.send({
result: result.complete,
});
} else {
res.send({
result: result.complete,
});
}
} catch (err) {
res.send(false);
console.log(err.message);
}
});
app.use("/image", express.static("./upload"));
app.post("/api/address", async (req, res) => {
try {
const accounts = await web3.eth.getAccounts();
console.log(accounts);
const test = await factory.methods.createMail();
const address = await test.call();
console.log(address);
await test.send({
from: accounts[0],
gas: 3000000,
});
res.send(address);
} catch (err) {
console.log(err.message);
}
});
app.post("/api/addmail", upload.single("image"), async (req, res) => {
try {
const accounts = await web3.eth.getAccounts();
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
await Promise.all([
mail.methods
.addSenderInfo(
req.body.sender_name,
req.body.sender_p1 +
"-" +
req.body.sender_p2 +
"-" +
req.body.sender_p3,
req.body.sender_email,
req.body.Spost + " " + req.body.Saddr1 + " " + req.body.Saddr2,
req.body.receiver_name,
req.body.receiver_p1 +
"-" +
req.body.receiver_p2 +
"-" +
req.body.receiver_p3,
req.body.Rpost + " " + req.body.Raddr1 + " " + req.body.Raddr2
)
.send({
from: accounts[0],
gas: 1500000,
}),
mail.methods
.addMailInfo(
req.body.product_name,
req.body.product_price,
req.body.quantity,
req.body.volume,
req.body.others,
req.body.password
)
.send({
from: accounts[0],
gas: 1500000,
}),
]);
res.send();
} catch (err) {
console.log(err.message);
}
});
app.post("/api/getonemail", upload.single("image"), async (req, res) => {
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
const senderInfo = await mail.methods.senderInfos(req.body.index).call();
const mailInfo = await mail.methods.mailInfos(req.body.index).call();
var newObj = Object.assign({}, senderInfo, mailInfo);
res.send(newObj);
});
app.post("/api/getship", upload.single("image"), async (req, res) => {
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
const shipAddr = await mail.methods.ship(req.body.index).call();
const ship = new web3.eth.Contract(JSON.parse(Ship.interface), shipAddr);
const hubLength = await ship.methods.hubLength().call();
console.log(hubLength);
if (hubLength == 0) {
let today = new Date();
const time = today.toLocaleString();
console.log(time);
try {
const accounts = await web3.eth.getAccounts();
await ship.methods.addHub("서울 HUB", time).send({
from: accounts[0],
gas: 1500000,
});
} catch (err) {
console.log(err.message);
}
const senderInfo = await mail.methods.senderInfos(req.body.index).call();
const hubData = await ship.methods.hubInfos(0).call();
var newObj = Object.assign({
receiverAddress: senderInfo.receiverAddress,
hub: hubData.hub,
hub_time: hubData.hub_time,
});
res.send(newObj);
} else {
const senderInfo = await mail.methods.senderInfos(req.body.index).call();
const hubData = await ship.methods.hubInfos(hubLength - 1).call();
var newObj = Object.assign({
receiverAddress: senderInfo.receiverAddress,
hub: hubData.hub,
hub_time: hubData.hub_time,
});
res.send(newObj);
}
});
app.post("/api/getallhub", upload.single("image"), async (req, res) => {
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
const shipAddr = await mail.methods.ship(req.body.index).call();
const ship = new web3.eth.Contract(JSON.parse(Ship.interface), shipAddr);
const hubLength = await ship.methods.hubLength().call();
if (hubLength == 0) {
console.log("현재 데이터 0");
res.send({
length: hubLength,
});
} else {
console.log("현재 데이터" + hubLength + "개");
let datas = [];
for (var i = 0; i < hubLength; i++) {
const value = await ship.methods.hubInfos(i).call();
datas.push(value);
}
const comp = await ship.methods.getComp().call();
console.log(comp);
res.send({
datas: datas,
comp: comp,
length: hubLength,
});
}
});
app.post("/api/addhub", upload.single("image"), async (req, res) => {
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
const shipAddr = await mail.methods.ship(req.body.index).call();
const ship = new web3.eth.Contract(JSON.parse(Ship.interface), shipAddr);
let today = new Date();
const time = today.toLocaleString();
console.log(time);
try {
const accounts = await web3.eth.getAccounts();
await ship.methods.addHub(req.body.hub, time).send({
from: accounts[0],
gas: 1500000,
});
} catch (err) {
console.log(err.message);
}
res.send();
});
app.post("/api/getusermail", upload.single("image"), async (req, res) => {
const mail = new web3.eth.Contract(
JSON.parse(Mail.interface),
req.body.address
);
const senderInfo = await mail.methods.senderInfos(req.body.index).call();
console.log(senderInfo);
const mailInfo = await mail.methods.mailInfos(req.body.index).call();
console.log(mailInfo);
var newObj = {
senderName: senderInfo.senderName,
receiverName: senderInfo.receiverName,
productName: mailInfo.productName,
quantity: mailInfo.quantity,
};
console.log(newObj);
res.send(newObj);
});
app.post("/api/users", upload.single("image"), (req, res) => {
let sql = "INSERT INTO USER VALUES (?,?,?,?,?,?)";
let id = req.body.id;
let pw = req.body.pw;
let name = req.body.name;
let p1 = req.body.p1;
let p2 = req.body.p2;
let p3 = req.body.p3;
let email = req.body.email;
let address = req.body.address;
console.log(id);
console.log(pw);
console.log(name);
console.log(p1 + p2 + p3);
console.log(email);
console.log(address);
let parms = [id, pw, name, p1 + p2 + p3, email, address];
connection.query(sql, parms, (err, rows, fields) => {
res.send(rows);
});
});
app.listen(port, () => console.log(`Listening on port ${port}`));