-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
126 lines (106 loc) · 3.66 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
'use strict';
/**
* CDS Pipe Tool main module
*
* @author David Taieb
*/
var express = require('express');
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require("express-session");
var errorHandler = require('errorhandler');
var morgan = require('morgan');
var bluemixHelperConfig = require('bluemix-helper-config');
var global = bluemixHelperConfig.global;
var configManager = bluemixHelperConfig.configManager;
var sdpLog = require('simple-data-pipe-sdk').logging.getLogger('sdp_common');
//var sdpLog = require('./server/logging/sdpLogger.js').getLogger('sdp_common');
var VCAP_APPLICATION = JSON.parse(process.env.VCAP_APPLICATION || "{}");
var VCAP_SERVICES = JSON.parse(process.env.VCAP_SERVICES || "{}");
var app = global.app = express();
sdpLog.info('Simple Data Pipe is initializing.');
//Enforce https on Bluemix
app.use( function( req, res, next ){
if ( req.headers && req.headers.$wssc === 'http'){
sdpLog.info("Automatically redirecting to https...");
return res.redirect('https://' + req.get('host') + req.url);
}
return next();
});
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(errorHandler({ dumpExceptions:true, showStack:true }));
app.use(expressSession({
secret: "simple_data_pipe",
saveUninitialized: true
}));
var env = app.get('env');
if ('production' === env) {
app.use(morgan('dev'));
}
if ('development' === env || 'test' === env) {
app.use(morgan('dev'));
app.use(errorHandler()); // Error handler - has to be last
}
var port = process.env.VCAP_APP_PORT || configManager.get("DEV_PORT") || 8082;
if (!process.env.VCAP_APP_HOST){
//Running locally. Salesforce requires authCallbacks to use SSL by default
global.appHost = "https://127.0.0.1";
global.appPort = port;
}
//Configure security if we are bound to an SSO service
var ssoService = bluemixHelperConfig.vcapServices.getService( "pipes-sso" );
if ( ssoService ){
sdpLog.info("Security is enabled.");
require('bluemix-helper-sso')(app, {
ssoService: ssoService,
relaxedUrls:[
"/js", "/img", "/css", "/bower_components", "templates"
],
createSessionStore: function( session ){
//Create a session store based on redis if available, if not, use the default in-memory store
var redisService = bluemixHelperConfig.vcapServices.getService("pipes-redis");
if ( redisService ){
var redisStore = require('connect-redis')(session);
return new redisStore({
host: redisService.credentials.hostname,
port: redisService.credentials.port,
pass: redisService.credentials.password
});
}
return null;
}
});
}else{
app.get("/userid", function( req, res, next ){
res.status(200).end();
})
}
app.use(express.static(path.join(__dirname, 'app')));
//Configure the endpoints
require("./server/connectorAPI").initEndPoints(app); //Pipe connector API
var wssConfigurator = require("./server/pipeAPI")(app); //Pipe configuration
var connected = function() {
sdpLog.info('Simple Data Pipe application started on port %s : %s', port, Date(Date.now()));
};
var options = {
key: fs.readFileSync('development/certs/server/my-server.key.pem'),
cert: fs.readFileSync('development/certs/server/my-server.crt.pem')
};
var server = null;
if (process.env.VCAP_APP_HOST){
server = require('http').createServer(app);
server.listen(port,
process.env.VCAP_APP_HOST,
connected);
}else{
server = require('https').createServer(options, app);
server.listen(port,connected);
}
if ( wssConfigurator && server ){
wssConfigurator( server );
}
require("cf-deployment-tracker-client").track();