forked from parse-community/parse-server-example
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
121 lines (107 loc) · 4.88 KB
/
Copy pathindex.js
File metadata and controls
121 lines (107 loc) · 4.88 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
// Feb 2023, we tried to upgrade this project to be of type "module" and use import/export instead of require.
// It worked locally, but when we deployed to a real server, the parse server failed to start.
// I never could determine why. So I punted and reverted to using require.
const express = require("express");
const ParseServer = require("parse-server").ParseServer;
const ParseDashboard = require("parse-dashboard");
const BloomFirebaseAuthAdapter = require("./bloomFirebaseAuthAdapter");
const databaseUri = process.env.DATABASE_URI || process.env.MONGODB_URI;
if (!databaseUri) {
console.log("DATABASE_URI not specified, falling back to localhost.");
}
const serverConfig = {
// Somehow, node 18 causes localhost to try to resolve as IPv6 here which can break things.
// Using 127.0.0.1 instead works around that.
databaseURI: databaseUri || "mongodb://127.0.0.1:27017/dev",
cloud: process.env.CLOUD_CODE_MAIN || __dirname + "/cloud/main.js",
appId: process.env.APP_ID || "myAppId",
masterKey: process.env.MASTER_KEY || "123",
readOnlyMasterKey: process.env.READ_ONLY_MASTER_KEY || "ro",
serverURL: process.env.SERVER_URL || "http://localhost:1337/parse",
appName: process.env.APP_NAME || "BloomLibrary.org",
auth: { bloom: { module: BloomFirebaseAuthAdapter, enabled: true } },
masterKeyIps: process.env.PARSE_SERVER_MASTER_KEY_IPS
? process.env.PARSE_SERVER_MASTER_KEY_IPS.split(",")
: ["127.0.0.1", "::1"],
// NOTE for the future parse-server 9.x upgrade: add
// readOnlyMasterKeyIps: ["0.0.0.0/0", "::/0"],
// The option doesn't exist in 8.x (the server rejects it as an invalid key). In 9.x the
// read-only master key (used by the dashboard's readonly user, from any IP) gets its own
// IP allowlist, and parse-server 10 will default it to localhost-only (DEPPS15).
// Note: "::/0" is the spelling parse-server recognizes as allow-all-IPv6; "::0" is NOT
// (see UPGRADE-PLAN.md 5.1).
enforcePrivateUsers: false,
allowClientClassCreation: false,
};
const dashboard = new ParseDashboard({
apps: [
{
appId: serverConfig.appId,
serverURL: serverConfig.serverURL,
masterKey: serverConfig.masterKey,
readOnlyMasterKey: serverConfig.readOnlyMasterKey,
appName: serverConfig.appName,
production: serverConfig.serverURL.includes("production"),
},
],
trustProxy: 1,
users: [
{
user: serverConfig.appId,
pass: serverConfig.masterKey,
},
{
user: "master",
pass: serverConfig.masterKey,
},
{
user: "readonly",
pass: serverConfig.readOnlyMasterKey,
readOnly: true,
},
],
});
const app = express();
// Serve the Parse API on the /parse URL prefix
const mountPath = process.env.PARSE_MOUNT || "/parse";
const server = new ParseServer(serverConfig);
// For an unknown reason, when deployed on a real server, await server.start() causes the server to never successfully start.
server
.start()
.catch((error) => {
// Without this, a failed start is nearly silent: parse-server may try to report
// the failure through its own logger, which can itself be the broken piece
// (e.g. an unwritable logs folder), and the process just exits. Write to both
// stderr and, best-effort, the parse-server logs folder, then exit nonzero so
// the failure is unmistakable. (Cost us a debugging session on 2026-07-07.)
console.error("FATAL: parse-server failed to start:", error);
try {
const logsFolder = process.env.PARSE_SERVER_LOGS_FOLDER || "./logs";
require("fs").appendFileSync(
require("path").join(logsFolder, "startup-failure.log"),
new Date().toISOString() + " " + (error.stack || error) + "\n"
);
} catch {
// the logs folder itself may be the problem; stderr already has it
}
process.exit(1);
})
.then(() => {
app.use(mountPath, server.app);
// The main thing here is the google-site-verification meta tag.
// This lets us access the site on the Google Search Console.
app.get("/", function (req, res) {
res.status(200).send(
"<html>" +
'<head><meta name="google-site-verification" content="dm8VsqC5uw-fikoD-4ZxYbPfzV-qYyrPCJq7aIgvlJo" /></head>' +
'<body><a href="https://bloomlibrary.org">Bloom Library</a></body>' +
"</html>"
);
});
app.use("/dashboard", dashboard);
const port = process.env.PORT || 1337;
const httpServer = require("http").createServer(app);
httpServer.listen(port, function () {
console.log("bloom-parse-server running on port " + port + ".");
});
});