forked from firebase/firebase-tools-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (111 loc) Β· 3.59 KB
/
server.js
File metadata and controls
122 lines (111 loc) Β· 3.59 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
/**
* Copyright 2020 Google LLC
*
* 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.
*/
/*
This is the entry point for starting the server process for both
static content and Emulator-UI-specific APIs. Please make sure to only use
language features available in Node.js 8+ (i.e. no Type annotations).
*/
const express = require('express');
const fetch = require('node-fetch').default;
const path = require('path');
/**
Start an express app that serves both static content and APIs.
*/
exports.startServer = function () {
const app = express();
exports.registerApis(app);
const webDir = path.join(path.dirname(process.argv[1]), 'build');
app.use(express.static(webDir));
// Required for the router to work properly.
app.get('*', function (req, res) {
res.sendFile(path.join(webDir, 'index.html'));
});
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || 3000;
app.listen(port, host, () => {
console.log(
`Web / API server started at http://${hostAndPort(host, port)}`
);
});
return app;
};
/*
Add the API routes to an express app.
These are also available through development server via ./setupProxy.js,
however, hot-reloading DOES NOT WORK with this file. Restart instead.
*/
exports.registerApis = function (app) {
const projectEnv = 'GCLOUD_PROJECT';
const hubEnv = 'FIREBASE_EMULATOR_HUB';
const projectId = process.env[projectEnv];
const hubHost = process.env[hubEnv];
if (!projectId || !hubHost) {
throw new Error(
`Please specify these environment variables: ${projectEnv} ${hubEnv}\n` +
'(Are you using firebase-tools@>=7.14.0 with `--project your-project`?)'
);
}
// Exposes the host and port of various emulators to facilitate accessing
// them using client SDKs. For features that involve multiple emulators or
// hard to accomplish using client SDKs, consider adding an API below.
app.get(
'/api/config',
jsonHandler(async (req) => {
const emulatorsRes = await fetch(`http://${hubHost}/emulators`);
const emulators = await emulatorsRes.json();
const json = { projectId };
Object.entries(emulators).forEach(([name, info]) => {
let host = info.host;
if (host === '0.0.0.0') {
host = '127.0.0.1';
} else if (host === '::') {
host = '::1';
}
json[name] = {
...info,
host,
hostAndPort: hostAndPort(host, info.port),
};
});
return json;
})
);
};
function hostAndPort(host, port) {
// Correctly put IPv6 addresses in brackets.
return host.indexOf(':') >= 0 ? `[${host}]:${port}` : `${host}:${port}`;
}
function jsonHandler(handler) {
return (req, res) => {
handler(req).then(
(body) => {
res.status(200).json(body);
},
(err) => {
console.error(err);
res.status(500).json({
message: err.message,
stack: err.stack,
raw: err,
});
}
);
};
}
// When this file is ran directly like `node server.js`, just start the server.
if (require.main === module) {
exports.startServer();
}