Skip to content

Commit 706532b

Browse files
authored
Add Metadata server sample. (GoogleCloudPlatform#485)
1 parent 9fd795b commit 706532b

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

appengine/metadata/package.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "appengine-metadata",
3+
"description": "Sample for accessing the Compute metadata server on GAE.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache-2.0",
7+
"author": "Google Inc.",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git"
11+
},
12+
"engines": {
13+
"node": ">=4"
14+
},
15+
"scripts": {
16+
"lint": "samples lint",
17+
"pretest": "npm run lint",
18+
"system-test": "samples test app",
19+
"test": "npm run system-test",
20+
"e2e-test": "samples test deploy"
21+
},
22+
"dependencies": {
23+
"express": "4.15.4",
24+
"got": "7.1.0"
25+
},
26+
"devDependencies": {
27+
"@google-cloud/nodejs-repo-tools": "1.4.17"
28+
},
29+
"cloud-repo-tools": {
30+
"test": {
31+
"app": {
32+
"msg": "External IP:",
33+
"args": [
34+
"server.js"
35+
]
36+
}
37+
},
38+
"requiresKeyFile": false,
39+
"requiresProjectId": false
40+
}
41+
}

appengine/metadata/server.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START appengine_metadata]
19+
const express = require('express');
20+
const request = require('got');
21+
22+
const app = express();
23+
app.enable('trust proxy');
24+
25+
const METADATA_NETWORK_INTERFACE_URL = 'http://metadata/computeMetadata/v1/' +
26+
'/instance/network-interfaces/0/access-configs/0/external-ip';
27+
28+
function getExternalIp () {
29+
const options = {
30+
headers: {
31+
'Metadata-Flavor': 'Google'
32+
},
33+
json: true
34+
};
35+
36+
return request(METADATA_NETWORK_INTERFACE_URL, options)
37+
.then((response) => response.body)
38+
.catch((err) => {
39+
if (err || err.statusCode !== 200) {
40+
console.log('Error while talking to metadata server, assuming localhost');
41+
return 'localhost';
42+
}
43+
return Promise.reject(err);
44+
});
45+
}
46+
47+
app.get('/', (req, res, next) => {
48+
getExternalIp()
49+
.then((externalIp) => {
50+
res.status(200).send(`External IP: ${externalIp}`).end();
51+
})
52+
.catch(next);
53+
});
54+
55+
const PORT = process.env.PORT || 8080;
56+
app.listen(PORT, () => {
57+
console.log(`App listening on port ${PORT}`);
58+
console.log('Press Ctrl+C to quit.');
59+
});
60+
// [END appengine_metadata]

circle.yml

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ deployment:
8282
- node scripts/build "appengine/errorreporting"
8383
- node scripts/build "appengine/hello-world"
8484
- node scripts/build "appengine/mailjet"
85+
- node scripts/build "appengine/metadata"
8586
- node scripts/build "appengine/static-files"
8687
- GCLOUD_STORAGE_BUCKET=docs-samples-gae-test-$(uuid); node scripts/build "appengine/storage"
8788
- node scripts/build "auth"

0 commit comments

Comments
 (0)