3
3
const fs = require ( 'fs' ) ;
4
4
const NodeRSA = require ( 'node-rsa' ) ;
5
5
const config = require ( 'config' ) ;
6
- const logger = require ( './logger' ) . global ;
6
+ const logger = require ( './logger' ) . setup ;
7
7
const userModel = require ( './models/user' ) ;
8
8
const userPermissionModel = require ( './models/user_permission' ) ;
9
9
const authModel = require ( './models/auth' ) ;
10
+ const debug_mode = process . env . NODE_ENV !== 'production' ;
10
11
11
12
module . exports = function ( ) {
12
13
return new Promise ( ( resolve , reject ) => {
@@ -22,6 +23,9 @@ module.exports = function () {
22
23
config_data = require ( filename ) ;
23
24
} catch ( err ) {
24
25
// do nothing
26
+ if ( debug_mode ) {
27
+ logger . debug ( filename + ' config file could not be required' ) ;
28
+ }
25
29
}
26
30
27
31
// Now create the keys and save them in the config.
@@ -40,12 +44,18 @@ module.exports = function () {
40
44
reject ( err ) ;
41
45
} else {
42
46
logger . info ( 'Wrote JWT key pair to config file: ' + filename ) ;
43
- config . util . loadFileConfigs ( ) ;
44
- resolve ( ) ;
47
+
48
+ logger . warn ( 'Restarting interface to apply new configuration' ) ;
49
+ process . exit ( 0 ) ;
45
50
}
46
51
} ) ;
52
+
47
53
} else {
48
54
// JWT key pair exists
55
+ if ( debug_mode ) {
56
+ logger . debug ( 'JWT Keypair already exists' ) ;
57
+ }
58
+
49
59
resolve ( ) ;
50
60
}
51
61
} )
@@ -54,49 +64,54 @@ module.exports = function () {
54
64
. query ( )
55
65
. select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
56
66
. where ( 'is_deleted' , 0 )
57
- . first ( 'count' )
58
- . then ( row => {
59
- if ( ! row . count ) {
60
- // Create a new user and set password
61
- logger . info ( 'Creating a new user: [email protected] with password: changeme' ) ;
67
+ . first ( ) ;
68
+ } )
69
+ . then ( row => {
70
+ if ( ! row . count ) {
71
+ // Create a new user and set password
72
+ logger . info ( 'Creating a new user: [email protected] with password: changeme' ) ;
62
73
63
- let data = {
64
- is_deleted : 0 ,
65
-
66
- name : 'Administrator' ,
67
- nickname : 'Admin' ,
68
- avatar : '' ,
69
- roles : [ 'admin' ]
70
- } ;
74
+ let data = {
75
+ is_deleted : 0 ,
76
+
77
+ name : 'Administrator' ,
78
+ nickname : 'Admin' ,
79
+ avatar : '' ,
80
+ roles : [ 'admin' ]
81
+ } ;
71
82
72
- return userModel
83
+ return userModel
84
+ . query ( )
85
+ . insertAndFetch ( data )
86
+ . then ( user => {
87
+ return authModel
73
88
. query ( )
74
- . insertAndFetch ( data )
75
- . then ( user => {
76
- return authModel
89
+ . insert ( {
90
+ user_id : user . id ,
91
+ type : 'password' ,
92
+ secret : 'changeme' ,
93
+ meta : { }
94
+ } )
95
+ . then ( ( ) => {
96
+ return userPermissionModel
77
97
. query ( )
78
98
. insert ( {
79
- user_id : user . id ,
80
- type : 'password' ,
81
- secret : 'changeme' ,
82
- meta : { }
83
- } )
84
- . then ( ( ) => {
85
- return userPermissionModel
86
- . query ( )
87
- . insert ( {
88
- user_id : user . id ,
89
- visibility : 'all' ,
90
- proxy_hosts : 'manage' ,
91
- redirection_hosts : 'manage' ,
92
- dead_hosts : 'manage' ,
93
- streams : 'manage' ,
94
- access_lists : 'manage' ,
95
- certificates : 'manage'
96
- } ) ;
99
+ user_id : user . id ,
100
+ visibility : 'all' ,
101
+ proxy_hosts : 'manage' ,
102
+ redirection_hosts : 'manage' ,
103
+ dead_hosts : 'manage' ,
104
+ streams : 'manage' ,
105
+ access_lists : 'manage' ,
106
+ certificates : 'manage'
97
107
} ) ;
98
108
} ) ;
99
- }
100
- } ) ;
109
+ } )
110
+ . then ( ( ) => {
111
+ logger . info ( 'Initial setup completed' ) ;
112
+ } ) ;
113
+ } else if ( debug_mode ) {
114
+ logger . debug ( 'Admin user setup not required' ) ;
115
+ }
101
116
} ) ;
102
117
} ;
0 commit comments