-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (111 loc) · 3.75 KB
/
index.js
File metadata and controls
127 lines (111 loc) · 3.75 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
// add express js and first route
import express from 'express';
import bodyParser from 'body-parser';
import { addressSecurity, approvalSecurity, dappSecurity, default as test, supportedChains, tokenSecurity } from './goplus/index.js';
const app = express();
const port = process.env.PORT || 3000;
import swaggerAutogen from 'swagger-autogen';
const outputFile = "./swagger.json";
const endpointsFiles = ["./index.js"];
const config = {}
swaggerAutogen()(outputFile, endpointsFiles, config).then(async () => {
await import('./index.js');
});
//import logger
import logger from 'morgan';
app.use(logger('combined'));
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));
//create documentation html page from swagger.json from node 16
import swaggerUi from 'swagger-ui-express';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const swaggerDocument = require('./swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//add logger to log every requests
// app.use(function (req, res, next) {
// console.log('Time:', Date.now());
// console.log('Request URL:', req.originalUrl);
// console.log('Request Type:', req.method);
// console.log('Request Body:', req.body);
// next();
// });
app.post('/addressSecurity',
function (req, res, next) {
if (req.body) {
if (!req.body.chain) {
return res.status(400).send({ error: 'chain is required' });
}
if (!req.body.address) {
return res.status(400).send({ error: 'address is required' });
}
}
next();
},
async (req, res) => {
let data = await addressSecurity(req.body.chain, req.body.address);
return res.send(data);
});
app.post('/approvalSecurity',
function (req, res, next) {
if (req.body) {
if (!req.body.chain) {
return res.status(400).send({ error: 'chain is required' });
}
if (!req.body.address) {
return res.status(400).send({ error: 'address is required' });
}
}
next();
},
async (req, res) => {
let data = await approvalSecurity(req.body.chain, req.body.address);
return res.send(data);
});
app.get('/supportedChains',
function (req, res, next) {
if (req.query) {
if (!req.query.apiName) {
return res.status(400).send({ error: 'apiName is required' });
}
}
next();
},
async (req, res) => {
let data = await supportedChains(req.query.apiName);
return res.send(data);
}
);
// route to get token security
app.post('/tokenSecurity', function (req, res, next) {
if (req.body) {
if (!req.body.chain) {
return res.status(400).send({ error: 'chain is required' });
}
if (!req.body.address) {
return res.status(400).send({ error: 'address is required' });
}
//if address is not an array return error
if (!Array.isArray(req.body.address)) {
return res.status(400).send({ error: 'address must be an array of tokens' });
}
}
next();
},
async (req, res) => {
let data = await tokenSecurity(req.body.chain, req.body.address);
return res.send(data);
});
app.post('/dappSecurity', function (req, res, next) {
if (req.body) {
if (!req.body.dapp) {
return res.status(400).send({ error: 'dapp is required' });
}
}
next();
},
async (req, res) => {
let data = await dappSecurity(req.body.dapp)
return res.send(data);
});