forked from Spacebrew/spacebrew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_persistent_live.js
145 lines (132 loc) · 3.91 KB
/
node_persistent_live.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
/**
* Spacebrew Live Persist Server
* -----------------------------
*
* This script runs the Spacebrew Live Persist module as a standalone server.
* It can be used to add live persist functionality to any spacebrew server,
* local or remote.
*
* @author: Julio Terra
* @filename: node_persistent_live.js
* @date: May 31, 2013
* @updated with version: 0.3.0
*
*/
var logger = require('./logger')
, livePersister = require('./spacebrew_live_persist')
, port = 9000
, host = "localhost"
, help = false
;
/**
* Prints startup message to console/terminal
*/
var printStartupMsg = function() {
console.log("");
console.log("This is a tool for persisting all routes created in the standard spacebrew admin.");
console.log("Connecting to spacebrew server at " + host + ":" + port + ".");
console.log("");
console.log("===========================================");
console.log("");
}
/**
* Processes the command line arguments when app is launched
*/
var processArguments = function(){
var argv = process.argv;
for(var i = 2; i < argv.length; i++){
switch(argv[i]){
case "-console.log":
case "--log":
setLogLevel("info");
break;
case "--loglevel":
setLogLevel( argv[i++] );
break;
case "--host":
setSpacebrewHost(argv[++i]);
break;
case "-p":
case "--port":
setSpacebrewPort(argv[++i]);
break;
case "-h":
case "--help":
printHelp();
help = true;
break;
}
}
};
/**
* Set the port of the spacebrew server. defaults to 9000. Can be overridden using the
* flag -p or --port when starting up the persistent router.
*
* @type {Number}
*/
var setSpacebrewPort = function( newPort ){
newPort = parseInt( newPort );
//check that tempPort is a number and within valid port range
if (!isNaN( newPort ) && newPort >= 1 && newPort <= 65535){
port = tempPort;
logger.log("info", "[setSpacebrewHost] set spacebrew port to " + newPort);
}
};
/**
* Set the hostname of the device that hosts the spacebrew server. defaults to localhost. Can
* be overridden using the flag -h or --host when starting up the persistent router.
*
* @type {String}
*/
var setSpacebrewHost = function ( newHost ){
host = newHost;
logger.log("info", "[setSpacebrewHost] set spacebrew host to " + newHost);
}
/**
* method that is used to set the log level when user sets log level via command line
*
* @param {String} newLevel New log level - "error", "warn", "debug", or "info"
*/
var setLogLevel = function( newLevel ) {
logger.debugLevel = newLevel;
logger.log("info", "[setLogLevel] log level set to " + newLevel);
}
/**
* Prints the node server help message to screen
*/
var printHelp = function (){
console.log("command line parameters:");
console.log("\t--port (-p): set the port of the spacebrew server (default 9000)");
console.log("\t--host: the hostname of the spacebrew server (default localhost)");
console.log("\t--help (-h): print this help text");
console.log("examples:");
console.log("\tnode spacebrew_live_persist.js -p 9011");
console.log("\tnode spacebrew_live_persist.js -h my-sweet-computer");
console.log("");
};
/**
* Method that handles key app functions including printing app startup message, processing
* command line arguments, and starting up persistent server
*/
var main = function() {
var persist_configs = {};
printStartupMsg();
processArguments();
// if app command included help flag then don't run app
if (help) {
process.exit();
}
// if app command did not include help flag then start-up persist server
else {
persist_configs = {
"host": host,
"port": port,
"logLevel": logger.debugLevel
}
livePersister.persistRoutes( persist_configs );
}
}
/**
* Run the app
*/
main();