forked from stimulant/ampm
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.cjs
More file actions
142 lines (118 loc) · 4.65 KB
/
Copy pathserver.cjs
File metadata and controls
142 lines (118 loc) · 4.65 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
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
#! /usr/bin/env node
var path = require("path"); //http://nodejs.org/api/path.html
var fs = require("fs-extra"); // Enhanced file system with recursive directory creation. https://github.com/jprichardson/node-fs-extra
var os = require("os"); // http://nodejs.org/api/os.html
var _ = require("lodash"); // Utilities. http://underscorejs.org/
var child_process = require("child_process"); // http://nodejs.org/api/child_process.html
var json = require("comment-json"); // https://www.npmjs.com/package/comment-json
// Exit cleanly if another ampm instance is already running on these ports.
process.on('uncaughtException', function(err) {
if (err.code === 'EADDRINUSE' || err.code === 'EACCES') {
console.error('ampm: port conflict (' + err.code + ' on ' + err.address + ':' + err.port + ') — another instance is likely running. Exiting.');
process.exit(0);
}
throw err;
});
var ConsoleState = require("./model/consoleState.cjs").ConsoleState;
var BaseModel = require("./model/baseModel.cjs").BaseModel;
var Network = require("./model/network.cjs").Network;
var Persistence = require("./model/persistence.cjs").Persistence;
var ServerState = require("./model/serverState.cjs").ServerState;
var Logging = require("./model/logging.cjs").Logging;
global.$$config = {};
// args will be ['node', 'server.js', 'ampm.json', 'dev.i14']
var configPath = "";
var configPaths = process.argv[2] ? process.argv[2].split(",") : ["ampm.json"];
var configScheme = process.argv[3] ? process.argv[3] : "";
// A persistent state object, saved to ampm-state.json.
global.$$serverState = new ServerState();
// load from server state if config is stored
if (global.$$serverState.get("configFile")) {
configPath = $$serverState.get("configFile");
} else {
configPath = configPaths[0];
}
if (configPath && fs.existsSync(configPath)) {
var config = fs.readFileSync(configPath, {
encoding: "UTF8",
});
// replace environment variables in the config file with their contents
process.env["CD"] = process.cwd(); // jshint ignore:line
config = config.replace(/%([^%]+)%/g, function (_, n) {
// also escape slashes
return (process.env[n] + "")
.replace(/[\\"']/g, "\\$&")
.replace(/[\\"']/g, "\\$&");
});
config = json.parse(config);
if (!config["default"]) {
// There are no schemes in the config, just ingest it whole.
console.log("Using single configuration.");
_.merge($$config, config);
} else {
// Merge the default config.
console.log("Merging config: default");
_.merge($$config, config["default"]);
var schemes = configScheme.split(".");
var currentScheme = "";
// Merge the schemes passed on the command line.
// "dev.foo" would merge "dev" then "dev.foo" then "dev.foo".
for (var i = 0; i < schemes.length; i++) {
currentScheme += schemes[i];
console.log("Merging config: " + currentScheme);
_.merge($$config, config[currentScheme]);
currentScheme += ".";
}
// Merge machine-specific schemes.
// "I14" would merge "I14", then "I14.dev", then "I14.dev.foo".
var machine = os.hostname();
console.log("Merging config: " + machine);
_.merge($$config, config[machine]);
currentScheme = "";
for (var i = 0; i < schemes.length; i++) {
currentScheme += schemes[i];
console.log("Merging config: " + machine + "." + currentScheme);
_.merge($$config, config[machine + "." + currentScheme]);
currentScheme += ".";
}
}
// Set the current working directory to the location of the config file,
// so that paths in the config file are relative to itself.
process.chdir(path.dirname(configPath));
$$serverState.saveState("lastConfig", $$config);
}
console.log("Server starting up.");
// Load the shared state plugin file.
global.$$plugin = null;
if ($$config.plugin && fs.existsSync($$config.plugin)) {
var plugin = require(path.resolve($$config.plugin)).Plugin;
global.$$plugin = new plugin();
}
// A container for all the network transports, generally accessed via $$network.transports.
global.$$network = new Network({
config: $$config.network,
});
// The manager of the application process, controlling restarts and heartbeats.
global.$$persistence = new Persistence({
config: $$config.persistence,
});
// The logging manager.
global.$$logging = new Logging({
config: $$config.logging,
});
// The back-end for the web console.
global.$$consoleState = new ConsoleState({
configs: configPaths,
});
// Start up components which depend on other components.
$$persistence.boot();
if ($$plugin) {
$$plugin.boot();
}
logger.info("Server started.");
logger.info(
"Console is at: http://" +
os.hostname() +
":" +
$$network.get("socketToConsolePort")
);