-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
113 lines (85 loc) · 2.89 KB
/
index.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
const log4js = require("log4js");
const loggerEcho = log4js.getLogger("echo");
loggerEcho.level = "info";
const config = require("./config");
const jwt = require('express-jwt');
const getPublicKey = require('./lib/getPublicKey');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 8080;
function ResponseEcho(list) {
this.countArray = list.length;
this.lastDate = list[this.countArray - 1].receivedDate;
this.list = list;
}
function ItemEcho(body, receivedDate, withAuthentication) {
this.body = body;
this.receivedDate = new Date(receivedDate).toISOString();
this.receivedDateMili = receivedDate;
this.withAuthentication = withAuthentication;
}
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.raw());
app.use(bodyParser.urlencoded({extended: false, type: '*/*'}));
if (config.AUTH0_DOMAIN && config.RESOURCE_SERVER) {
;
loggerEcho.info('Aplicação echo-generic COM autenticação');
loggerEcho.info('AUTH0_DOMAIN: ' + config.AUTH0_DOMAIN);
loggerEcho.info('RESOURCE_SERVER: ' + config.RESOURCE_SERVER);
const jwtCheck = jwt({
secret: getPublicKey(config.AUTH0_DOMAIN),
audience: config.RESOURCE_SERVER,
algorithms: ['RS256'],
issuer: `https://${config.AUTH0_DOMAIN}/`
});
app.use('/auth/*', jwtCheck, function (req, res, next) {
if (req.user) {
loggerEcho.info('Auth: Current user: ' + req.user.sub + ' (scope=' + (req.user.scope || 'N/A') + ')');
}
next();
});
} else {
loggerEcho.info('Aplicação echo-generic SEM autenticação');
}
app.all('/*', (req, res) => {
let dataChegou = Date.now();
res.setHeader("X-response-date", new Date(dataChegou).toISOString());
if ("DELETE" === req.method) {
app.set(req.originalUrl, null);
return res.sendStatus(200);
}
if ("POST" === req.method) {
let lista = app.get(req.originalUrl);
if (!lista) {
lista = [];
}
let withAuthentication = false;
if (req.user) {
withAuthentication = true;
}
let itemEcho = new ItemEcho(req.body, dataChegou, withAuthentication);
lista.push(itemEcho);
app.set(req.originalUrl, lista);
app.set("date_" + req.originalUrl, itemEcho.receivedDate);
return res.sendStatus(200);
}
let salvedBody;
if ("GET" === req.method) {
salvedBody = app.get(req.originalUrl);
}
loggerEcho.log('req: ' + req.originalUrl);
if (salvedBody) {
if (!salvedBody) {
salvedBody = [];
}
let responseEcho = new ResponseEcho(salvedBody);
return res.send(responseEcho);
} else {
return res.sendStatus(404);
}
})
app.listen(port, () => {
loggerEcho.log(`App listening at https://------:${port}`)
})