33const fs = require ( 'fs' ) ;
44const NodeRSA = require ( 'node-rsa' ) ;
55const config = require ( 'config' ) ;
6- const logger = require ( './logger' ) . global ;
6+ const logger = require ( './logger' ) . setup ;
77const userModel = require ( './models/user' ) ;
88const userPermissionModel = require ( './models/user_permission' ) ;
99const authModel = require ( './models/auth' ) ;
10+ const debug_mode = process . env . NODE_ENV !== 'production' ;
1011
1112module . exports = function ( ) {
1213 return new Promise ( ( resolve , reject ) => {
@@ -22,6 +23,9 @@ module.exports = function () {
2223 config_data = require ( filename ) ;
2324 } catch ( err ) {
2425 // do nothing
26+ if ( debug_mode ) {
27+ logger . debug ( filename + ' config file could not be required' ) ;
28+ }
2529 }
2630
2731 // Now create the keys and save them in the config.
@@ -40,12 +44,18 @@ module.exports = function () {
4044 reject ( err ) ;
4145 } else {
4246 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 ) ;
4550 }
4651 } ) ;
52+
4753 } else {
4854 // JWT key pair exists
55+ if ( debug_mode ) {
56+ logger . debug ( 'JWT Keypair already exists' ) ;
57+ }
58+
4959 resolve ( ) ;
5060 }
5161 } )
@@ -54,49 +64,54 @@ module.exports = function () {
5464 . query ( )
5565 . select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
5666 . 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: admin@example.com 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: admin@example.com with password: changeme' ) ;
6273
63- let data = {
64- is_deleted : 0 ,
65- email : 'admin@example.com' ,
66- name : 'Administrator' ,
67- nickname : 'Admin' ,
68- avatar : '' ,
69- roles : [ 'admin' ]
70- } ;
74+ let data = {
75+ is_deleted : 0 ,
76+ email : 'admin@example.com' ,
77+ name : 'Administrator' ,
78+ nickname : 'Admin' ,
79+ avatar : '' ,
80+ roles : [ 'admin' ]
81+ } ;
7182
72- return userModel
83+ return userModel
84+ . query ( )
85+ . insertAndFetch ( data )
86+ . then ( user => {
87+ return authModel
7388 . 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
7797 . query ( )
7898 . 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'
97107 } ) ;
98108 } ) ;
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+ }
101116 } ) ;
102117} ;
0 commit comments