-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
145 lines (127 loc) · 3.86 KB
/
app.js
File metadata and controls
145 lines (127 loc) · 3.86 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
var forceSsl = require('force-ssl-heroku');
const csp = require('content-security-policy');
const sts = require('strict-transport-security');
const referrerPolicy = require('referrer-policy');
const permissionsPolicy = require('permissions-policy');
const mongoose = require('mongoose');
const helmet = require('helmet');
const axios = require('axios')
const app = express();
const cspPolicy = {
'default-src': csp.SRC_ANY,
'style-src': [
csp.SRC_SELF,
csp.SRC_USAFE_INLINE,
'https://fonts.googleapis.com/',
'https://use.typekit.net'
],
'script-src': [
csp.SRC_SELF,
csp.SRC_USAFE_INLINE,
csp.SRC_UNSAFE_EVAL,
'https://fonts.googleapis.com/',
'http://apis.google.com/',
'http://connect.facebook.net/',
'*.facebook.com'
],
'connect-src': csp.SRC_ANY,
'child-src': [
csp.SRC_SELF,
'https://apis.google.com',
'https://facebook.com',
'https://www.googleapis.com/',
'https://accounts.google.com/'
],
'img-src': [
csp.SRC_ANY,
csp.SRC_DATA
]
};
const globalCSP = csp.getCSP(cspPolicy);
const globalSTS = sts.getSTS({ 'max-age': 31536000, 'includeSubDomains': true });
app.use(referrerPolicy({ policy: 'same-origin' }));
app.use(globalCSP);
app.use(globalSTS);
app.use(helmet.noSniff());
app.use(helmet.frameguard());
app.use(permissionsPolicy({
features: {
fullscreen: ['self']
}
}));
app.use(express.static(__dirname + '/dist/bbooks'));
app.use(express.static(path.join(__dirname + 'node_modules')));
app.use(bodyParser.json()); // support json encoded bodies
// app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(forceSsl);
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB , {
useNewUrlParser: true,
useUnifiedTopology: true,
poolSize: 100
}).then(() => {
console.log("Conectado com o mongo")
}).catch((err) => {
console.log("Erro ao se conectar: " + err)
})
require('./schema/chat')
const Chat = mongoose.model('chats');
app.get('*', (req, res, next) => {
res.sendFile(path.join(__dirname, 'dist/bbooks/index.html'));
});
app.post('/chat', (req, res) => {
const newChat = new Chat({
"exchangeId": req.body.exchangeId
});
newChat.save().then((saved) => {
let host = process.env.COMPETITION_API ? process.env.COMPETITION_API : 'http://localhost:8082'
let url = host +'/exchanges/' + req.body.exchangeId + '/chat/' + saved._id;
axios.put(url, {})
.then(response => {
retorno = {
chat: saved,
exchange: response.data
}
res.json(retorno);
})
.catch(error => {
res.send(error);
})
}).catch(err => res.send(err));
});
app.put('/chat', (req, res) => {
Chat.findOne({ _id: req.body.id }).then((chat) => {
if (chat) {
let mes = req.body.message
mes.data = new Date();
chat.messages.push(mes);
chat.save().then((saved) => res.json(saved));
}
else
res.sendStatus(404);
}).catch(err => res.send(err));
});
app.post('/chat/get', (req, res) => {
Chat.findOne({ _id: req.body.chat.id }).then((chat) => {
if (chat) {
res.json(chat);
}
else
res.sendStatus(404);
}).catch(err => res.send(err));
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
let err = new Error('Not Found');
err.filter = 404;
next(err);
});
// HTTP listener
const PORT = process.env.PORT || 3030;
app.listen(PORT, function () {
console.log('Bbooks running on port ' + PORT);
});
module.exports = app;