-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (39 loc) · 1.29 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
const express = require('express');
const {readFileSync} = require('fs');
const handlebars = require('handlebars');
const app = express();
// Serve the files in /assets at the URI /assets.
app.use('/assets', express.static('assets'));
// The HTML content is produced by rendering a handlebars template.
// The template values are stored in global state for reuse.
const data = {
service: process.env.K_SERVICE || '???',
revision: process.env.K_REVISION || '???',
};
let template;
app.get('/', async (req, res) => {
// The handlebars template is stored in global state so this will only once.
if (!template) {
// Load Handlebars template from filesystem and compile for use.
try {
template = handlebars.compile(readFileSync('index.html.hbs', 'utf8'));
} catch (e) {
console.error(e);
res.status(500).send('Internal Server Error');
}
}
// Apply the template to the parameters to generate an HTML string.
try {
const output = template(data);
res.status(200).send(output);
} catch (e) {
console.error(e);
res.status(500).send('Internal Server Error');
}
});
const PORT = process.env.PORT || 8082;
app.listen(PORT, () => {
console.log(
`Hello from Cloud Run! The container started successfully and is listening for HTTP requests on ${PORT}`
);
});