-
Notifications
You must be signed in to change notification settings - Fork 166
/
index.js
347 lines (298 loc) · 11.2 KB
/
index.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env node
// ref. parse-server/index.js
// ref. parse-server/bin/parse-server
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var links = require('docker-links').parseLinks(process.env);
var fs = require('fs');
var AzureStorageAdapter = require('parse-server-azure-storage').AzureStorageAdapter;
var databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI
if (!databaseUri) {
if (links.mongo) {
databaseUri = 'mongodb://' + links.mongo.hostname + ':' + links.mongo.port + '/dev';
}
}
if (!databaseUri) {
console.log('DATABASE_URI not specified, falling back to localhost.');
}
var facebookAppIds = process.env.FACEBOOK_APP_IDS;
if (facebookAppIds) {
facebookAppIds = facebookAppIds.split(",");
}
var gcmId = process.env.GCM_ID;
var gcmKey = process.env.GCM_KEY;
var iosPushConfigs = new Array();
var isFile = function(f) {
var b = false;
try {
b = fs.statSync(f).isFile();
} catch (e) {
}
return b;
}
var productionBundleId = process.env.PRODUCTION_BUNDLE_ID;
var productionPfx = process.env.PRODUCTION_PFX || '/certs/production-pfx.p12';
productionPfx = isFile(productionPfx) ? productionPfx : null;
var productionCert = process.env.PRODUCTION_CERT || '/certs/production-pfx-cert.pem';
productionCert = isFile(productionCert) ? productionCert : null;
var productionKey = process.env.PRODUCTION_KEY || '/certs/production-pfx-key.pem';
productionKey = isFile(productionKey) ? productionKey : null;
var productionPassphrase = process.env.PRODUCTION_PASSPHRASE || null;
var productionPushConfig;
if (productionBundleId && (productionPfx || (productionCert && productionKey))) {
productionPushConfig = {
pfx: productionPfx,
cert: productionCert,
key: productionKey,
passphrase: productionPassphrase,
bundleId: productionBundleId,
production: true
};
iosPushConfigs.push(productionPushConfig);
}
var devBundleId = process.env.DEV_BUNDLE_ID;
var devPfx = process.env.DEV_PFX || '/certs/dev-pfx.p12';
devPfx = isFile(devPfx) ? devPfx : null;
var devCert = process.env.DEV_CERT || '/certs/dev-pfx-cert.pem';
devCert = isFile(devCert) ? devCert : null;
var devKey = process.env.DEV_KEY || '/certs/dev-pfx-key.pem';
devKey = isFile(devKey) ? devKey : null;
var devPassphrase = process.env.DEV_PASSPHRASE || null;
var devPushConfig;
if (devBundleId && (devPfx || (devCert && devKey))) { // exsiting files if not null
devPushConfig = {
pfx: devPfx,
cert: devCert,
key: devKey,
passphrase: devPassphrase,
bundleId: devBundleId,
production: false
};
iosPushConfigs.push(devPushConfig);
}
if(process.env.APNS_BUNDLES_ID && process.env.APNS_BUNDLES_P12 && process.env.APNS_BUNDLES_PROD) {
var APNSBundlesId = process.env.APNS_BUNDLES_ID.split(',').map(function(entry) {
return entry.trim();
});
var APNSBundlesP12 = process.env.APNS_BUNDLES_P12.split(',').map(function(entry) {
return entry.trim();
});
var APNSBundlesProd = process.env.APNS_BUNDLES_PROD.split(',').map(function(entry) {
return entry.trim();
});
if(APNSBundlesId.length === APNSBundlesP12.length && APNSBundlesP12.length === APNSBundlesProd.length) {
for (var i = 0; i < APNSBundlesId.length; i++) {
APNSpushConfig = {
pfx: APNSBundlesP12[i],
bundleId: APNSBundlesId[i],
production: (APNSBundlesProd[i] === 'true' ? true : false)
};
iosPushConfigs.push(APNSpushConfig);
}
}
}
var pushConfig = {};
if (gcmId && gcmKey) {
pushConfig.android = {
senderId: gcmId,
apiKey: gcmKey
}
}
if (iosPushConfigs.length > 0) {
pushConfig.ios = iosPushConfigs;
//console.log('Multiple iOS push configurations.')
}
console.log(pushConfig);
var port = process.env.PORT || 1337;
// Serve the Parse API on the /parse URL prefix
var mountPath = process.env.PARSE_MOUNT || '/parse';
var serverURL = process.env.SERVER_URL || 'http://localhost:' + port + mountPath; // Don't forget to change to https if needed
var S3Adapter = require('parse-server').S3Adapter;
var GCSAdapter = require('parse-server').GCSAdapter;
//var FileSystemAdapter = require('parse-server').FileSystemAdapter;
var filesAdapter;
if (process.env.S3_ACCESS_KEY &&
process.env.S3_SECRET_KEY &&
process.env.S3_BUCKET) {
var directAccess = !!+(process.env.S3_DIRECT);
filesAdapter = new S3Adapter(
process.env.S3_ACCESS_KEY,
process.env.S3_SECRET_KEY,
process.env.S3_BUCKET,
{directAccess: directAccess});
} else if (process.env.GCP_PROJECT_ID &&
process.env.GCP_KEYFILE_PATH &&
process.env.GCS_BUCKET) {
var directAccess = !!+(process.env.GCS_DIRECT);
filesAdapter = new GCSAdapter(
process.env.GCP_PROJECT_ID,
process.env.GCP_KEYFILE_PATH,
process.env.GCS_BUCKET,
{directAccess: directAccess});
} else if (process.env.AZURE_ACCOUNT &&
process.env.AZURE_CONTAINER &&
process.env.AZURE_ACCESS_KEY) {
var directAccess = !!+(process.env.AZURE_DIRECT);
filesAdapter = new AzureStorageAdapter(
process.env.AZURE_ACCOUNT,
process.env.AZURE_CONTAINER,
{
accessKey: process.env.AZURE_ACCESS_KEY,
directAccess: directAccess
});
}
var emailModule = process.env.EMAIL_MODULE;
var verifyUserEmails = !!+(process.env.VERIFY_USER_EMAILS);
var emailAdapter;
if (!emailModule) {
verifyUserEmails = false;
} else {
var emailAdapterOptions = {
fromAddress: process.env.EMAIL_FROM,
domain: process.env.EMAIL_DOMAIN,
apiKey: process.env.EMAIL_API_KEY
};
if (process.env.EMAIL_VERIFICATION_SUBJECT) {
emailAdapterOptions.verificationSubject = process.env.EMAIL_VERIFICATION_SUBJECT;
}
if (process.env.EMAIL_VERIFICATION_BODY) {
emailAdapterOptions.verificationBody = process.env.EMAIL_VERIFICATION_BODY;
}
if (process.env.EMAIL_VERIFICATION_BODY_HTML) {
emailAdapterOptions.verificationBodyHTML = fs.readFileSync(process.env.EMAIL_VERIFICATION_BODY_HTML, "utf8") || process.env.EMAIL_VERIFICATION_BODY_HTML;
}
if (process.env.EMAIL_PASSWORD_RESET_SUBJECT) {
emailAdapterOptions.passwordResetSubject = process.env.EMAIL_PASSWORD_RESET_SUBJECT;
}
if (process.env.EMAIL_PASSWORD_RESET_BODY) {
emailAdapterOptions.passwordResetBody = process.env.EMAIL_PASSWORD_RESET_BODY;
}
if (process.env.EMAIL_PASSWORD_RESET_BODY_HTML) {
emailAdapterOptions.passwordResetBodyHTML = fs.readFileSync(process.env.EMAIL_PASSWORD_RESET_BODY_HTML, "utf8") || process.env.EMAIL_PASSWORD_RESET_BODY_HTML;
}
emailAdapter = {
module: emailModule,
options: emailAdapterOptions
};
}
console.log(verifyUserEmails);
console.log(emailModule);
console.log(emailAdapter);
var enableAnonymousUsers = !!+(process.env.ENABLE_ANON_USERS);
var allowClientClassCreation = !!+(process.env.ALLOW_CLIENT_CLASS_CREATION);
var liveQuery = process.env.LIVEQUERY_SUPPORT;
console.log("LIVEQUERY_SUPPORT: " + liveQuery);
var liveQueryParam;
if(liveQuery) {
var liveQueryClasses = process.env.LIVEQUERY_CLASSES.split(',').map(function(entry) {
return entry.trim();
});
console.log("LIVEQUERY_CLASSES: " + liveQueryClasses);
liveQueryParam = {
classNames: liveQueryClasses
};
}
var databaseOptions = {};
if (process.env.DATABASE_TIMEOUT) {
databaseOptions = {
socketTimeoutMS: +(process.env.DATABASE_TIMEOUT)
};
}
var auth = {};
for (var env in process.env) {
if (!process.env.hasOwnProperty(env)) {
return;
}
var env_parameters = /^AUTH_([^_]*)_(.+)/.exec(env);
if (env_parameters !== null) {
if (typeof auth[env_parameters[1].toLowerCase()] === "undefined") {
auth[env_parameters[1].toLowerCase()] = {};
}
auth[env_parameters[1].toLowerCase()][env_parameters[2].toLowerCase()] = process.env[env];
}
}
var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://localhost:27017/dev',
databaseOptions: databaseOptions,
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myAppId',
masterKey: process.env.MASTER_KEY, //Add your master key here. Keep it secret!
serverURL: serverURL,
collectionPrefix: process.env.COLLECTION_PREFIX,
clientKey: process.env.CLIENT_KEY,
restAPIKey: process.env.REST_API_KEY,
javascriptKey: process.env.JAVASCRIPT_KEY,
dotNetKey: process.env.DOTNET_KEY,
fileKey: process.env.FILE_KEY,
filesAdapter: filesAdapter,
auth: auth,
facebookAppIds: facebookAppIds,
maxUploadSize: process.env.MAX_UPLOAD_SIZE,
push: pushConfig,
verifyUserEmails: verifyUserEmails,
emailAdapter: emailAdapter,
enableAnonymousUsers: enableAnonymousUsers,
allowClientClassCreation: allowClientClassCreation,
//oauth = {},
appName: process.env.APP_NAME,
publicServerURL: process.env.PUBLIC_SERVER_URL,
liveQuery: liveQueryParam,
logLevel: process.env.LOG_LEVEL || 'info'
//customPages: process.env.CUSTOM_PAGES || // {
//invalidLink: undefined,
//verifyEmailSuccess: undefined,
//choosePassword: undefined,
//passwordResetSuccess: undefined
//}
});
//console.log("appId: " + api.appId);
//console.log("masterKey: " + api.masterKey);
//console.log("cloud: " + api.cloud);
//console.log("databaseURI: " + api.databaseURI);
console.log("appId: " + process.env.APP_ID);
console.log("masterKey: " + process.env.MASTER_KEY);
var app = express();
var trustProxy = !!+(process.env.TRUST_PROXY || '1'); // default enable trust
if (trustProxy) {
console.log("trusting proxy: " + process.env.TRUST_PROXY);
app.enable('trust proxy');
}
app.use(mountPath, api);
// Parse Server plays nicely with the rest of your web routes
app.get('/', function(req, res) {
res.status(200).send('I dream of being a web site.');
});
if(liveQuery) {
console.log("Starting live query server");
var httpServer = require('http').createServer(app);
httpServer.listen(port);
console.log('plac');
var parseLiveQueryServer = ParseServer.createLiveQueryServer(httpServer);
} else {
app.listen(port, function() {
console.log('docker-parse-server running on ' + serverURL + ' (:' + port + mountPath + ')');
});
}
// GraphQL
var isSupportGraphQL = process.env.GRAPHQL_SUPPORT;
var schemaURL = process.env.GRAPHQL_SCHEMA || './cloud/graphql/schema.js';
console.log('isSupportGraphQL :', isSupportGraphQL);
console.log('schemaURL :', schemaURL);
if(isSupportGraphQL){
console.log('Starting GraphQL...');
var IS_DEVELOPMENT = process.env.NODE_ENV !== 'production';
function getSchema() {
if (IS_DEVELOPMENT) {
delete require.cache[require.resolve(schemaURL)];
}
return require(schemaURL);
}
var graphQLHTTP = require('express-graphql');
app.use('/graphql', graphQLHTTP(function(request){ return {
graphiql: IS_DEVELOPMENT,
pretty: IS_DEVELOPMENT,
schema: getSchema()
}}));
// TOHAVE : Support custom `./graphql` path and maybe `port`?
isSupportGraphQL && console.log('GraphQL running on ' + serverURL.split(port + mountPath).join(port) + '/graphql');
}