Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server.js #617

Closed
wants to merge 13 commits into from
19 changes: 10 additions & 9 deletions appengine/metadata/standard/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,32 @@ const request = require('got');
const app = express();
app.enable('trust proxy');

const METADATA_NETWORK_INTERFACE_URL = 'http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip';
const METADATA_PROJECT_ID_URL = 'http://metadata.google.internal/computeMetadata/' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can this be on one line? (You may have to add an // eslint-disable at the end of it if you get linter issues.)

'v1/project/project-id';

function getExternalIp () {
function getProjectId () {
const options = {
headers: {
'Metadata-Flavor': 'Google'
},
json: true
json: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: do we need to explicitly specify this?

};

return request(METADATA_NETWORK_INTERFACE_URL, options)
return request(METADATA_PROJECT_ID_URL, options)
.then((response) => response.body)
.catch((err) => {
if (err && err.statusCode !== 200) {
console.log('Error while talking to metadata server, assuming localhost');
return 'localhost';
console.log('Error while talking to metadata server, assuming UnknownProjectID');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a more obvious default value? Or perhaps throw an error outright?

return 'UnknownProjectID';
}
return Promise.reject(err);
});
}

app.get('/', (req, res, next) => {
getExternalIp()
.then((externalIp) => {
res.status(200).send(`External IP: ${externalIp}`).end();
getProjectId()
.then((projectId) => {
res.status(200).send(`Project ID: ${projectId}`).end();
})
.catch(next);
});
Expand Down