forked from ukmadlz/parse-on-bluemix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
124 lines (102 loc) · 3.58 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
// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
'use strict';
// Required Libs
var express = require('express');
var ParseServer = require('parse-server').ParseServer;
var http = require('http');
// Start Express
var app = express();
// Validate Keys
if (!process.env.APP_ID) {
throw 'Please apply the Application ID from Parse.com';
}
if (!process.env.MASTER_KEY) {
throw 'Please apply the Master Key from Parse.com';
}
if (process.env.DATABASE_URI) {
var databaseUri = process.env.DATABASE_URI;
} else if (process.env.VCAP_SERVICES) {
var vcapServices = JSON.parse(process.env.VCAP_SERVICES);
const pattern = /mongo/i;
for (var i = 0; i < vcapServices['user-provided'].length; i++) {
if (vcapServices['user-provided'][i].name.search(pattern) >= 0 ||
vcapServices['user-provided'][i].credentials.uri.search(pattern) >= 0) {
var databaseUri = 'mongodb://' +
vcapServices['user-provided'][i].credentials.user +
':' + vcapServices['user-provided'][i].credentials.password +
'@' + vcapServices['user-provided'][i].credentials.uri;
break;
}
}
} else {
throw 'Please provide DATABASE_URI to an instance of MongoDB or deploy to Bluemix with a Compose MongoDB service';
}
// Server Location
var port = process.env.VCAP_APP_PORT || process.env.PORT || 1337;
var host = process.env.VCAP_APP_HOST || 'localhost';
var mountPath = process.env.PARSE_MOUNT || '/';
// Specify the connection string for your mongodb database
// and the location to your Parse cloud code
var parseConfig = {
databaseURI: databaseUri,
appId: process.env.APP_ID,
masterKey: process.env.MASTER_KEY,
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
serverURL: ((process.env.HTTPS) ? 'https://' : 'http://') + host + ':' + port + mountPath,
};
// Optional Keys
if (process.env.FILE_KEY) {
// String
parseConfig.fileKey = process.env.FILE_KEY;
}
if (process.env.CLIENT_KEY) {
// String
parseConfig.clientKey = process.env.CLIENT_KEY;
}
if (process.env.JS_KEY) {
// String
parseConfig.javascriptKey = process.env.JS_KEY;
}
if (process.env.REST_KEY) {
// String
parseConfig.restAPIKey = process.env.REST_KEY;
}
if (process.env.DOTNET_KEY) {
// String
parseConfig.dotNetKey = process.env.DOTNET_KEY;
}
if (process.env.ALLOW_CLIENT_CLASS_CREATION) {
// Boolean
parseConfig.allowClientClassCreation = process.env.ALLOW_CLIENT_CLASS_CREATION;
}
if (process.env.ENABLE_ANONYMOUS_USERS) {
// Boolean
parseConfig.enableAnonymousUsers = process.env.ENABLE_ANONYMOUS_USERS;
}
if (process.env.OAUTH) {
// Object: https://github.com/ParsePlatform/parse-server/wiki/Parse-Server-Guide#oauth
parseConfig.oauth = process.env.OAUTH;
}
if (process.env.FACEBOOK_APP_IDS) {
// Array
parseConfig.facebookAppIds = process.env.FACEBOOK_APP_IDS;
}
// Create Parse Server instance
var api = new ParseServer(parseConfig);
// Serve the Parse API on the / URL prefix
app.use(mountPath, api);
// And listen to requests
app.listen(port, host, function() {
console.log('parse-server running on port ' + port + '.');
});
require("cf-deployment-tracker-client").track();