This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
203 lines (186 loc) · 6.33 KB
/
setup.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'use strict';
var config = require('./config'),
db = require('./db');
var fs = require('fs');
function verifyConfig() {
// Go through and ensure all config settings are set correctly
// Static values
if (!config.root || config.root == '') {
console.log('No root address given');
return false;
}
if (!config.secret || config.secret == '') {
console.log('No secret key given');
return false;
}
if (!config.name || config.name == '') {
console.log('No name given');
return false;
}
// API keys
if (!config.googleClientId || !config.googleClientSecret || config.googleClientId == '' || config.googleClientSecret == '') {
console.log('Google API key incorrect');
return false;
}
if (!config.facebookClientId || !config.facebookClientSecret || config.facebookClientId == '' || config.facebookClientSecret == '') {
console.log('Facebook API key incorrect');
return false;
}
if (!config.sendgridAPIkey || !config.sendgridAPIuser || config.sendgridAPIkey == '' || config.sendgridAPIuser == '') {
console.log('Sendgrid API key incorrect');
return false;
}
// Email addresses
if (!config.reportEmail || config.reportEmail == '') {
console.log('Report email missing');
return false;
} else if (config.reportEmail.indexOf('@') == -1 || config.reportEmail.split('@')[1].indexOf('.') == -1) {
console.log('Report email format incorrect');
return false;
}
if (!config.supportEmail || config.supportEmail == '') {
console.log('Support email missing');
return false;
} else if (config.supportEmail.indexOf('@') == -1 || config.supportEmail.split('@')[1].indexOf('.') == -1) {
console.log('Support email format incorrect');
return false;
}
// Lists
if (!config.userActivationStatus) {
console.log('User Activation Status not set');
return false;
} else if (config.userActivationStatus.length !== 10) {
console.log('User activation statuses missing, should be 10');
return false;
} else {
for (var status in config.userActivationStatus) {
if (status == '') {
console.log('Status name not given correctly');
return false;
}
}
}
// Database connection
if (!config.mysqlHost || config.mysqlHost == '') {
console.log('No mysqlHost given');
return false;
}
if (!config.mysqlDatabase || config.mysqlDatabase == '') {
console.log('No mysqlDatabase given');
return false;
}
if (!config.mysqlPass || config.mysqlPass == '') {
console.log('No mysqlPass given');
return false;
}
if (!config.mysqlUser || config.mysqlUser == '') {
console.log('No mysqlUser given');
return false;
}
if (!config.mysqlPort) {
console.log('No mysqlPort given');
return false;
} else if (config.mysqlPort < 0 || config.mysqlPort > 65535) {
console.log('mysqlPort out of range');
return false;
}
var mysql = require('mysql');
var conn = mysql.createConnection({
host: config.mysqlHost,
user: config.mysqlUser,
password: config.mysqlPass,
database: config.mysqlDatabase,
port: config.mysqlPort
});
conn.connect(function (err) {
conn.end();
if (err) {
console.log('Unable to connect to database');
console.log(err);
return false;
}
});
// Files
if (!config.termsFile || config.termsFile == '') {
console.log('Terms file not set');
return false;
}
/*fs.access(config.termsFile, fs.F_OK, function (err) {
if (err) {
console.log('Unable to find terms file');
return false;
}
});
fs.access(config.termsFile, fs.R_OK, function (err) {
if (err) {
console.log('Unable to read terms file');
return false;
}
});*/
fs.readFile(config.termsFile, 'utf8', function (err, content) {
if (err) {
console.log('Error reading terms file');
console.log(err);
return false;
}
if (!content || content.trim() == '') {
console.log('Terms file is empty');
return false;
}
});
return true;
}
function createDatabase() {
// Create the database based on a series of tests
/*fs.access('dbsetup.sql', fs.R_OK, function (err) {
if (err) {
console.log('Unable to read database setup script');
return false;
}*/
fs.readFile('dbsetup.sql', 'utf8', function (err, content) {
if (err) {
console.log('Error reading database setup script');
return false;
}
content = content.replace('nouseaccount', config.mysqlDatabase); // Set correct database name in script
var mysql = require('mysql');
var conn = mysql.createConnection({
host: config.mysqlHost,
user: config.mysqlUser,
password: config.mysqlPass,
database: config.mysqlDatabase,
port: config.mysqlPort,
multipleStatements: true
});
conn.connect(function (err) {
if (err) {
console.log('Unable to connect to database');
console.log(err);
return false;
}
});
conn.query(content, function (err, results) {
// Results of database creation
conn.end();
if (err) {
console.log(err);
return false;
}
});
});
//});
return true;
}
function updateDatabase() {
// Make any changes to the database from the default configuration
// Currently there are no updates so there is no need to use it
return true;
}
exports.setup = function () {
console.log('Setting up account');
if (!verifyConfig()) return false;
if (!createDatabase()) return false;
if (!updateDatabase()) return false;
console.log('account setup successful!');
return true;
}