9
9
* environments: [development, test, production]
10
10
*
11
11
*/
12
- var _ = require ( 'underscore' ) ;
13
12
var fs = require ( 'fs' ) ;
14
13
var path = require ( 'path' ) ;
15
14
16
- var ENV = process . env . NODE_ENV || 'development' ;
17
-
18
- if ( process . argv [ 2 ] ) {
19
- ENV = process . argv [ 2 ] ;
15
+ var argv = require ( 'yargs' )
16
+ . usage ( 'Usage: $0 <environment> [options]' )
17
+ . help ( 'h' )
18
+ . example (
19
+ '$0 production -c /etc/sql-api/config.js' ,
20
+ 'start server in production environment with /etc/sql-api/config.js as config file'
21
+ )
22
+ . alias ( 'h' , 'help' )
23
+ . alias ( 'c' , 'config' )
24
+ . nargs ( 'c' , 1 )
25
+ . describe ( 'c' , 'Load configuration from path' )
26
+ . argv ;
27
+
28
+ var environmentArg = argv . _ [ 0 ] || process . env . NODE_ENV || 'development' ;
29
+ var configurationFile = path . resolve ( argv . config || './config/environments/' + environmentArg + '.js' ) ;
30
+ if ( ! fs . existsSync ( configurationFile ) ) {
31
+ console . error ( 'Configuration file "%s" does not exist' , configurationFile ) ;
32
+ process . exit ( 1 ) ;
20
33
}
21
34
22
- process . env . NODE_ENV = ENV ;
35
+ global . settings = require ( configurationFile ) ;
36
+ var ENVIRONMENT = argv . _ [ 0 ] || process . env . NODE_ENV || global . settings . environment ;
37
+ process . env . NODE_ENV = ENVIRONMENT ;
23
38
24
39
var availableEnvironments = [ 'development' , 'production' , 'test' , 'staging' ] ;
25
40
26
41
// sanity check arguments
27
- if ( availableEnvironments . indexOf ( ENV ) === - 1 ) {
28
- console . error ( "\nnode app.js [environment]" ) ;
29
- console . error ( "environments: " + availableEnvironments . join ( ', ' ) ) ;
42
+ if ( availableEnvironments . indexOf ( ENVIRONMENT ) === - 1 ) {
43
+ console . error ( "node app.js [environment]" ) ;
44
+ console . error ( "Available environments: " + availableEnvironments . join ( ', ' ) ) ;
30
45
process . exit ( 1 ) ;
31
46
}
32
47
33
- // set Node.js app settings and boot
34
- global . settings = require ( __dirname + '/config/settings' ) ;
35
- var env = require ( __dirname + '/config/environments/' + ENV ) ;
36
- env . api_hostname = require ( 'os' ) . hostname ( ) . split ( '.' ) [ 0 ] ;
37
- _ . extend ( global . settings , env ) ;
48
+ global . settings . api_hostname = require ( 'os' ) . hostname ( ) . split ( '.' ) [ 0 ] ;
38
49
39
50
global . log4js = require ( 'log4js' ) ;
40
- var log4js_config = {
41
- appenders : [ ] ,
42
- replaceConsole :true
51
+ var log4jsConfig = {
52
+ appenders : [ ] ,
53
+ replaceConsole : true
43
54
} ;
44
55
45
- if ( env . log_filename ) {
46
- var logdir = path . dirname ( env . log_filename ) ;
47
- // See cwd inlog4js.configure call below
48
- logdir = path . resolve ( __dirname , logdir ) ;
49
- if ( ! fs . existsSync ( logdir ) ) {
50
- console . error ( "Log filename directory does not exist: " + logdir ) ;
56
+ if ( global . settings . log_filename ) {
57
+ var logFilename = path . resolve ( global . settings . log_filename ) ;
58
+ var logDirectory = path . dirname ( logFilename ) ;
59
+ if ( ! fs . existsSync ( logDirectory ) ) {
60
+ console . error ( "Log filename directory does not exist: " + logDirectory ) ;
51
61
process . exit ( 1 ) ;
52
62
}
53
- console . log ( "Logs will be written to " + env . log_filename ) ;
54
- log4js_config . appenders . push (
55
- { type : "file" , filename : env . log_filename }
63
+ console . log ( "Logs will be written to " + logFilename ) ;
64
+ log4jsConfig . appenders . push (
65
+ { type : "file" , absolute : true , filename : logFilename }
56
66
) ;
57
67
} else {
58
- log4js_config . appenders . push (
68
+ log4jsConfig . appenders . push (
59
69
{ type : "console" , layout : { type :'basic' } }
60
70
) ;
61
71
}
62
-
63
- if ( global . settings . rollbar ) {
64
- log4js_config . appenders . push ( {
65
- type : __dirname + "/app/models/log4js_rollbar.js" ,
66
- options : global . settings . rollbar
67
- } ) ;
68
- }
69
-
70
- global . log4js . configure ( log4js_config , { cwd : __dirname } ) ;
72
+ global . log4js . configure ( log4jsConfig ) ;
71
73
global . logger = global . log4js . getLogger ( ) ;
72
74
73
75
@@ -78,12 +80,14 @@ if ( ! global.settings.base_url ) {
78
80
79
81
var version = require ( "./package" ) . version ;
80
82
81
- var app = require ( global . settings . app_root + '/app/app' ) ( ) ;
82
- app . listen ( global . settings . node_port , global . settings . node_host , function ( ) {
83
- console . log (
84
- "CartoDB SQL API %s listening on %s:%s with base_url %s (%s)" ,
85
- version , global . settings . node_host , global . settings . node_port , global . settings . base_url , ENV
86
- ) ;
83
+ var server = require ( './app/server' ) ( ) ;
84
+ var listener = server . listen ( global . settings . node_port , global . settings . node_host ) ;
85
+ listener . on ( 'listening' , function ( ) {
86
+ console . info ( 'Using configuration file "%s"' , configurationFile ) ;
87
+ console . log (
88
+ "CartoDB SQL API %s listening on %s:%s PID=%d (%s)" ,
89
+ version , global . settings . node_host , global . settings . node_port , process . pid , ENVIRONMENT
90
+ ) ;
87
91
} ) ;
88
92
89
93
process . on ( 'uncaughtException' , function ( err ) {
@@ -92,15 +96,19 @@ process.on('uncaughtException', function(err) {
92
96
93
97
process . on ( 'SIGHUP' , function ( ) {
94
98
global . log4js . clearAndShutdownAppenders ( function ( ) {
95
- global . log4js . configure ( log4js_config ) ;
99
+ global . log4js . configure ( log4jsConfig ) ;
96
100
global . logger = global . log4js . getLogger ( ) ;
97
101
console . log ( 'Log files reloaded' ) ;
98
102
} ) ;
103
+
104
+ if ( server . batch && server . batch . logger ) {
105
+ server . batch . logger . reopenFileStreams ( ) ;
106
+ }
99
107
} ) ;
100
108
101
109
process . on ( 'SIGTERM' , function ( ) {
102
- app . batch . stop ( ) ;
103
- app . batch . drain ( function ( err ) {
110
+ server . batch . stop ( ) ;
111
+ server . batch . drain ( function ( err ) {
104
112
if ( err ) {
105
113
console . log ( 'Exit with error' ) ;
106
114
return process . exit ( 1 ) ;
0 commit comments