Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions appengine/metadata/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "appengine-metadata",
"description": "Sample for accessing the Compute metadata server on GAE.",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": {
"type": "git",
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
},
"engines": {
"node": ">=4"
},
"scripts": {
"lint": "samples lint",
"pretest": "npm run lint",
"system-test": "samples test app",
"test": "npm run system-test",
"e2e-test": "samples test deploy"
},
"dependencies": {
"express": "4.15.4",
"got": "7.1.0"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "1.4.17"
},
"cloud-repo-tools": {
"test": {
"app": {
"msg": "External IP:",
"args": [
"server.js"
]
}
},
"requiresKeyFile": false,
"requiresProjectId": false
}
}
60 changes: 60 additions & 0 deletions appengine/metadata/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2017, Google, Inc.
* 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.
*/

'use strict';

// [START appengine_metadata]
const express = require('express');
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';

function getExternalIp () {
const options = {
headers: {
'Metadata-Flavor': 'Google'
},
json: true
};

return request(METADATA_NETWORK_INTERFACE_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';
}
return Promise.reject(err);
});
}

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

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// [END appengine_metadata]
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ deployment:
- node scripts/build "appengine/errorreporting"
- node scripts/build "appengine/hello-world"
- node scripts/build "appengine/mailjet"
- node scripts/build "appengine/metadata"
- node scripts/build "appengine/static-files"
- GCLOUD_STORAGE_BUCKET=docs-samples-gae-test-$(uuid); node scripts/build "appengine/storage"
- node scripts/build "auth"
Expand Down