cloudcms-server 0.9.255 → 0.9.260
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/broadcast/providers/redis.js +24 -49
- package/clients/nrp.js +117 -0
- package/clients/redis.js +64 -0
- package/launchpad/index.js +161 -11
- package/launchpad/launchers/cluster.js +87 -109
- package/launchpad/launchers/redis.js +53 -0
- package/launchpad/launchers/single.js +30 -22
- package/locks/locks.js +4 -3
- package/locks/providers/cluster.js +1 -1
- package/locks/providers/memory.js +1 -1
- package/locks/providers/redis.js +62 -82
- package/middleware/admin/admin.js +2 -2
- package/middleware/authentication/providers/saml.js +2 -1
- package/middleware/awareness/awareness.js +12 -2
- package/middleware/awareness/providers/redis.js +225 -199
- package/middleware/cache/providers/redis.js +127 -89
- package/middleware/cloudcms/cloudcms.js +5 -1
- package/middleware/proxy/proxy.js +2 -1
- package/middleware/stores/stores.js +2 -2
- package/middleware/virtual-config/virtual-config.js +4 -4
- package/middleware/wcm/wcm.js +4 -4
- package/package.json +10 -6
- package/server/index.js +624 -592
- package/temp/clusterlock/index.js +2 -0
- package/temp/passport-saml/LICENSE +23 -0
- package/temp/passport-saml/README.md +406 -0
- package/temp/passport-saml/lib/node-saml/algorithms.d.ts +5 -0
- package/temp/passport-saml/lib/node-saml/algorithms.js +41 -0
- package/temp/passport-saml/lib/node-saml/algorithms.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/index.d.ts +3 -0
- package/temp/passport-saml/lib/node-saml/index.js +6 -0
- package/temp/passport-saml/lib/node-saml/index.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.d.ts +45 -0
- package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.js +86 -0
- package/temp/passport-saml/lib/node-saml/inmemory-cache-provider.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/saml-post-signing.d.ts +3 -0
- package/temp/passport-saml/lib/node-saml/saml-post-signing.js +15 -0
- package/temp/passport-saml/lib/node-saml/saml-post-signing.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/saml.d.ts +77 -0
- package/temp/passport-saml/lib/node-saml/saml.js +1170 -0
- package/temp/passport-saml/lib/node-saml/saml.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/types.d.ts +95 -0
- package/temp/passport-saml/lib/node-saml/types.js +8 -0
- package/temp/passport-saml/lib/node-saml/types.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/utility.d.ts +3 -0
- package/temp/passport-saml/lib/node-saml/utility.js +19 -0
- package/temp/passport-saml/lib/node-saml/utility.js.map +1 -0
- package/temp/passport-saml/lib/node-saml/xml.d.ts +21 -0
- package/temp/passport-saml/lib/node-saml/xml.js +140 -0
- package/temp/passport-saml/lib/node-saml/xml.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/index.d.ts +6 -0
- package/temp/passport-saml/lib/passport-saml/index.js +11 -0
- package/temp/passport-saml/lib/passport-saml/index.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.d.ts +13 -0
- package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.js +63 -0
- package/temp/passport-saml/lib/passport-saml/multiSamlStrategy.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/strategy.d.ts +20 -0
- package/temp/passport-saml/lib/passport-saml/strategy.js +167 -0
- package/temp/passport-saml/lib/passport-saml/strategy.js.map +1 -0
- package/temp/passport-saml/lib/passport-saml/types.d.ts +51 -0
- package/temp/passport-saml/lib/passport-saml/types.js +11 -0
- package/temp/passport-saml/lib/passport-saml/types.js.map +1 -0
- package/temp/passport-saml/package.json +96 -0
- package/util/auth.js +1 -1
- package/util/cloudcms.js +6 -6
- package/util/proxy-factory.js +22 -7
- package/util/redis.js +113 -0
- package/util/util.js +1 -1
- package/launchpad/launchers/sticky-cluster.js +0 -43
package/server/index.js
CHANGED
|
@@ -16,22 +16,20 @@ var session = require('express-session');
|
|
|
16
16
|
var cookieParser = require('cookie-parser');
|
|
17
17
|
var flash = require("connect-flash");
|
|
18
18
|
|
|
19
|
-
const redis = require('redis');
|
|
19
|
+
//const redis = require('redis');
|
|
20
20
|
const connectRedis = require('connect-redis');
|
|
21
21
|
|
|
22
22
|
// we don't bind a single passport - instead, we get the constructor here by hand
|
|
23
23
|
var Passport = require("passport").Passport;
|
|
24
24
|
|
|
25
25
|
var util = require("../util/util");
|
|
26
|
+
var redisHelper = require("../util/redis");
|
|
26
27
|
|
|
27
28
|
var launchPad = require("../launchpad/index");
|
|
28
29
|
var cluster = require("cluster");
|
|
29
30
|
|
|
30
31
|
var requestParam = require("request-param")();
|
|
31
32
|
|
|
32
|
-
var app = express();
|
|
33
|
-
app.disable('x-powered-by');
|
|
34
|
-
|
|
35
33
|
// cloudcms app server support
|
|
36
34
|
var main = require("../index");
|
|
37
35
|
|
|
@@ -48,11 +46,27 @@ toobusy.interval(250);
|
|
|
48
46
|
|
|
49
47
|
var responseTime = require("response-time");
|
|
50
48
|
|
|
49
|
+
// safely checks for the existence of a path
|
|
50
|
+
var safeExists = function(_path)
|
|
51
|
+
{
|
|
52
|
+
var exists = false;
|
|
53
|
+
try
|
|
54
|
+
{
|
|
55
|
+
exists = fs.existsSync(_path);
|
|
56
|
+
}
|
|
57
|
+
catch (e)
|
|
58
|
+
{
|
|
59
|
+
// swallow
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return exists;
|
|
63
|
+
}
|
|
64
|
+
|
|
51
65
|
var requestCounter = 0;
|
|
52
66
|
|
|
53
67
|
// holds configuration settings
|
|
54
68
|
var SETTINGS = {
|
|
55
|
-
"setup": "single", // single, cluster,
|
|
69
|
+
"setup": "single", // single, cluster, redis
|
|
56
70
|
"name": "Cloud CMS Application Server",
|
|
57
71
|
"socketFunctions": [],
|
|
58
72
|
"routeFunctions": [],
|
|
@@ -309,17 +323,67 @@ var SETTINGS = {
|
|
|
309
323
|
}
|
|
310
324
|
};
|
|
311
325
|
|
|
326
|
+
// always push core tag helpers to the front
|
|
327
|
+
SETTINGS.dustFunctions.unshift(coreHelpers);
|
|
328
|
+
|
|
329
|
+
// if SETTINGS.errorFunctions is empty, plug in a default error handler
|
|
330
|
+
if (SETTINGS.errorFunctions.length === 0)
|
|
331
|
+
{
|
|
332
|
+
SETTINGS.errorFunctions.push(main.defaultErrorHandler);
|
|
333
|
+
}
|
|
334
|
+
else
|
|
335
|
+
{
|
|
336
|
+
// otherwise, if they plugged in a custom error handler, make sure we at least have a console logger ahead of it
|
|
337
|
+
// so that things are sure to get logged out to console
|
|
338
|
+
SETTINGS.errorFunctions.unshift(main.consoleErrorLogger);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// insert an error handler to handle refresh token failures
|
|
342
|
+
SETTINGS.errorFunctions.unshift(main.refreshTokenErrorHandler);
|
|
343
|
+
|
|
344
|
+
// CLOUDCMS_HOSTS_PATH environment variable
|
|
345
|
+
// assume /hosts with optional fallback to /System/Volumes/Data/hosts for MacOS support
|
|
346
|
+
if (!process.env.CLOUDCMS_HOSTS_PATH)
|
|
347
|
+
{
|
|
348
|
+
process.env.CLOUDCMS_HOSTS_PATH = "/hosts";
|
|
349
|
+
|
|
350
|
+
if (!safeExists(process.env.CLOUDCMS_HOSTS_PATH))
|
|
351
|
+
{
|
|
352
|
+
if (safeExists("/System/Volumes/Data/hosts"))
|
|
353
|
+
{
|
|
354
|
+
process.env.CLOUDCMS_HOSTS_PATH = "/System/Volumes/Data/hosts";
|
|
355
|
+
}
|
|
356
|
+
else
|
|
357
|
+
{
|
|
358
|
+
const homedir = require('os').homedir();
|
|
359
|
+
|
|
360
|
+
if (safeExists(homedir + "/hosts"))
|
|
361
|
+
{
|
|
362
|
+
process.env.CLOUDCMS_HOSTS_PATH = homedir + "/hosts";
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
|
|
312
384
|
// runs on 2999 by default
|
|
313
385
|
process.env.PORT = process.env.PORT || 2999;
|
|
314
386
|
|
|
315
|
-
// allows for specification of alternative transports
|
|
316
|
-
// SETTINGS.socketTransports = [
|
|
317
|
-
// 'websocket',
|
|
318
|
-
// 'xhr-polling',
|
|
319
|
-
// 'jsonp-polling',
|
|
320
|
-
// 'polling'
|
|
321
|
-
// ];
|
|
322
|
-
|
|
323
387
|
var exports = module.exports;
|
|
324
388
|
|
|
325
389
|
/**
|
|
@@ -503,62 +567,19 @@ var _start = function(overrides, callback) {
|
|
|
503
567
|
callback = function() {};
|
|
504
568
|
}
|
|
505
569
|
|
|
506
|
-
// always push core tag helpers to the front
|
|
507
|
-
SETTINGS.dustFunctions.unshift(coreHelpers);
|
|
508
|
-
|
|
509
|
-
// if SETTINGS.errorFunctions is empty, plug in a default error handler
|
|
510
|
-
if (SETTINGS.errorFunctions.length === 0)
|
|
511
|
-
{
|
|
512
|
-
SETTINGS.errorFunctions.push(main.defaultErrorHandler);
|
|
513
|
-
}
|
|
514
|
-
else
|
|
515
|
-
{
|
|
516
|
-
// otherwise, if they plugged in a custom error handler, make sure we at least have a console logger ahead of it
|
|
517
|
-
// so that things are sure to get logged out to console
|
|
518
|
-
SETTINGS.errorFunctions.unshift(main.consoleErrorLogger);
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
// insert an error handler to handle refresh token failures
|
|
522
|
-
SETTINGS.errorFunctions.unshift(main.refreshTokenErrorHandler);
|
|
523
|
-
|
|
524
570
|
// create our master config
|
|
525
571
|
var config = clone(SETTINGS);
|
|
526
572
|
if (overrides) {
|
|
527
573
|
util.merge(overrides, config);
|
|
528
574
|
}
|
|
529
|
-
|
|
530
|
-
// assume for launchpad
|
|
531
|
-
if (!config.setup) {
|
|
532
|
-
config.setup = "single";
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
launchPad({
|
|
536
|
-
"setup": config.setup,
|
|
537
|
-
"factory": function(done) {
|
|
538
|
-
startSlave(config, function(app, server) {
|
|
539
|
-
done(server);
|
|
540
|
-
});
|
|
541
|
-
},
|
|
542
|
-
"report": function() {
|
|
543
|
-
runFunctions(config.reportFunctions, [], function(err) {
|
|
544
|
-
// todo
|
|
545
|
-
});
|
|
546
|
-
},
|
|
547
|
-
"complete": function() {
|
|
548
|
-
callback();
|
|
549
|
-
}
|
|
550
|
-
});
|
|
551
|
-
};
|
|
552
|
-
|
|
553
|
-
var startSlave = function(config, afterStartFn)
|
|
554
|
-
{
|
|
575
|
+
|
|
555
576
|
// set up modes
|
|
556
577
|
process.env.CLOUDCMS_APPSERVER_MODE = "development";
|
|
557
|
-
|
|
558
|
-
if (process.env.NODE_ENV
|
|
578
|
+
|
|
579
|
+
if (process.env.NODE_ENV === "production") {
|
|
559
580
|
process.env.CLOUDCMS_APPSERVER_MODE = "production";
|
|
560
581
|
}
|
|
561
|
-
|
|
582
|
+
|
|
562
583
|
/*
|
|
563
584
|
// set up domain hosting
|
|
564
585
|
// if not otherwise specified, we assume hosting at *.cloudcms.net
|
|
@@ -569,10 +590,10 @@ var startSlave = function(config, afterStartFn)
|
|
|
569
590
|
}
|
|
570
591
|
}
|
|
571
592
|
*/
|
|
572
|
-
|
|
593
|
+
|
|
573
594
|
// store config on process instance
|
|
574
595
|
process.configuration = config;
|
|
575
|
-
|
|
596
|
+
|
|
576
597
|
// some config overrides can come in through process.configuration
|
|
577
598
|
if (process.configuration) {
|
|
578
599
|
if (process.configuration.virtualHost && process.configuration.virtualHost.domain) {
|
|
@@ -586,7 +607,7 @@ var startSlave = function(config, afterStartFn)
|
|
|
586
607
|
if (process.env.CLOUDCMS_VIRTUAL_HOST_DOMAIN) {
|
|
587
608
|
process.env.CLOUDCMS_VIRTUAL_HOST_DOMAIN = process.env.CLOUDCMS_VIRTUAL_HOST_DOMAIN.toLowerCase();
|
|
588
609
|
}
|
|
589
|
-
|
|
610
|
+
|
|
590
611
|
if (!process.env.CLOUDCMS_STANDALONE_HOST) {
|
|
591
612
|
process.env.CLOUDCMS_STANDALONE_HOST = "local";
|
|
592
613
|
}
|
|
@@ -628,6 +649,11 @@ var startSlave = function(config, afterStartFn)
|
|
|
628
649
|
if (!process.configuration.session) {
|
|
629
650
|
process.configuration.session = {};
|
|
630
651
|
}
|
|
652
|
+
// auto-configuration for redis?
|
|
653
|
+
if (process.env.CLOUDCMS_REDIS_URL || (process.env.CLOUDCMS_REDIS_ENDPOINT && process.env.CLOUDCMS_REDIS_PORT)) {
|
|
654
|
+
process.env.CLOUDCMS_SESSION_TYPE = "redis";
|
|
655
|
+
}
|
|
656
|
+
|
|
631
657
|
if (process.env.CLOUDCMS_SESSION_TYPE) {
|
|
632
658
|
process.configuration.session.enabled = true;
|
|
633
659
|
process.configuration.session.type = process.env.CLOUDCMS_SESSION_TYPE;
|
|
@@ -635,7 +661,50 @@ var startSlave = function(config, afterStartFn)
|
|
|
635
661
|
if (process.env.CLOUDCMS_SESSION_SECRET) {
|
|
636
662
|
process.configuration.session.secret = process.env.CLOUDCMS_SESSION_SECRET;
|
|
637
663
|
}
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
// determine the max files
|
|
667
|
+
util.maxFiles(function(err, maxFiles) {
|
|
668
|
+
process.env.CLOUDCMS_MAX_FILES = maxFiles;
|
|
669
|
+
|
|
670
|
+
// assume for launchpad
|
|
671
|
+
if (!config.setup) {
|
|
672
|
+
config.setup = "single";
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
launchPad(config.setup, config, {
|
|
676
|
+
"createHttpServer": function(app, done) {
|
|
677
|
+
createHttpServer(app, function(err, httpServer) {
|
|
678
|
+
done(err, httpServer);
|
|
679
|
+
});
|
|
680
|
+
},
|
|
681
|
+
"startServer": function(config, done) {
|
|
682
|
+
startServer(config, function(err, app, httpServer, httpServerPort) {
|
|
683
|
+
done(err, app, httpServer, httpServerPort);
|
|
684
|
+
});
|
|
685
|
+
},
|
|
686
|
+
"configureServer": function(config, app, httpServer, done) {
|
|
687
|
+
configureServer(config, app, httpServer, function(err) {
|
|
688
|
+
done(err);
|
|
689
|
+
});
|
|
690
|
+
},
|
|
691
|
+
"report": function(config) {
|
|
692
|
+
runFunctions(config.reportFunctions, [], function(err) {
|
|
693
|
+
// todo
|
|
694
|
+
});
|
|
695
|
+
},
|
|
696
|
+
"complete": function(config, err) {
|
|
697
|
+
callback(err);
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
});
|
|
701
|
+
};
|
|
638
702
|
|
|
703
|
+
var startServer = function(config, startServerFinishedFn)
|
|
704
|
+
{
|
|
705
|
+
var app = express();
|
|
706
|
+
app.disable('x-powered-by');
|
|
707
|
+
|
|
639
708
|
// session store
|
|
640
709
|
var initializedSession = null;
|
|
641
710
|
if (process.configuration.session)
|
|
@@ -670,25 +739,20 @@ var startSlave = function(config, afterStartFn)
|
|
|
670
739
|
}
|
|
671
740
|
else if (process.configuration.session.type === "redis")
|
|
672
741
|
{
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
var redisClient = redis.createClient(redisPort, redisHost, redisOptions);
|
|
688
|
-
|
|
689
|
-
var RedisStore = connectRedis(session);
|
|
690
|
-
sessionConfig.store = new RedisStore({ client: redisClient });
|
|
691
|
-
}
|
|
742
|
+
(async function() {
|
|
743
|
+
var redisOptions = redisHelper.redisOptions();
|
|
744
|
+
redisHelper.createAndConnect(redisOptions, function(err, redisClient) {
|
|
745
|
+
|
|
746
|
+
if (err) {
|
|
747
|
+
console.error(err);
|
|
748
|
+
}
|
|
749
|
+
else
|
|
750
|
+
{
|
|
751
|
+
var RedisStore = connectRedis(session);
|
|
752
|
+
sessionConfig.store = new RedisStore({ client: redisClient });
|
|
753
|
+
}
|
|
754
|
+
});
|
|
755
|
+
})();
|
|
692
756
|
}
|
|
693
757
|
else if (process.configuration.session.type === "memory" || !process.configuration.session.type)
|
|
694
758
|
{
|
|
@@ -703,604 +767,376 @@ var startSlave = function(config, afterStartFn)
|
|
|
703
767
|
initializedSession = session(sessionConfig);
|
|
704
768
|
}
|
|
705
769
|
}
|
|
770
|
+
|
|
771
|
+
// global temp directory
|
|
772
|
+
util.createTempDirectory(function(err, tempDirectory) {
|
|
773
|
+
process.env.CLOUDCMS_TEMPDIR_PATH = tempDirectory;
|
|
706
774
|
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
{
|
|
710
|
-
var exists = false;
|
|
711
|
-
try
|
|
712
|
-
{
|
|
713
|
-
exists = fs.existsSync(_path);
|
|
714
|
-
}
|
|
715
|
-
catch (e)
|
|
716
|
-
{
|
|
717
|
-
// swallow
|
|
718
|
-
}
|
|
775
|
+
// global service starts
|
|
776
|
+
main.init(app, function (err) {
|
|
719
777
|
|
|
720
|
-
|
|
721
|
-
}
|
|
778
|
+
app.enable('strict routing');
|
|
722
779
|
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
780
|
+
////////////////////////////////////////////////////////////////////////////
|
|
781
|
+
//
|
|
782
|
+
// BASE CONFIGURATION
|
|
783
|
+
//
|
|
784
|
+
// Configures NodeJS app server using dustjs templating engine
|
|
785
|
+
// Runs on port 3000 by default
|
|
786
|
+
//
|
|
787
|
+
////////////////////////////////////////////////////////////////////////////
|
|
728
788
|
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
789
|
+
// all environments
|
|
790
|
+
app.set('port', process.env.PORT);
|
|
791
|
+
app.set('views', process.env.CLOUDCMS_APPSERVER_BASE_PATH + "/views");
|
|
792
|
+
|
|
793
|
+
if (config.viewEngine === "dust")
|
|
732
794
|
{
|
|
733
|
-
|
|
795
|
+
var cons = require('consolidate');
|
|
796
|
+
|
|
797
|
+
app.set('view engine', 'html');
|
|
798
|
+
app.set('view engine', 'dust');
|
|
799
|
+
app.engine('html', cons.dust);
|
|
800
|
+
app.engine('dust', cons.dust);
|
|
734
801
|
}
|
|
735
|
-
else
|
|
802
|
+
else if (config.viewEngine === "handlebars" || config.viewEngine === "hbs")
|
|
736
803
|
{
|
|
737
|
-
|
|
804
|
+
var hbs = require('hbs');
|
|
738
805
|
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
806
|
+
app.set('view engine', 'html');
|
|
807
|
+
app.set('view engine', 'hbs');
|
|
808
|
+
app.engine('html', hbs.__express);
|
|
809
|
+
app.engine('hbs', hbs.__express);
|
|
743
810
|
}
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
// global temp directory
|
|
748
|
-
util.createTempDirectory(function(err, tempDirectory) {
|
|
749
|
-
process.env.CLOUDCMS_TEMPDIR_PATH = tempDirectory;
|
|
750
|
-
|
|
751
|
-
// determine the max files
|
|
752
|
-
util.maxFiles(function(err, maxFiles) {
|
|
753
|
-
|
|
754
|
-
process.env.CLOUDCMS_MAX_FILES = maxFiles;
|
|
755
|
-
|
|
756
|
-
// global service starts
|
|
757
|
-
main.init(app, function (err) {
|
|
758
811
|
|
|
759
|
-
|
|
812
|
+
////////////////////////////////////////////////////////////////////////////
|
|
813
|
+
//
|
|
814
|
+
// VIRTUAL SUPPORT
|
|
815
|
+
//
|
|
816
|
+
// Configure NodeJS to load virtual driver and configure for virtual descriptors
|
|
817
|
+
// ahead of anything else running.
|
|
818
|
+
//
|
|
819
|
+
////////////////////////////////////////////////////////////////////////////
|
|
820
|
+
|
|
821
|
+
// custom morgan logger
|
|
822
|
+
morgan(function (tokens, req, res) {
|
|
823
|
+
|
|
824
|
+
var status = res.statusCode;
|
|
825
|
+
var len = parseInt(res.getHeader('Content-Length'), 10);
|
|
826
|
+
var host = req.domainHost;
|
|
827
|
+
if (req.virtualHost) {
|
|
828
|
+
host = req.virtualHost;
|
|
829
|
+
}
|
|
760
830
|
|
|
761
|
-
|
|
762
|
-
//
|
|
763
|
-
// BASE CONFIGURATION
|
|
764
|
-
//
|
|
765
|
-
// Configures NodeJS app server using dustjs templating engine
|
|
766
|
-
// Runs on port 3000 by default
|
|
767
|
-
//
|
|
768
|
-
////////////////////////////////////////////////////////////////////////////
|
|
831
|
+
len = isNaN(len) ? '0b' : len = bytes(len);
|
|
769
832
|
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
833
|
+
var d = new Date();
|
|
834
|
+
var dateString = d.toDateString();
|
|
835
|
+
var timeString = d.toTimeString();
|
|
773
836
|
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
var cons = require('consolidate');
|
|
837
|
+
// gray color
|
|
838
|
+
var grayColor = "\x1b[90m";
|
|
777
839
|
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
840
|
+
// status color
|
|
841
|
+
var color = 32;
|
|
842
|
+
if (status >= 500) {
|
|
843
|
+
color = 31;
|
|
782
844
|
}
|
|
783
|
-
else if (
|
|
784
|
-
|
|
785
|
-
var hbs = require('hbs');
|
|
786
|
-
|
|
787
|
-
app.set('view engine', 'html');
|
|
788
|
-
app.set('view engine', 'hbs');
|
|
789
|
-
app.engine('html', hbs.__express);
|
|
790
|
-
app.engine('hbs', hbs.__express);
|
|
845
|
+
else if (status >= 400) {
|
|
846
|
+
color = 33;
|
|
791
847
|
}
|
|
848
|
+
else if (status >= 300) {
|
|
849
|
+
color = 36;
|
|
850
|
+
}
|
|
851
|
+
var statusColor = "\x1b[" + color + "m";
|
|
792
852
|
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
// VIRTUAL SUPPORT
|
|
796
|
-
//
|
|
797
|
-
// Configure NodeJS to load virtual driver and configure for virtual descriptors
|
|
798
|
-
// ahead of anything else running.
|
|
799
|
-
//
|
|
800
|
-
////////////////////////////////////////////////////////////////////////////
|
|
801
|
-
|
|
802
|
-
// custom morgan logger
|
|
803
|
-
morgan(function (tokens, req, res) {
|
|
804
|
-
|
|
805
|
-
var status = res.statusCode;
|
|
806
|
-
var len = parseInt(res.getHeader('Content-Length'), 10);
|
|
807
|
-
var host = req.domainHost;
|
|
808
|
-
if (req.virtualHost) {
|
|
809
|
-
host = req.virtualHost;
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
len = isNaN(len) ? '0b' : len = bytes(len);
|
|
813
|
-
|
|
814
|
-
var d = new Date();
|
|
815
|
-
var dateString = d.toDateString();
|
|
816
|
-
var timeString = d.toTimeString();
|
|
853
|
+
// final color
|
|
854
|
+
var finalColor = "\x1b[0m";
|
|
817
855
|
|
|
818
|
-
|
|
819
|
-
|
|
856
|
+
if (process.env.CLOUDCMS_APPSERVER_MODE === "production")
|
|
857
|
+
{
|
|
858
|
+
grayColor = "";
|
|
859
|
+
statusColor = "";
|
|
860
|
+
finalColor = "";
|
|
861
|
+
}
|
|
820
862
|
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
863
|
+
var message = '';
|
|
864
|
+
message += grayColor + '<' + req.id + '> ';
|
|
865
|
+
message += grayColor + '[' + dateString + ' ' + timeString + '] ';
|
|
866
|
+
message += grayColor + host + ' ';
|
|
867
|
+
//message += grayColor + '(' + req.ip + ') ';
|
|
868
|
+
message += statusColor + res.statusCode + ' ';
|
|
869
|
+
message += statusColor + (new Date - req._startTime) + ' ms ';
|
|
870
|
+
message += grayColor + '"' + req.method + ' ';
|
|
871
|
+
message += grayColor + req.originalUrl + '" ';
|
|
872
|
+
message += grayColor + len + ' ';
|
|
873
|
+
message += finalColor;
|
|
874
|
+
|
|
875
|
+
return message;
|
|
876
|
+
});
|
|
833
877
|
|
|
834
|
-
|
|
835
|
-
|
|
878
|
+
/*
|
|
879
|
+
// debug headers being set
|
|
880
|
+
app.use(function(req, res, next) {
|
|
881
|
+
var setHeader = res.setHeader;
|
|
882
|
+
res.setHeader = function(a,b) {
|
|
883
|
+
console.trace("Writing header: " + a + " = " + b);
|
|
884
|
+
setHeader.call(this, a,b);
|
|
885
|
+
};
|
|
886
|
+
next();
|
|
887
|
+
});
|
|
888
|
+
*/
|
|
836
889
|
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
890
|
+
// middleware which blocks requests when we're too busy
|
|
891
|
+
app.use(function(req, res, next) {
|
|
892
|
+
if (toobusy()) {
|
|
893
|
+
res.status(503).send("The web application is too busy to serve this request. Please try again.");
|
|
894
|
+
} else {
|
|
895
|
+
next();
|
|
896
|
+
}
|
|
897
|
+
});
|
|
843
898
|
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
message += statusColor + (new Date - req._startTime) + ' ms ';
|
|
851
|
-
message += grayColor + '"' + req.method + ' ';
|
|
852
|
-
message += grayColor + req.originalUrl + '" ';
|
|
853
|
-
message += grayColor + len + ' ';
|
|
854
|
-
message += finalColor;
|
|
855
|
-
|
|
856
|
-
return message;
|
|
857
|
-
});
|
|
899
|
+
// add req.id re
|
|
900
|
+
app.use(function (req, res, next) {
|
|
901
|
+
requestCounter++;
|
|
902
|
+
req.id = requestCounter;
|
|
903
|
+
next();
|
|
904
|
+
});
|
|
858
905
|
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
app.use(function(req, res, next) {
|
|
862
|
-
var setHeader = res.setHeader;
|
|
863
|
-
res.setHeader = function(a,b) {
|
|
864
|
-
console.trace("Writing header: " + a + " = " + b);
|
|
865
|
-
setHeader.call(this, a,b);
|
|
866
|
-
};
|
|
867
|
-
next();
|
|
868
|
-
});
|
|
869
|
-
*/
|
|
870
|
-
|
|
871
|
-
// middleware which blocks requests when we're too busy
|
|
872
|
-
app.use(function(req, res, next) {
|
|
873
|
-
if (toobusy()) {
|
|
874
|
-
res.status(503).send("The web application is too busy to serve this request. Please try again.");
|
|
875
|
-
} else {
|
|
876
|
-
next();
|
|
877
|
-
}
|
|
878
|
-
});
|
|
906
|
+
// APPLY CUSTOM INIT FUNCTIONS
|
|
907
|
+
runFunctions(config.initFunctions, [app], function (err) {
|
|
879
908
|
|
|
880
|
-
//
|
|
909
|
+
// retain originalUrl and originalPath since these can get modified along the way
|
|
881
910
|
app.use(function (req, res, next) {
|
|
882
|
-
|
|
883
|
-
req.
|
|
911
|
+
req.originalUrl = req.url;
|
|
912
|
+
req.originalPath = req.path;
|
|
884
913
|
next();
|
|
885
914
|
});
|
|
886
915
|
|
|
887
|
-
//
|
|
888
|
-
|
|
916
|
+
// req.param method
|
|
917
|
+
app.use(requestParam);
|
|
889
918
|
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
req.originalUrl = req.url;
|
|
893
|
-
req.originalPath = req.path;
|
|
894
|
-
next();
|
|
895
|
-
});
|
|
896
|
-
|
|
897
|
-
// req.param method
|
|
898
|
-
app.use(requestParam);
|
|
919
|
+
// add req.log function
|
|
920
|
+
app.use(function (req, res, next) {
|
|
899
921
|
|
|
900
|
-
|
|
901
|
-
app.use(function (req, res, next) {
|
|
922
|
+
req._log = req.log = function (text/*, warn*/) {
|
|
902
923
|
|
|
903
|
-
|
|
924
|
+
var host = req.domainHost;
|
|
925
|
+
if (req.virtualHost)
|
|
926
|
+
{
|
|
927
|
+
host = req.virtualHost;
|
|
928
|
+
}
|
|
904
929
|
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
host = req.virtualHost;
|
|
909
|
-
}
|
|
930
|
+
var timestamp = moment(new Date()).format("MM/DD/YYYY HH:mm:ss Z");
|
|
931
|
+
var grayColor = "\x1b[90m";
|
|
932
|
+
var finalColor = "\x1b[0m";
|
|
910
933
|
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
934
|
+
// in production, don't use colors
|
|
935
|
+
if (process.env.CLOUDCMS_APPSERVER_MODE === "production")
|
|
936
|
+
{
|
|
937
|
+
grayColor = "";
|
|
938
|
+
finalColor = "";
|
|
939
|
+
}
|
|
914
940
|
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
941
|
+
var message = '';
|
|
942
|
+
message += grayColor + '<' + req.id + '> ';
|
|
943
|
+
if (cluster.worker && cluster.worker.id)
|
|
944
|
+
{
|
|
945
|
+
message += grayColor + '(' + cluster.worker.id + ') ';
|
|
946
|
+
}
|
|
947
|
+
message += grayColor + '[' + timestamp + '] ';
|
|
948
|
+
message += grayColor + host + ' ';
|
|
949
|
+
message += grayColor + text + '';
|
|
950
|
+
message += finalColor;
|
|
921
951
|
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
message += grayColor + '[' + timestamp + '] ';
|
|
929
|
-
message += grayColor + host + ' ';
|
|
930
|
-
message += grayColor + text + '';
|
|
931
|
-
message += finalColor;
|
|
952
|
+
/*
|
|
953
|
+
if (warn)
|
|
954
|
+
{
|
|
955
|
+
message = "\r\n**** SLOW RESPONSE ****\r\n" + message + "\r\n";
|
|
956
|
+
}
|
|
957
|
+
*/
|
|
932
958
|
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
{
|
|
936
|
-
message = "\r\n**** SLOW RESPONSE ****\r\n" + message + "\r\n";
|
|
937
|
-
}
|
|
938
|
-
*/
|
|
959
|
+
console.log(message);
|
|
960
|
+
};
|
|
939
961
|
|
|
940
|
-
|
|
941
|
-
|
|
962
|
+
next();
|
|
963
|
+
});
|
|
942
964
|
|
|
943
|
-
|
|
944
|
-
|
|
965
|
+
// common interceptors and config
|
|
966
|
+
main.common1(app);
|
|
945
967
|
|
|
946
|
-
|
|
947
|
-
|
|
968
|
+
// general logging of requests
|
|
969
|
+
// gather statistics on response time
|
|
970
|
+
app.use(responseTime(function (req, res, time) {
|
|
948
971
|
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
972
|
+
var warn = false;
|
|
973
|
+
if (time > 1000)
|
|
974
|
+
{
|
|
975
|
+
warn = true;
|
|
976
|
+
}
|
|
952
977
|
|
|
953
|
-
|
|
954
|
-
|
|
978
|
+
var requestPath = req.originalPath;
|
|
979
|
+
if (requestPath)
|
|
980
|
+
{
|
|
981
|
+
var filter = false;
|
|
982
|
+
if (requestPath.indexOf("/login") > -1)
|
|
955
983
|
{
|
|
956
|
-
|
|
984
|
+
filter = true;
|
|
957
985
|
}
|
|
958
|
-
|
|
959
|
-
var requestPath = req.originalPath;
|
|
960
|
-
if (requestPath)
|
|
986
|
+
if (requestPath.indexOf("/token") > -1)
|
|
961
987
|
{
|
|
962
|
-
|
|
963
|
-
if (requestPath.indexOf("/login") > -1)
|
|
964
|
-
{
|
|
965
|
-
filter = true;
|
|
966
|
-
}
|
|
967
|
-
if (requestPath.indexOf("/token") > -1)
|
|
968
|
-
{
|
|
969
|
-
filter = true;
|
|
970
|
-
}
|
|
971
|
-
if (filter)
|
|
972
|
-
{
|
|
973
|
-
requestPath = util.stripQueryStringFromUrl(requestPath);
|
|
974
|
-
}
|
|
988
|
+
filter = true;
|
|
975
989
|
}
|
|
990
|
+
if (filter)
|
|
991
|
+
{
|
|
992
|
+
requestPath = util.stripQueryStringFromUrl(requestPath);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
976
995
|
|
|
977
|
-
|
|
978
|
-
|
|
996
|
+
req.log(req.method + " " + requestPath + " [" + res.statusCode + "] (" + time.toFixed(2) + " ms)", warn);
|
|
997
|
+
}));
|
|
979
998
|
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
999
|
+
// set up CORS allowances
|
|
1000
|
+
// this lets CORS requests float through the proxy
|
|
1001
|
+
app.use(main.ensureCORS());
|
|
983
1002
|
|
|
984
|
-
|
|
985
|
-
|
|
1003
|
+
// set up default security headers
|
|
1004
|
+
app.use(main.ensureHeaders());
|
|
986
1005
|
|
|
987
|
-
|
|
988
|
-
|
|
1006
|
+
// common interceptors and config
|
|
1007
|
+
main.common2(app);
|
|
989
1008
|
|
|
990
|
-
|
|
991
|
-
|
|
1009
|
+
// APPLY CUSTOM DRIVER FUNCTIONS
|
|
1010
|
+
runFunctions(config.driverFunctions, [app], function(err) {
|
|
992
1011
|
|
|
993
|
-
|
|
994
|
-
|
|
1012
|
+
// binds gitana driver into place
|
|
1013
|
+
main.common3(app);
|
|
995
1014
|
|
|
996
|
-
|
|
997
|
-
|
|
1015
|
+
// parse cookies
|
|
1016
|
+
app.use(cookieParser());
|
|
998
1017
|
|
|
999
|
-
|
|
1000
|
-
|
|
1018
|
+
// cloudcms things need to run here
|
|
1019
|
+
main.common4(app, true);
|
|
1001
1020
|
|
|
1002
|
-
|
|
1003
|
-
|
|
1021
|
+
// APPLY CUSTOM FILTER FUNCTIONS
|
|
1022
|
+
runFunctions(config.filterFunctions, [app], function (err) {
|
|
1004
1023
|
|
|
1005
|
-
|
|
1006
|
-
|
|
1024
|
+
// PATH BASED PERFORMANCE CACHING
|
|
1025
|
+
main.perf1(app);
|
|
1007
1026
|
|
|
1008
|
-
|
|
1009
|
-
|
|
1027
|
+
// proxy - anything that goes to /proxy is handled here early and nothing processes afterwards
|
|
1028
|
+
main.proxy(app);
|
|
1010
1029
|
|
|
1011
|
-
|
|
1012
|
-
|
|
1030
|
+
// MIMETYPE BASED PERFORMANCE CACHING
|
|
1031
|
+
main.perf2(app);
|
|
1013
1032
|
|
|
1014
|
-
|
|
1015
|
-
|
|
1033
|
+
// DEVELOPMENT BASED PERFORMANCE CACHING
|
|
1034
|
+
main.perf3(app);
|
|
1016
1035
|
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1036
|
+
// standard body parsing + a special cloud cms body parser that makes a last ditch effort for anything
|
|
1037
|
+
// that might be JSON (regardless of content type)
|
|
1038
|
+
app.use(function (req, res, next) {
|
|
1020
1039
|
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
});
|
|
1040
|
+
multipart(process.configuration.bodyParsers.multipart || {})(req, res, function (err) {
|
|
1041
|
+
bodyParser.json(process.configuration.bodyParsers.json || {})(req, res, function (err) {
|
|
1042
|
+
bodyParser.urlencoded(process.configuration.bodyParsers.urlencoded || {})(req, res, function (err) {
|
|
1043
|
+
main.bodyParser()(req, res, function (err) {
|
|
1044
|
+
next(err);
|
|
1027
1045
|
});
|
|
1028
1046
|
});
|
|
1029
1047
|
});
|
|
1030
|
-
|
|
1031
1048
|
});
|
|
1032
1049
|
|
|
1033
|
-
|
|
1034
|
-
{
|
|
1035
|
-
app.use(initializedSession);
|
|
1036
|
-
app.use(flash());
|
|
1037
|
-
}
|
|
1050
|
+
});
|
|
1038
1051
|
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
app.use(
|
|
1052
|
+
if (initializedSession)
|
|
1053
|
+
{
|
|
1054
|
+
app.use(initializedSession);
|
|
1055
|
+
app.use(flash());
|
|
1056
|
+
}
|
|
1043
1057
|
|
|
1044
|
-
|
|
1045
|
-
|
|
1058
|
+
// this is the same as calling
|
|
1059
|
+
// app.use(passport.initialize());
|
|
1060
|
+
// except we create a new passport each time and store on request to support multitenancy
|
|
1061
|
+
app.use(function(req, res, next) {
|
|
1046
1062
|
|
|
1047
|
-
|
|
1048
|
-
|
|
1063
|
+
var passport = new Passport();
|
|
1064
|
+
passport._key = "passport-" + req.virtualHost;
|
|
1049
1065
|
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
// load data from existing session
|
|
1053
|
-
req._passport.session = req.session[passport._key];
|
|
1054
|
-
}
|
|
1066
|
+
req._passport = {};
|
|
1067
|
+
req._passport.instance = passport;
|
|
1055
1068
|
|
|
1056
|
-
|
|
1057
|
-
|
|
1069
|
+
if (req.session && req.session[passport._key])
|
|
1070
|
+
{
|
|
1071
|
+
// load data from existing session
|
|
1072
|
+
req._passport.session = req.session[passport._key];
|
|
1073
|
+
}
|
|
1058
1074
|
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
done(null, user);
|
|
1062
|
-
});
|
|
1063
|
-
req.passport.deserializeUser(function(user, done) {
|
|
1064
|
-
done(null, user);
|
|
1065
|
-
});
|
|
1075
|
+
// add this in
|
|
1076
|
+
req.passport = req._passport.instance;
|
|
1066
1077
|
|
|
1067
|
-
|
|
1078
|
+
// passport - serialize and deserialize
|
|
1079
|
+
req.passport.serializeUser(function(user, done) {
|
|
1080
|
+
done(null, user);
|
|
1081
|
+
});
|
|
1082
|
+
req.passport.deserializeUser(function(user, done) {
|
|
1083
|
+
done(null, user);
|
|
1068
1084
|
});
|
|
1069
1085
|
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1086
|
+
next();
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
// passport session
|
|
1090
|
+
if (initializedSession)
|
|
1091
|
+
{
|
|
1092
|
+
app.use(function(req, res, next) {
|
|
1093
|
+
req.passport.session()(req, res, next);
|
|
1094
|
+
});
|
|
1095
|
+
}
|
|
1077
1096
|
|
|
1078
|
-
|
|
1079
|
-
|
|
1097
|
+
// welcome files
|
|
1098
|
+
main.welcome(app);
|
|
1080
1099
|
|
|
1081
|
-
|
|
1082
|
-
|
|
1100
|
+
// configure cloudcms app server command handing
|
|
1101
|
+
main.interceptors(app, true);
|
|
1083
1102
|
|
|
1084
|
-
|
|
1103
|
+
//app.use(app.router);
|
|
1085
1104
|
|
|
1086
|
-
|
|
1087
|
-
|
|
1105
|
+
// healthcheck middleware
|
|
1106
|
+
main.healthcheck(app);
|
|
1088
1107
|
|
|
1089
|
-
|
|
1090
|
-
|
|
1108
|
+
// APPLY CUSTOM ROUTES
|
|
1109
|
+
runFunctions(config.routeFunctions, [app], function (err) {
|
|
1091
1110
|
|
|
1092
|
-
|
|
1093
|
-
|
|
1111
|
+
// configure cloudcms app server handlers
|
|
1112
|
+
main.handlers(app, true);
|
|
1094
1113
|
|
|
1095
|
-
|
|
1096
|
-
|
|
1114
|
+
// register error functions
|
|
1115
|
+
runFunctions(config.errorFunctions, [app], function (err) {
|
|
1097
1116
|
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1117
|
+
// APPLY CUSTOM CONFIGURE FUNCTIONS
|
|
1118
|
+
var allConfigureFunctions = [];
|
|
1119
|
+
for (var env in config.configureFunctions)
|
|
1120
|
+
{
|
|
1121
|
+
var functions = config.configureFunctions[env];
|
|
1122
|
+
if (functions)
|
|
1101
1123
|
{
|
|
1102
|
-
var
|
|
1103
|
-
if (functions)
|
|
1124
|
+
for (var i = 0; i < functions.length; i++)
|
|
1104
1125
|
{
|
|
1105
|
-
|
|
1106
|
-
{
|
|
1107
|
-
allConfigureFunctions.push(functions[i]);
|
|
1108
|
-
}
|
|
1126
|
+
allConfigureFunctions.push(functions[i]);
|
|
1109
1127
|
}
|
|
1110
1128
|
}
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
////////////////////////////////////////////////////////////////////////////
|
|
1114
|
-
//
|
|
1115
|
-
// INITIALIZE THE SERVER
|
|
1116
|
-
//
|
|
1117
|
-
////////////////////////////////////////////////////////////////////////////
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
// create the server (either HTTP or HTTPS)
|
|
1121
|
-
var httpServer = null;
|
|
1122
|
-
if (process.configuration.https) {
|
|
1123
|
-
// configure helmet to support auto-upgrade of http->https
|
|
1124
|
-
app.use(helmet());
|
|
1125
|
-
// create https server
|
|
1126
|
-
httpServer = https.createServer(process.configuration.https, app);
|
|
1127
|
-
} else {
|
|
1128
|
-
// legacy
|
|
1129
|
-
httpServer = http.Server(app);
|
|
1130
|
-
}
|
|
1129
|
+
}
|
|
1130
|
+
runFunctions(allConfigureFunctions, [app], function (err) {
|
|
1131
1131
|
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
{
|
|
1136
|
-
|
|
1132
|
+
// create the server (either HTTP or HTTPS)
|
|
1133
|
+
createHttpServer(app, function(err, httpServer) {
|
|
1134
|
+
|
|
1135
|
+
if (err) {
|
|
1136
|
+
return startServerFinishedFn(err);
|
|
1137
1137
|
}
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
// socket
|
|
1141
|
-
httpServer.on("connection", function (socket) {
|
|
1142
|
-
socket.setNoDelay(true);
|
|
1143
|
-
});
|
|
1144
|
-
const { Server } = require("socket.io");
|
|
1145
|
-
var io = process.IO = new Server(httpServer);
|
|
1146
|
-
//io.set('transports', config.socketTransports);
|
|
1147
|
-
io.use(function (socket, next) {
|
|
1148
|
-
|
|
1149
|
-
// console.log("New socket being initialized");
|
|
1150
|
-
|
|
1151
|
-
// attach _log function
|
|
1152
|
-
socket._log = function (text) {
|
|
1153
|
-
|
|
1154
|
-
var host = socket.handshake.headers.host;
|
|
1155
|
-
if (socket.handshake.headers["x-forwarded-host"])
|
|
1156
|
-
{
|
|
1157
|
-
host = socket.handshake.headers["x-forwarded-host"];
|
|
1158
|
-
}
|
|
1159
|
-
|
|
1160
|
-
var d = new Date();
|
|
1161
|
-
var dateString = d.toDateString();
|
|
1162
|
-
var timeString = d.toTimeString();
|
|
1163
|
-
|
|
1164
|
-
// gray color
|
|
1165
|
-
var grayColor = "\x1b[90m";
|
|
1166
|
-
|
|
1167
|
-
// final color
|
|
1168
|
-
var finalColor = "\x1b[0m";
|
|
1169
|
-
|
|
1170
|
-
if (process.env.CLOUDCMS_APPSERVER_MODE === "production")
|
|
1171
|
-
{
|
|
1172
|
-
grayColor = "";
|
|
1173
|
-
finalColor = "";
|
|
1174
|
-
}
|
|
1175
|
-
|
|
1176
|
-
var message = '';
|
|
1177
|
-
message += grayColor + '<socket> ';
|
|
1178
|
-
message += grayColor + '[' + dateString + ' ' + timeString + '] ';
|
|
1179
|
-
message += grayColor + host + ' ';
|
|
1180
|
-
message += grayColor + text + '';
|
|
1181
|
-
message += finalColor;
|
|
1182
|
-
|
|
1183
|
-
console.log(message);
|
|
1184
|
-
};
|
|
1185
|
-
/*
|
|
1186
|
-
socket.on("connect", function () {
|
|
1187
|
-
console.log("Socket connect()");
|
|
1188
|
-
});
|
|
1189
|
-
*/
|
|
1190
|
-
/*
|
|
1191
|
-
socket.on("disconnect", function () {
|
|
1192
|
-
var message = "Socket disconnected";
|
|
1193
|
-
if (socket && socket.host)
|
|
1194
|
-
{
|
|
1195
|
-
message += ", host=" + socket.host;
|
|
1196
|
-
}
|
|
1197
|
-
if (socket && socket.gitana && socket.gitana.application && socket.gitana.application())
|
|
1198
|
-
{
|
|
1199
|
-
message += ", application=" + socket.gitana.application().title;
|
|
1200
|
-
}
|
|
1201
|
-
console.log(message);
|
|
1202
|
-
});
|
|
1203
|
-
*/
|
|
1204
|
-
|
|
1205
|
-
// APPLY CUSTOM SOCKET.IO CONFIG
|
|
1206
|
-
runFunctions(config.socketFunctions, [socket], function (err) {
|
|
1207
|
-
|
|
1208
|
-
require("../middleware/awareness/awareness").initSocketIO(function() {
|
|
1209
|
-
next();
|
|
1210
|
-
});
|
|
1211
|
-
|
|
1212
|
-
// INSIGHT SERVER
|
|
1213
|
-
// if (config.insight && config.insight.enabled)
|
|
1214
|
-
// {
|
|
1215
|
-
// console.log("Init Insight to Socket");
|
|
1216
|
-
|
|
1217
|
-
// require("../insight/insight").init(socket, function () {
|
|
1218
|
-
// next();
|
|
1219
|
-
// });
|
|
1220
|
-
// }
|
|
1221
|
-
// else
|
|
1222
|
-
// {
|
|
1223
|
-
// next();
|
|
1224
|
-
// }
|
|
1225
|
-
});
|
|
1226
|
-
|
|
1227
|
-
});
|
|
1228
|
-
|
|
1229
|
-
// SET INITIAL VALUE FOR SERVER TIMESTAMP
|
|
1230
|
-
process.env.CLOUDCMS_APPSERVER_TIMESTAMP = new Date().getTime();
|
|
1231
|
-
|
|
1232
|
-
// DUST
|
|
1233
|
-
runFunctions(config.dustFunctions, [app, duster.getDust()], function (err) {
|
|
1234
|
-
|
|
1235
|
-
// APPLY SERVER BEFORE START FUNCTIONS
|
|
1236
|
-
runFunctions(config.beforeFunctions, [app], function (err) {
|
|
1237
|
-
|
|
1238
|
-
httpServer._listenPort = app.get("port");
|
|
1239
|
-
|
|
1240
|
-
// AFTER SERVER START
|
|
1241
|
-
runFunctions(config.afterFunctions, [app], function (err) {
|
|
1242
|
-
|
|
1243
|
-
function cleanup() {
|
|
1244
|
-
|
|
1245
|
-
if (cluster.isMaster)
|
|
1246
|
-
{
|
|
1247
|
-
console.log("");
|
|
1248
|
-
console.log("");
|
|
1249
|
-
|
|
1250
|
-
console.log("Cloud CMS Module shutting down");
|
|
1251
|
-
|
|
1252
|
-
// close server connections as cleanly as we can
|
|
1253
|
-
console.log(" -> Closing server connections");
|
|
1254
|
-
}
|
|
1255
|
-
|
|
1256
|
-
try
|
|
1257
|
-
{
|
|
1258
|
-
httpServer.close();
|
|
1259
|
-
}
|
|
1260
|
-
catch (e)
|
|
1261
|
-
{
|
|
1262
|
-
console.log("Server.close produced error: " + JSON.stringify(e));
|
|
1263
|
-
}
|
|
1264
|
-
|
|
1265
|
-
// ask toobusy to shut down as cleanly as we can
|
|
1266
|
-
if (cluster.isMaster)
|
|
1267
|
-
{
|
|
1268
|
-
console.log(" -> Closing toobusy monitor");
|
|
1269
|
-
}
|
|
1270
|
-
|
|
1271
|
-
try
|
|
1272
|
-
{
|
|
1273
|
-
toobusy.shutdown();
|
|
1274
|
-
}
|
|
1275
|
-
catch (e)
|
|
1276
|
-
{
|
|
1277
|
-
console.log("toobusy.shutdown produced error: " + JSON.stringify(e));
|
|
1278
|
-
}
|
|
1279
|
-
|
|
1280
|
-
if (cluster.isMaster)
|
|
1281
|
-
{
|
|
1282
|
-
console.log("");
|
|
1283
|
-
}
|
|
1284
|
-
|
|
1285
|
-
// tell the process to exit
|
|
1286
|
-
process.exit();
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
// listen for kill or interrupt so that we can shut down cleanly
|
|
1290
|
-
process.on('SIGINT', cleanup);
|
|
1291
|
-
process.on('SIGTERM', cleanup);
|
|
1292
|
-
|
|
1293
|
-
// if we are on a worker process, then inform the master that we completed
|
|
1294
|
-
if (process.send)
|
|
1295
|
-
{
|
|
1296
|
-
process.send("server-startup");
|
|
1297
|
-
}
|
|
1298
|
-
|
|
1299
|
-
afterStartFn(app, httpServer);
|
|
1300
|
-
|
|
1301
|
-
});
|
|
1302
|
-
});
|
|
1303
|
-
});
|
|
1138
|
+
|
|
1139
|
+
startServerFinishedFn(null, app, httpServer);
|
|
1304
1140
|
});
|
|
1305
1141
|
});
|
|
1306
1142
|
});
|
|
@@ -1312,6 +1148,202 @@ var startSlave = function(config, afterStartFn)
|
|
|
1312
1148
|
});
|
|
1313
1149
|
};
|
|
1314
1150
|
|
|
1151
|
+
var createHttpServer = function(app, done)
|
|
1152
|
+
{
|
|
1153
|
+
// create the server (either HTTP or HTTPS)
|
|
1154
|
+
var httpServer = null;
|
|
1155
|
+
|
|
1156
|
+
if (process.configuration.https)
|
|
1157
|
+
{
|
|
1158
|
+
if (app)
|
|
1159
|
+
{
|
|
1160
|
+
// configure helmet to support auto-upgrade of http->https
|
|
1161
|
+
app.use(helmet());
|
|
1162
|
+
}
|
|
1163
|
+
|
|
1164
|
+
// create https server
|
|
1165
|
+
httpServer = https.createServer(process.configuration.https, app);
|
|
1166
|
+
}
|
|
1167
|
+
else
|
|
1168
|
+
{
|
|
1169
|
+
// legacy
|
|
1170
|
+
httpServer = http.Server(app);
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
// request timeout
|
|
1174
|
+
var requestTimeout = 30000; // 30 seconds
|
|
1175
|
+
if (process.configuration && process.configuration.timeout)
|
|
1176
|
+
{
|
|
1177
|
+
requestTimeout = process.configuration.timeout;
|
|
1178
|
+
}
|
|
1179
|
+
httpServer.setTimeout(requestTimeout);
|
|
1180
|
+
|
|
1181
|
+
// socket
|
|
1182
|
+
httpServer.on("connection", function (socket) {
|
|
1183
|
+
socket.setNoDelay(true);
|
|
1184
|
+
});
|
|
1185
|
+
|
|
1186
|
+
done(null, httpServer);
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
var configureServer = function(config, app, httpServer, configureServerFinishedFn)
|
|
1190
|
+
{
|
|
1191
|
+
var io = httpServer.io;
|
|
1192
|
+
if (io)
|
|
1193
|
+
{
|
|
1194
|
+
//io.set('transports', config.socketTransports);
|
|
1195
|
+
io.use(function (socket, next) {
|
|
1196
|
+
|
|
1197
|
+
// console.log("New socket being initialized");
|
|
1198
|
+
|
|
1199
|
+
// attach _log function
|
|
1200
|
+
socket._log = function (text) {
|
|
1201
|
+
|
|
1202
|
+
var host = socket.handshake.headers.host;
|
|
1203
|
+
if (socket.handshake.headers["x-forwarded-host"])
|
|
1204
|
+
{
|
|
1205
|
+
host = socket.handshake.headers["x-forwarded-host"];
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
var d = new Date();
|
|
1209
|
+
var dateString = d.toDateString();
|
|
1210
|
+
var timeString = d.toTimeString();
|
|
1211
|
+
|
|
1212
|
+
// gray color
|
|
1213
|
+
var grayColor = "\x1b[90m";
|
|
1214
|
+
|
|
1215
|
+
// final color
|
|
1216
|
+
var finalColor = "\x1b[0m";
|
|
1217
|
+
|
|
1218
|
+
if (process.env.CLOUDCMS_APPSERVER_MODE === "production")
|
|
1219
|
+
{
|
|
1220
|
+
grayColor = "";
|
|
1221
|
+
finalColor = "";
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
var message = '';
|
|
1225
|
+
message += grayColor + '<socket> ';
|
|
1226
|
+
message += grayColor + '[' + dateString + ' ' + timeString + '] ';
|
|
1227
|
+
message += grayColor + host + ' ';
|
|
1228
|
+
message += grayColor + text + '';
|
|
1229
|
+
message += finalColor;
|
|
1230
|
+
|
|
1231
|
+
console.log(message);
|
|
1232
|
+
};
|
|
1233
|
+
/*
|
|
1234
|
+
socket.on("connect", function () {
|
|
1235
|
+
console.log("Socket connect()");
|
|
1236
|
+
});
|
|
1237
|
+
*/
|
|
1238
|
+
/*
|
|
1239
|
+
socket.on("disconnect", function () {
|
|
1240
|
+
var message = "Socket disconnected";
|
|
1241
|
+
if (socket && socket.host)
|
|
1242
|
+
{
|
|
1243
|
+
message += ", host=" + socket.host;
|
|
1244
|
+
}
|
|
1245
|
+
if (socket && socket.gitana && socket.gitana.application && socket.gitana.application())
|
|
1246
|
+
{
|
|
1247
|
+
message += ", application=" + socket.gitana.application().title;
|
|
1248
|
+
}
|
|
1249
|
+
console.log(message);
|
|
1250
|
+
});
|
|
1251
|
+
*/
|
|
1252
|
+
|
|
1253
|
+
// APPLY CUSTOM SOCKET.IO CONFIG
|
|
1254
|
+
runFunctions(config.socketFunctions, [socket], function (err) {
|
|
1255
|
+
|
|
1256
|
+
require("../middleware/awareness/awareness").initSocketIO(function() {
|
|
1257
|
+
next();
|
|
1258
|
+
});
|
|
1259
|
+
|
|
1260
|
+
// INSIGHT SERVER
|
|
1261
|
+
// if (config.insight && config.insight.enabled)
|
|
1262
|
+
// {
|
|
1263
|
+
// console.log("Init Insight to Socket");
|
|
1264
|
+
|
|
1265
|
+
// require("../insight/insight").init(socket, function () {
|
|
1266
|
+
// next();
|
|
1267
|
+
// });
|
|
1268
|
+
// }
|
|
1269
|
+
// else
|
|
1270
|
+
// {
|
|
1271
|
+
// next();
|
|
1272
|
+
// }
|
|
1273
|
+
});
|
|
1274
|
+
|
|
1275
|
+
});
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
// SET INITIAL VALUE FOR SERVER TIMESTAMP
|
|
1279
|
+
process.env.CLOUDCMS_APPSERVER_TIMESTAMP = new Date().getTime();
|
|
1280
|
+
|
|
1281
|
+
// DUST
|
|
1282
|
+
runFunctions(config.dustFunctions, [app, duster.getDust()], function (err) {
|
|
1283
|
+
|
|
1284
|
+
// APPLY SERVER BEFORE START FUNCTIONS
|
|
1285
|
+
runFunctions(config.beforeFunctions, [app], function (err) {
|
|
1286
|
+
|
|
1287
|
+
// AFTER SERVER START
|
|
1288
|
+
runFunctions(config.afterFunctions, [app], function (err) {
|
|
1289
|
+
|
|
1290
|
+
function cleanup() {
|
|
1291
|
+
|
|
1292
|
+
if (cluster.isMaster)
|
|
1293
|
+
{
|
|
1294
|
+
console.log("");
|
|
1295
|
+
console.log("");
|
|
1296
|
+
|
|
1297
|
+
console.log("Cloud CMS Module shutting down");
|
|
1298
|
+
|
|
1299
|
+
// close server connections as cleanly as we can
|
|
1300
|
+
console.log(" -> Closing server connections");
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
try
|
|
1304
|
+
{
|
|
1305
|
+
httpServer.close();
|
|
1306
|
+
}
|
|
1307
|
+
catch (e)
|
|
1308
|
+
{
|
|
1309
|
+
console.log("Server.close produced error: " + JSON.stringify(e));
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
// ask toobusy to shut down as cleanly as we can
|
|
1313
|
+
if (cluster.isMaster)
|
|
1314
|
+
{
|
|
1315
|
+
console.log(" -> Closing toobusy monitor");
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
try
|
|
1319
|
+
{
|
|
1320
|
+
toobusy.shutdown();
|
|
1321
|
+
}
|
|
1322
|
+
catch (e)
|
|
1323
|
+
{
|
|
1324
|
+
console.log("toobusy.shutdown produced error: " + JSON.stringify(e));
|
|
1325
|
+
}
|
|
1326
|
+
|
|
1327
|
+
if (cluster.isMaster)
|
|
1328
|
+
{
|
|
1329
|
+
console.log("");
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
// tell the process to exit
|
|
1333
|
+
process.exit();
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
// listen for kill or interrupt so that we can shut down cleanly
|
|
1337
|
+
process.on('SIGINT', cleanup);
|
|
1338
|
+
process.on('SIGTERM', cleanup);
|
|
1339
|
+
|
|
1340
|
+
configureServerFinishedFn();
|
|
1341
|
+
});
|
|
1342
|
+
});
|
|
1343
|
+
});
|
|
1344
|
+
};
|
|
1345
|
+
|
|
1346
|
+
|
|
1315
1347
|
|
|
1316
1348
|
////////////////////////////////////////////////////////////////////////////
|
|
1317
1349
|
//
|