forked from FabMo/FabMo-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
928 lines (863 loc) · 39.7 KB
/
engine.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/*
* engine.js
*
* Defines the Engine object, which is the top level application singleton that represents this running engine instance.
*
* The engine object is primarily defined by its start() method, which defines most of the startup configuration
* activities for the engine.
*/
var restify = require("restify");
var util = require("./util");
var events = require("events");
//var socketio = require('socket.io');
var async = require("async");
var process = require("process");
var machine = require("./machine");
var config = require("./config");
var OS = process.platform;
var PLATFORM = process.env.PLATFORM;
var log = require("./log").logger("engine");
var db = require("./db");
var macros = require("./macros");
var dashboard = require("./dashboard");
var network = require("./network");
var glob = require("glob");
var argv = require("minimist")(process.argv);
var fs = require("fs");
var sessions = require("client-sessions");
var authentication = require("./authentication");
var profiles = require("./profiles");
var crypto = require("crypto");
var moment = require("moment");
//other util
var Util = require("util");
var PACKAGE_CHECK_DELAY = 30; // Seconds
// The engine object has a few high level properties and some key methods that define the application lifecycle
// Most of the important stuff is in the start() method.
var Engine = function () {
this.version = null;
this.time_synced = false;
this.firmware = {
build: null,
config: null,
version: null,
};
// is istavailable to the client via HTTP endpoint and websocket
this.status = {
online: false,
};
// The default network manager is the generic one (until the network module has been asked to load one specific to this platform)
this.networkManager = network.Generic;
//TO-DO file should not be emiting (bring these into seperate modules)
events.EventEmitter.call(this);
};
Util.inherits(Engine, events.EventEmitter);
/*
* Configure the engine for the first time.
* This function is typically called when an engine configuration does not exist.
* It sets some defaults in the engine config (by way of the config module) based on system platform.
* Mainly this amounts to configuring appropriate serial port paths and http hosting ports
* for different types of machines.
*/
function EngineConfigFirstTime(callback) {
if (PLATFORM) {
log.info("Setting platform to " + PLATFORM);
config.engine.set("platform", PLATFORM);
}
switch (OS) {
case "linux":
var ports = {
control_port_linux: "/dev/ttyACM0",
data_port_linux: "/dev/ttyACM0",
};
config.engine.update(ports, function () {
callback();
});
break;
case "darwin":
config.engine.set("server_port", 9876);
glob.glob("/dev/cu.usbmodem*", function (err, files) {
if (files.length >= 1) {
var ports = {
control_port_osx: files[0],
data_port_osx: files[1] || files[0],
};
config.engine.update(ports, function () {
callback();
});
} else {
callback();
}
});
break;
default:
callback();
break;
}
}
/*
* Set the current system time to the provided value.
* obj - an object with a 'utc' property that coresponds to the current UTC time
*/
Engine.prototype.setTime = function (time) {
if (this.time_synced) {
log.warn(
"Not accepting an externally provided time. Local time is trusted."
);
return;
} else {
var m = moment.unix(time / 1000.0);
var t = m.utc().format("YYYY-MM-DD HH:mm:ss");
var cmd = "timedatectl set-time " + t + "; timedatectl";
util.doshell(cmd, function (stdout) {
log.debug("Time Set To: " + stdout);
this.time_synced = true;
});
}
};
/*
* Stop this engine instance.
* Basically does cleanup activities before shutting the engine down.
*/
Engine.prototype.stop = function (reason, callback) {
this.machine.disconnect(reason);
this.machine.setState(this.machine, "stopped");
callback(null);
};
/*
* Get the version of this engine.
* Honor semver numbering:
* For working dev version tag in branch from most recent; use ODD numbers just for redundancy
* For production builds, tag with the next EVEN number in MASTER.
* The Release Version is codified by a version.json that defines a version object that includes:
* hash - A hash for the build, typically a commit SHA (may not exist in a release type build)
* number - The semver version number
* type - The type of release, either 'dev', 'rc', or 'release'
*
* If that file exists and can be parsed, return a version object that reflects its contents.
* If it does not exist, use git (if available on the system) to read out working version information instead
* (See: example-version.json)
*/
Engine.prototype.getVersion = function (callback) {
// grab the version and abbreviated SHA (if one)
util.doshell(
"git describe",
function (data) {
this.version = {};
this.version.number = (data || "").trim();
// Our version tags in git are prefixed with "v",
// The rrequirement is to display without the "v"
// We have old versions without the "v" prefix
if (this.version.number.substring(0, 1) === "v") {
this.version.number = this.version.number.substring(1);
}
this.version.debug = "debug" in argv;
// then see if we have an official release version# in json file
// see: version-example.json
fs.readFile(
"version.json",
"utf8",
function (err, data) {
if (err) {
log.debug(
" ... no version file ... using git and dev info"
);
this.version.type = "dev";
this.version.number = this.version.number + "-dev";
var random =
Math.floor(Math.random() * (99999 - 10000)) + 10000;
log.info("Adding random prefix to dev engine version");
this.version.number =
random.toString() + "-" + this.version.number;
return callback(null, this.version);
}
try {
log.debug(
" ... found json version file ... this is a RELEASE VERSION"
);
data = JSON.parse(data);
if (data.number) {
this.version.number = data.number;
this.version.type = "release";
}
} catch (e) {
log.debug(" ... can't parse version file!");
this.version.number =
this.version.number + "-dev-fault";
this.version.type = "dev";
// eslint-disable-next-line no-redeclare
var random =
Math.floor(Math.random() * (99999 - 10000)) + 10000;
log.info("Adding random prefix to dev engine version");
this.version.number =
random.toString() + "-" + this.version.number;
} finally {
callback(null, this.version);
}
}.bind(this)
);
}.bind(this)
);
};
/*
* Return "info" about this engine, which currently is just version data for the engine and the firmware
*/
Engine.prototype.getInfo = function (callback) {
callback(null, {
firmware: this.firmware,
version: this.version,
});
};
// Set the online flag (indicates whether the engine is online) to the provided value
// online - true to indicate that the engine can see the network. False otherwise.
Engine.prototype.setOnline = function (online) {
this.status.online = online;
this.emit("status", this.status);
};
// ##############################################################################
/*
* This function is the primary content of the Engine object. It is called on application launch, and is
* sort of the "main thread" of execution. It executes a bunch of startup/configuration functions, and
* then sets up the http server to listen on the appropriate port. Setup happens first so the user doesn't
* connect to the engine before it is ready to accept connections.
*/
Engine.prototype.start = function (callback) {
async.series(
[
// AFTER LOADING SOME Config data and info
// We will:
// Configure the engine data directories
// Create the folder structure (typically) in /opt/fabmo
// ... eg: /opt/fabmo/config, /opt/fabmo/macros, etc...
// TODO - This seems like a duplication of the same step below
////## BUT testing shows that for some start scenarios,
// ... both versions seem necessary 1/2/21
function setup_application(callback) {
log.info("#START# --> Checking engine data directory tree...");
config.createDataDirectories(callback);
},
// Load the engine configuration from disk.
function load_engine_config(callback) {
log.info("Loading engine configuration...");
config.configureEngine(callback);
},
// Determine if we're configuring the engine for the first time ever.
// If so, populate the engine configuration with defaults that are sensible for this platform.
function check_engine_config(callback) {
if (!config.engine.get("init")) {
log.info("Configuring the engine for the first time...");
EngineConfigFirstTime(function () {
config.engine.set("init", true);
callback();
});
} else {
callback();
}
},
// Load profiles. See the profiles module for what this entails.
function load_profiles(callback) {
// eslint-disable-next-line no-unused-vars
profiles.load(function (err, profiles) {
if (err) {
log.error(err);
}
callback();
});
},
// Load users. See config/user_config.js for what this entails.
function load_users(callback) {
log.info("Loading users....");
config.configureUser(function () {
callback();
});
},
// Read the selected profile from the engine config.
// If the engine config doesn't specify a profile, look in /fabmo/site/.default
// If /fabmo/site/.default exists and has content, interpret its content as the selected profile.
// If neither of these strategies yields a profile, just choose the 'Default' profile.
function profile_shim(callback) {
var profile = config.engine.get("profile");
var def = "";
if (profile) {
return callback();
} else {
fs.readFile(
"../site/.default",
"utf8",
function (err, content) {
if (err) {
def = "Default";
} else {
def = content;
}
config.engine.set("profile", def, callback);
}
);
}
}.bind(this),
// Read the engine version and hang onto it
function get_fabmo_version(callback) {
log.info("Getting engine version...");
this.getVersion(
function (err, data) {
if (err) {
log.error(err);
this.version = "";
return callback();
}
log.info(
"Got engine version: " +
JSON.stringify(this.version)
);
this.version = data;
callback();
}.bind(this)
);
}.bind(this),
// The approot should be cleared if:
// - We're running in 'debug' mode (and might be making changes to system apps)
// - The version of the engine has changed since the last time we run it.
// So, we check those things, and clear the approot accordingly.
// See dashboard/app_manager.js for more about the approot.
function clear_approot(callback) {
var flg_clr_approot = false;
var last_time_version = (
config.engine.get("version") || ""
).trim();
var this_time_version = (
this.version.hash ||
this.version.number ||
""
).trim();
log.debug("Previous engine version: " + last_time_version);
log.debug(" Current engine version: " + this_time_version);
if (last_time_version != this_time_version) {
log.info(
"Engine version has changed - flag to clear the approot."
);
flg_clr_approot = true;
} else {
log.info("Engine version is unchanged since last run.");
//callback();
}
if ("debug" in argv) {
log.info(
"Running in debug mode - flag to clear the approot."
);
flg_clr_approot = true;
}
if (flg_clr_approot) {
config.clearAppRoot(function (err, stdout) {
log.info("Clearing the approot ...");
config.engine.set("version", this_time_version);
if (err) {
log.error(err);
} else {
log.debug(" @flg_cr_approot- " + stdout);
}
callback();
});
} else {
callback();
}
}.bind(this),
function create_data_directories(callback) {
log.debug("Create_data_directories ...");
config.createDataDirectories(callback);
}.bind(this),
// "Apply" the engine configuration, that is, take the configuration values loaded and actually
// set up the application based on them. See config/engine_config.js to see what this entails.
function apply_engine_config(callback) {
log.info("Applying engine configuration...");
config.engine.apply(callback);
},
// Configure the DB see db.js to see what this entails.
function setup_database(callback) {
log.info("Configuring database...");
db.configureDB(callback);
},
// Cleanup the DB. This is to clear jobs that may be "dangling" after a crash, or other inconsistencies.
// See db.js for more details.
function clean_database(callback) {
log.info("Cleaning up database...");
db.cleanup(callback);
},
// Connect to G2 and initialize machine runtimes. See machine.js for what this entails.
function connect(callback) {
log.info("Connecting to G2...");
// eslint-disable-next-line no-unused-vars
machine.connect(function (err, machine) {
if (err) {
log.error("!!!!!!!!!!!!!!!!!!!!!!!!");
log.error("Could not connect to G2.");
log.error("(" + err + ")");
log.error("!!!!!!!!!!!!!!!!!!!!!!!!");
}
callback(null);
});
}.bind(this),
// Apply the "machine" configuration - see config/machine_config.js for details
function load_machine_config(callback) {
this.machine = machine.machine;
log.info("Loading the machine configuration...");
// eslint-disable-next-line no-unused-vars
config.configureMachine(this.machine, function (err, result) {
if (err) {
log.warn(err);
}
callback(null);
});
}.bind(this),
// The default unit system is specified in the machine configuration. It requires
// special care to apply - see g2.js for details.
function set_units(callback) {
if (this.machine.isConnected()) {
this.machine.driver.setUnits(
config.machine.get("units"),
callback
);
} else {
callback(null);
}
}.bind(this),
// Configure G2 by loading all its json settings and static configuration parameters
function load_driver_config(callback) {
if (this.machine.isConnected()) {
log.info("Configuring G2...");
config.configureDriver(
machine.machine.driver,
// eslint-disable-next-line no-unused-vars
function (err, data) {
if (err) {
log.error(
"There were problems loading the G2 configuration."
);
}
callback(null);
}
);
} else {
log.warn("Skipping G2 configuration due to no connection.");
// eslint-disable-next-line no-unused-vars
config.configureDriver(null, function (err, data) {
callback(null);
});
callback(null);
}
}.bind(this),
// Retrieve the firmware version from G2. This is done only once, and the value
// is cached as a property of the Engine object.
function get_g2_version(callback) {
if (this.machine.isConnected()) {
log.info("Getting G2 firmware version...");
this.machine.driver.get(
["fb", "fbs", "fbc"],
function (err, value) {
if (err) {
log.error(
"Could not get the G2 firmware build. (" +
err +
")"
);
} else {
log.info("G2 Firmware Information: " + value);
this.firmware.build = value[0];
this.firmware.version = value[1];
this.firmware.config = value[2];
// Our version tags in git are prefixed with "v",
// The rrequirement is to display without the "v"
// We have old versions without the "v" prefix
if (
this.firmware.version.substring(0, 1) ===
"v"
) {
this.firmware.version =
this.firmware.version.substring(1);
}
}
callback(null);
}.bind(this)
);
} else {
log.warn(
"Skipping G2 firmware version check due to no connection."
);
callback(null);
}
}.bind(this),
// This is a shim that clears some obsolete entries from the configuration that have been
// known to cause errors with modern versions of G2.
// TODO - this is the sort of thing that might be better done by the updater as part of the
// installation process. It is typical for such processes to "migrate" config files to latest version.
function g2_shim(callback) {
log.debug("Running G2 Shim...");
var entries = [
"1sa",
"1tr",
"1mi",
"2sa",
"2tr",
"2mi",
"3sa",
"3tr",
"3mi",
"4sa",
"4tr",
"4mi",
"5sa",
"5tr",
"5mi",
"6sa",
"6tr",
"6mi",
"ja",
];
var do_shim = false;
for (var i = 0; i < entries.length; i++) {
if (config.driver.has(entries[i])) {
do_shim = true;
}
}
if (do_shim) {
log.debug("Deleting obsolete entries in G2 config");
// eslint-disable-next-line no-unused-vars
config.driver.deleteMany(entries, function (err, data) {
config.driver.restore(function () {
callback();
});
});
} else {
log.debug("No obsolete entries in G2 config.");
callback();
}
}.bind(this),
// Load commands which are populated dynamically from the contents of a folder
// in the openSBP runtime. See runtime/opensbp/opensbp.js for what this entails.
function load_opensbp_commands(callback) {
if (!this.machine.isConnected()) {
log.warn(
"Not loading SBP Commands due to no connection to motion system."
);
return callback(null);
}
log.info("Loading OpenSBP Commands...");
this.machine.sbp_runtime.loadCommands(callback);
}.bind(this),
// Apply the OpenSBP runtime config. See config/opensbp_config.js for what this entails.
function load_opensbp_config(callback) {
if (!this.machine.isConnected()) {
log.warn(
"Not configuring SBP due to no connection to motion system."
);
return callback(null);
}
log.info("Configuring OpenSBP runtime...");
config.configureOpenSBP(callback);
}.bind(this),
// Apply the machine config. See config/machine_config.js for what this entails
function apply_machine_config(callback) {
log.info("Applying machine configuration...");
config.machine.apply(callback);
}.bind(this),
// Setup the dashboard. See dashboard/index.js and dashboard/app_manager.js for what this entails.
function configure_dashboard(callback) {
log.info("Configuring dashboard...");
dashboard.configure(callback);
},
// Load the apps, see dashboard/app_manager.js for what this entails.
// TODO: Would this be better to just be included in the dashboard configuration function?
function load_apps(callback) {
log.info("Loading dashboard apps...");
dashboard.loadApps(function (err, result) {
callback(null, result);
});
},
// Load macros from disk. See macros.js
function load_macros(callback) {
log.info("Loading macros...");
macros.load(callback);
},
// Load and apply the 'instance' configuration. This is the dynamic machine state, like the current position,
// which is saved periodically in order to preserve state if the tool is power cycled.
// See config/instance_config.js for more details.
function load_instance_config(callback) {
if (!this.machine.isConnected()) {
log.warn(
"Not configuring instance due to no connection to motion system."
);
return callback(null);
}
log.info("Loading instance info...");
config.configureInstance(this.machine.driver, callback);
}.bind(this),
function apply_instance_config(callback) {
if (!this.machine.isConnected()) {
log.warn(
"Not applying instance config due to no connection to motion system."
);
return callback(null);
}
log.info("Applying instance configuration...");
config.instance.apply(callback);
}.bind(this),
// The secret key is used for authentication. It is persistent, stored with other config files.
function generate_auth_key(callback) {
log.info("Configuring secret key...");
var secret_file = config.getDataDir() + "/config/auth_secret";
fs.readFile(
secret_file,
"utf8",
function (err, data) {
// If there's already a secret key from disk, use it
if (!err && data && data.length == 512) {
log.info("Secret key already exists, using that.");
this.auth_secret = data;
return callback();
}
// If not, generate, save and use a new one
log.info("Generating a new secret key.");
this.auth_secret = crypto
.randomBytes(256)
.toString("hex");
fs.writeFile(
secret_file,
this.auth_secret,
// eslint-disable-next-line no-unused-vars
function (err, data) {
callback();
}.bind(this)
);
}.bind(this)
);
}.bind(this),
// Initialize the network module
function setup_network(callback) {
var name = config.engine.get("name");
log.info("name is - " + name);
network.createNetworkManager(
name,
// eslint-disable-next-line no-unused-vars
function (err, nm) {
this.networkManager = nm;
this.networkManager.on(
"network",
function (evt) {
if (
evt.mode === "station" ||
evt.mode === "ethernet"
) {
// 30 Second delay is used here to make sure timesyncd has enough time to update network time
// before trying to pull an update (https requests will fail with an inaccurate system time)
log.info(
"Network is possibly available: Going to check for packages in " +
PACKAGE_CHECK_DELAY +
" seconds."
);
setTimeout(
function () {
//TODO re-imlpement for dashboard only updates?
//log.info('Doing beacon report due to network change');
//this.beacon.setLocalAddresses(this.networkManager.getLocalAddresses());
//this.beacon.once('network');
// log.info('Running package check due to network change');
// this.runAllPackageChecks();
}.bind(this),
PACKAGE_CHECK_DELAY * 1000
);
}
}.bind(this)
);
// Call the network manager init function, which actually starts looking for networks, etc.
log.info("Setting up the network...");
try {
this.networkManager.init();
log.info("Network manager started.");
} catch (e) {
log.error(e);
log.error("Problem starting network manager:" + e);
}
return callback(null);
}.bind(this)
);
}.bind(this),
function setup_config_events(callback) {
config.engine.on(
"change",
function (evt) {
if (evt.beacon_url) {
this.beacon.set(
"url",
config.updater.get("beacon_url")
);
}
//TODO ... as part of dealing with beacon
////## this 'once' generating can't read prop error on first config changes
// If the tool name changes, report the change to beacon
if (evt.name) {
this.beacon.once("config");
}
// If beacon consent changes, let the beacon daemon know (possibly do a report)
if (evt.consent_for_beacon) {
this.beacon.set(
"consent_for_beacon",
evt.consent_for_beacon
);
log.info(
"Consent for beacon is " +
evt.consent_for_beacon
);
}
}.bind(this)
);
callback();
}.bind(this),
// Kick off the server if all of the above went OK.
function start_server(callback) {
log.info("Setting up the webserver...");
// Initialize a server and attach it to the application
var server = restify.createServer({ name: "FabMo Engine" });
this.server = server;
// Allow JSON over Cross-origin resource sharing
log.info("Configuring cross-origin requests...");
server.use(function crossOrigin(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"X-Requested-With"
);
return next();
});
// Introduce deliberate latency for testing. You can activate this by
// passing the --slow switch when running server.js (must include --debug too)
if ("debug" in argv && argv.debug === "slow") {
log.warn("Configuring deliberate latency for testing...");
server.use(function latency(req, res, next) {
setTimeout(next, 500 * Math.random());
});
}
// If in 'debug' mode, do some extra logging of HTTP requests
if ("debug" in argv) {
server.use(function debug(req, res, next) {
// temporarily removed because making hard to read manual mode action
// log.debug(req.method + ' ' + req.url);
next();
});
}
// The query parser does just that - map query params to an object for convenience
server.use(
restify.plugins.queryParser({
mapParams: true,
})
);
/*
* The engine URL scheme includes a software version number that performs a
* cache-busting function: When the engine is updated, the version number in the
* URL is revised, and the cache is thus busted.
* 404 errors are handled in two different ways depending on their content:
*/
server.on("NotFound", function (req, res) {
var current_hash = config.engine.get("version");
var url_arr = req.url.split("/");
// If a URL contains a hash that doesn't match the current version hash:
// Replace the URL's hash with the current one, and redirect.
if (url_arr[1] !== current_hash) {
url_arr.splice(1, 1, current_hash);
var newPath = url_arr.join("/");
res.redirect(newPath, function () {
return;
});
} else {
// If the URL contains a current hash, and simply points to a bogus resource,
// just redirect to the root.
res.redirect("/", function () {
return;
});
}
});
// Catch-all handler that responds gracefully when an internal server error occurs.
server.on("uncaughtException", function (res, err) {
log.uncaught(err);
var answer = {
status: "error",
message: err.message,
};
res.json(answer);
});
// Configure local directory for uploading files
log.info("Cofiguring upload directory...");
server.use(
restify.plugins.bodyParser({
uploadDir: config.engine.get("upload_dir") || "/tmp",
mapParams: true,
})
);
// TODO: What is this path sanitizer doing?
server.pre(restify.pre.sanitizePath());
// Configure authentication
log.info("Configuring authentication...");
log.info(
"Secret Key: " +
this.auth_secret.slice(0, 5) +
"..." +
this.auth_secret.slice(-5)
);
server.cookieSecret = this.auth_secret;
server.use(
sessions({
// cookie name dictates the key name added to the request object
cookieName: "session",
// should be a large unguessable string
secret: server.cookieSecret, // REQUIRE HTTPS SUPPORT !!!
// how long the session will stay valid in ms
duration: 31 * 24 * 60 * 60 * 1000,
cookie: {
maxAge: 31 * 24 * 60 * 60 * 1000, // duration of the cookie in milliseconds, defaults to duration above
ephemeral: false,
httpOnly: false, // when true, cookie is not accessible from javascript
secure: false, // when true, cookie will only be sent over SSL. use key 'secureProxy' instead if you handle SSL not in your node process
},
})
);
server.use(authentication.passport.initialize());
server.use(authentication.passport.session());
// gzipping is a typical way to speed up network operations
// We enable it on the server, so that supporting clients can use it
log.info("Enabling gzip for transport...");
server.use(restify.plugins.gzipResponse());
// Import the routes module and apply the routes to the server
// Routes are loaded dynamically. See routes/routes.js for details.
log.info("Loading routes...");
server.io = require("socket.io")(server.server);
// eslint says routes is unused, but removing it breaks fabmo
// eslint-disable-next-line no-unused-vars
var routes = require("./routes")(server);
// Kick off the server listening for connections
// 0.0.0.0 causes us to listen on ALL interfaces (so the engine can be seen over ethernet, wifi, etc.)
server.listen(
config.engine.get("server_port"),
"0.0.0.0",
function () {
log.info(server.name + " listening at " + server.url);
callback(null, server);
}
);
// TODO - should this be done after server.listen, or before? (or does it matter?)
authentication.configure();
}.bind(this),
],
// Print some kind of sane debugging information if anything above fails
// eslint-disable-next-line no-unused-vars
function (err, results) {
if (err) {
log.stack();
log.error(err);
typeof callback === "function" && callback(err);
} else {
typeof callback === "function" && callback(null, this);
}
}.bind(this)
);
};
// This module IS the engine object, but instantiating the object does virtually nothing:
// It is the call to start() that kicks off all the real work of running the application.
module.exports = new Engine();