-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (77 loc) · 2.99 KB
/
app.js
File metadata and controls
86 lines (77 loc) · 2.99 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
'use strict'
global._ = require('underscore')
const querystring = require('querystring')
const uuid = require('uuid/v4')
const request = require('request')
var SwaggerExpress = require('swagger-express-mw')
var SwaggerUi = require('swagger-tools/middleware/swagger-ui')
var express = require('express')
var path = require('path')
var createReverseProxy = require('./api/proxy/reverse.proxy')
var morgan = require('morgan')
const environmentConfigLoader = require('./config/environments/environment.loader')
var app = express()
module.exports = app // for testing
var config = {
appRoot: __dirname // required config
}
const environmentConfiguration = environmentConfigLoader.loadEnvironmentConfig()
console.info('Backend locations: ', environmentConfiguration['backend_locations'])
console.info('Backend trips: ', environmentConfiguration['backend_trips'])
console.info('Backend prices: ', environmentConfiguration['backend_prices'])
console.info('Backend offers: ', environmentConfiguration['backend_offers'])
console.info('Backend prebookings: ', environmentConfiguration['backend_prebookings'])
console.info('Backend bookings: ', environmentConfiguration['backend_bookings'])
SwaggerExpress.create(config, function (err, swaggerExpress) {
if (err) {
throw err
}
// install middleware
app.use(SwaggerUi(swaggerExpress.runner.swagger))
swaggerExpress.register(app)
app.use(morgan(environmentConfiguration.morganFormat))
app.use(express.static(path.join(__dirname, '/public')))
createReverseProxy(app, environmentConfiguration)
app.listen(environmentConfiguration.appPort)
let clientId = '***'
let clientSecret = '***'
if (swaggerExpress.runner.swagger.paths['/offers']) {
console.log('try this: http://localhost:' + environmentConfiguration.appPort + '/app')
global.CONTRACT_ID = 'ACP1024'
global.CONVERSATION_ID = uuid()
global.MOCKED = clientSecret.length <= 3
if (!global.MOCKED) {
global.getToken = function() {
if (global.TOKEN_VALIDITY && (Date.now() + 10000) < global.TOKEN_VALIDITY) {
return global.TOKEN;
} else {
var form = {
grant_type: 'client_credentials',
scope: '',
client_id: clientId,
client_secret: clientSecret
}
var formData = querystring.stringify(form)
var contentLength = formData.length
request({
headers: {
'Content-Length': contentLength,
'Content-Type': 'application/x-www-form-urlencoded'
},
uri: 'https://sso-int.sbb.ch/auth/realms/SBB_Public/protocol/openid-connect/token',
body: formData,
method: 'POST'
}, function (err, response, body) {
if (!!err) {
console.log(err)
}
global.TOKEN = JSON.parse(body).access_token
global.TOKEN_VALIDITY = Date.now() + JSON.parse(body).expires_in
})
return global.TOKEN;
}
}
global.getToken();
}
}
})