generated from GoogleCloudPlatform/cloud-run-microservice-template-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (30 loc) · 850 Bytes
/
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
import app from "./app.js";
import { logger, initLogCorrelation } from "./utils/logging.js";
import { fetchProjectId } from "./utils/metadata.js";
/**
* Initialize app and start Express server
*/
const main = async () => {
let project = process.env.GOOGLE_CLOUD_PROJECT;
if (!project) {
try {
project = await fetchProjectId();
} catch (err) {
logger.warn("Could not fetch Project Id for tracing.");
}
}
// Initialize request-based logger with project Id
initLogCorrelation(project);
// Start server listening on PORT env var
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => logger.info(`Listening on port ${PORT}`));
};
/**
* Listen for termination signal
*/
process.on("SIGTERM", () => {
// Clean up resources on shutdown
logger.info("Caught SIGTERM.");
logger.flush();
});
main();