forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
179 lines (157 loc) · 6.28 KB
/
Copy pathapp.js
File metadata and controls
179 lines (157 loc) · 6.28 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Read config.js to grab redis info
var fs = require('fs'),
path = require('path'),
utils = require('./public/src/utils.js'),
args = {};
// Runtime environment
global.env = process.env.NODE_ENV || 'production',
// Parse any passed-in arguments
process.argv.slice(2).forEach(function(value) {
if (value.slice(0, 2) === '--') {
var arg = value.slice(2).split('=');
args[arg[0]] = arg[1] || true;
}
});
console.log('Info: Checking for valid base configuration file');
fs.readFile(path.join(__dirname, 'config.json'), function(err, data) {
if (!err && args.setup !== true) {
global.config = JSON.parse(data);
global.config.url = global.config.base_url + (global.config.use_port ? ':' + global.config.port : '') + '/';
global.config.upload_url = global.config.url + 'uploads/';
console.log('Info: Base Configuration OK.');
var meta = require('./src/meta.js');
meta.config.get(function(config) {
for(c in config) {
if (config.hasOwnProperty(c)) {
global.config[c] = config[c];
}
}
var categories = require('./src/categories.js'),
RDB = require('./src/redis.js'),
templates = require('./public/src/templates.js'),
webserver = require('./src/webserver.js'),
websockets = require('./src/websockets.js'),
admin = {
'categories': require('./src/admin/categories.js')
};
DEVELOPMENT = true;
global.configuration = {};
global.templates = {};
(function(config) {
config['ROOT_DIRECTORY'] = __dirname;
templates.init([
'header', 'footer', 'logout', 'admin/header', 'admin/footer', 'admin/index',
'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext',
'emails/header', 'emails/footer', 'install/header', 'install/footer', 'install/redis',
'noscript/header', 'noscript/home', 'noscript/category', 'noscript/topic'
]);
templates.ready(function() {
webserver.init();
});
//setup scripts to be moved outside of the app in future.
function setup_categories() {
console.log('Info: Checking categories...');
categories.getAllCategories(function(data) {
if (data.categories.length === 0) {
console.log('Info: Setting up default categories...');
fs.readFile(config.ROOT_DIRECTORY + '/install/data/categories.json', function(err, default_categories) {
default_categories = JSON.parse(default_categories);
for (var category in default_categories) {
admin.categories.create(default_categories[category]);
}
});
// Hardcoding uid 1 as an admin
var user = require('./src/user.js');
user.makeAdministrator(1);
} else {
console.log('Info: Good.');
}
});
}
setup_categories();
}(global.configuration));
});
} else {
// New install, ask setup questions
if (args.setup) console.log('Info: NodeBB Setup Triggered via Command Line');
else console.log('Info: Configuration not found, starting NodeBB setup');
var ask = function(question, callback) {
process.stdin.resume();
process.stdout.write(question + ': ');
process.stdin.once('data', function(data) {
callback(data.toString().trim());
});
}
process.stdout.write(
"\nWelcome to NodeBB!\nThis looks like a new installation, so you'll have to answer a " +
"few questions about your environment before we can proceed.\n\n" +
"Press enter to accept the default setting (shown in brackets).\n\n\n" +
"What is...\n\n"
);
ask('... the publically accessible URL of this installation? (http://localhost)', function(base_url) {
ask('... the port number of your install? (4567)', function(port) {
ask('Will you be using a port number to access NodeBB? (y)', function(use_port) {
ask('... the host IP or address of your Redis instance? (127.0.0.1)', function(redis_host) {
ask('... the host port of your Redis instance? (6379)', function(redis_port) {
ask('... the password of your Redis database? (no password)', function(redis_password) {
ask('... your NodeBB secret? (keyboard mash for a bit here)', function(secret) {
ask('... the number of rounds to use for bcrypt.genSalt? (10)', function(bcrypt_rounds) {
if (!base_url) base_url = 'http://localhost';
if (!port) port = 4567;
if (!use_port) use_port = true; else use_port = (use_port === 'y' ? true : false);
if (!redis_host) redis_host = '127.0.0.1';
if (!redis_port) redis_port = 6379;
if (!secret) secret = utils.generateUUID();
if (!bcrypt_rounds) bcrypt_rounds = 10;
var fs = require('fs'),
path = require('path'),
config = {
secret: secret,
base_url: base_url,
port: port,
use_port: use_port,
upload_path: '/public/uploads/',
bcrypt_rounds: bcrypt_rounds,
redis: {
host: redis_host,
port: redis_port,
password: redis_password
}
}
// Server-side config
fs.writeFile(path.join(__dirname, 'config.json'), JSON.stringify(config, null, 4), function(err) {
if (err) throw err;
else {
process.stdout.write(
"\n\nConfiguration Saved OK\n\n"
);
if (!args.setup) {
process.stdout.write(
"Please start NodeBB again and register a new user at " +
base_url + (use_port ? ':' + port : '') + "/register. This user will automatically become an administrator.\n\n"
);
}
process.stdout.write(
"If at any time you'd like to run this setup again, run the app with the \"--setup\" flag\n\n"
);
process.exit();
}
});
// Client-side config
fs.writeFile(path.join(__dirname, 'public', 'config.json'), JSON.stringify({
socket: {
address: base_url,
port: port
},
api_url: base_url + (use_port ? ':' + port : '') + '/api/'
}, null, 4));
});
});
});
});
});
});
});
});
}
});