/
server.js
162 lines (150 loc) · 5.13 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
// Global Variables
global.postgresserver = process.env.postgresserver || process.env.npm_package_config_postgresConnectionString;
global.rasaserver = process.env.rasaserver || process.env.npm_package_config_rasaserver;
global.rasacoreserver = process.env.rasacoreserver || process.env.npm_package_config_rasacoreserver;
global.jwtsecret = process.env.jwtsecret || process.env.npm_package_config_jwtsecret;
var express = require('express');
var proxy = require('http-proxy-middleware');
var bodyParser = require('body-parser');
var app = express();
var request = require('request');
var routes = require('./routes/index')
var cors = require('cors')
var jwt = require('jsonwebtoken');
const db = require('./db/db')
const url = require('url');
app.use(cors())
app.use(bodyParser.urlencoded({
parameterLimit: 10000,
limit: '2mb',
extended: true
}));
app.use(bodyParser.json({ limit: '2mb' }));
/** Serve static files for UI website on root / */
app.use('/', express.static('web/src/'));
// route middleware to verify a token
app.use(function(req, res, next) {
if(!req.headers.authorization) {
if(req.originalUrl.endsWith('auth') || req.originalUrl.endsWith('authclient')){
console.log("No Token, but got an Auth request. Allowing it");
next();
}else{
return res.status(401).send({
success: false,
message: 'No Authorization header.'
});
}
}else {
// read token and check it
if (req.headers.authorization.split(' ')[0] === 'Bearer'){
var token = req.headers.authorization.split(' ')[1];
// verifies secret and checks exp
jwt.verify(token, global.jwtsecret, function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
// if everything is good, save to request for use in other routes
req.jwt = decoded;
req.original_token=token;
next();
}
});
}else{
// if there is no token send error..angularjs/ chat clients will figure how to create the token.
return res.status(401).send({
success: false,
message: 'No token provided.'
});
}
}
});
app.use('/api/v2/', routes);
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status( err.code || 500 )
.json({
status: 'error',
message: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500)
.json({
status: 'error',
message: err.message
});
});
var listener = app.listen(5001);
checkRasaUI();
checkDB();
checkRasaNLU();
//checkRasaCore();
function checkRasaUI() {
console.log('');
console.log('Rasa UI Server: ' + listener.address().address + ':' + listener.address().port);
console.log('');
}
function checkDB() {
db.one('select current_database(), current_schema(), inet_server_port(), inet_server_addr()')
.then(function (data) {
var dbconn = process.env.postgresserver != undefined ? 'process.env.postgresserver' : 'package.json';
console.log('');
console.log('Postgres DB Connected');
console.log('Using connection string from: ' + dbconn);
console.log('Postgres Server: ' + data["inet_server_addr"] + ':' + data["inet_server_port"]);
console.log('Database:' + data["current_database"]);
console.log('Schema:' + data["current_schema"]);
console.log('');
})
.catch(function (err) {
var dbconn = process.env.postgresserver != undefined ? 'process.env.postgresserver' : 'package.json';
console.log('Postgres DB Connection Error: ' + err);
console.log('Using connection string from: ' + dbconn);
});
}
function checkRasaNLU() {
request(global.rasaserver + '/config', function (error, response, body) {
try {
if (body !== undefined) {
var rasaconn = process.env.rasaserver != undefined ? 'process.env.rasaserver' : 'package.json';
console.log('');
console.log('Rasa NLU Connected');
console.log('Using connection string from: ' + rasaconn);
console.log('Rasa NLU Server: ' + global.rasaserver);
}
if (error !== null) {
var rasaconn = process.env.rasaserver != undefined ? 'process.env.rasaserver' : 'package.json';
console.log('');
console.log('Rasa NLU Error: ' + error);
console.log('Using connection string from: ' + rasaconn);
}
console.log('');
} catch (err) {
console.log('Rasa Connection Error: ' + err);
}
});
}
function checkRasaCore() {
request(global.rasacoreserver + '/config', function (error, response, body) {
try {
if (body !== undefined) {
console.log('');
console.log('Rasa Core Connected');
console.log('Rasa Core Server: ' + global.rasacoreserver);
}
if (error !== null) {
console.log('');
console.log('Rasa Core Error: ' + error);
}
console.log('');
} catch (err) {
console.log('Rasa Connection Error: ' + err);
}
});
}