-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Closed
Update server.js #617
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f201d50
Update server.js
309bd55
Merge branch 'master' into michaelawyu-patch-2
c43ea27
Update server.js
michaelawyu 2c72a06
Update server.js
michaelawyu 262c6c4
Update package.json
michaelawyu c11778e
Update circle.yml
michaelawyu 1a009c2
Lint
michaelawyu 2c947cb
Update package.json
michaelawyu ffbc2c7
Rename server.js to app.js
michaelawyu 2fda19e
Update package.json
michaelawyu f28c0f9
Update package.json
michaelawyu 46b16b6
Update package.json
michaelawyu 25becdd
Merge branch 'master' into michaelawyu-patch-2
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/' + | ||
'v1/project/project-id'; | ||
|
||
function getExternalIp () { | ||
function getProjectId () { | ||
const options = { | ||
headers: { | ||
'Metadata-Flavor': 'Google' | ||
}, | ||
json: true | ||
json: false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.)