Skip to content

Commit 5225a8f

Browse files
kdeusjmdobry
authored andcommitted
Copied Endpoints Echo sample from /appengine/endpoints (#247)
* Copied Endpoints Echo sample from /appengine/endpoints * Added gke.yaml * Renamed gke.yaml to container-engine.yaml * Cleaned up readme, added Dockerfile * Added region tags for docs * Switch to http container-engine.yaml * Added trailing newline * Changed to point at Node.js-specific image
1 parent 30197c8 commit 5225a8f

File tree

8 files changed

+386
-0
lines changed

8 files changed

+386
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM gcr.io/google_appengine/nodejs
2+
3+
ADD . /app
4+
WORKDIR /app
5+
6+
RUN npm install
7+
ENV PORT=8081
8+
ENTRYPOINT ["npm", "start"]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Google Cloud Endpoints sample for Node.js
2+
3+
This sample demonstrates how to use Google Cloud Endpoints using Node.js.
4+
5+
For a complete walkthrough showing how to run this sample in different
6+
environments, see the
7+
[Google Cloud Endpoints Quickstarts](https://cloud.google.com/endpoints/docs/quickstarts).
8+
9+
## Running locally
10+
11+
Refer to the [appengine/README.md](../../appengine/README.md) file for
12+
instructions on running locally.
13+
14+
## Send an echo request
15+
16+
Choose your local or production server:
17+
18+
```
19+
# If you're running locally, you won't need an API key.
20+
$ export ENDPOINTS_HOST=http://localhost:8080
21+
22+
$ export ENDPOINTS_HOST=https://PROJECT-ID.appspot.com
23+
$ export ENDPOINTS_KEY=AIza...
24+
```
25+
26+
Send the request:
27+
28+
```
29+
$ curl -vv -d '{"message":"foo"}' -H 'Content-Type: application/json' "${ENDPOINTS_HOST}/echo?key=${ENDPOINTS_KEY}"
30+
```
31+
32+
If you're running locally, you won't need an API key.
33+
34+
## Sending authenticated requests
35+
36+
No Node.js client is written yet, but you can try the Python client found [here][python-client].
37+
It will send authenticated JWT requests using a Google Cloud service account, or using a three-legged OAuth flow.
38+
39+
[python-client]: https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/endpoints/getting-started

endpoints/getting-started/app.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2015-2016, Google, Inc.
2+
//
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+
// [START app]
16+
'use strict';
17+
18+
// [START setup]
19+
var express = require('express');
20+
var bodyParser = require('body-parser');
21+
22+
var app = express();
23+
app.use(bodyParser.json());
24+
// [END setup]
25+
26+
app.post('/echo', function (req, res) {
27+
res.status(200).json({ message: req.body.message });
28+
});
29+
30+
function authInfoHandler (req, res) {
31+
var authUser = { id: 'anonymous' };
32+
var encodedInfo = req.get('X-Endpoint-API-UserInfo');
33+
if (encodedInfo) {
34+
authUser = JSON.parse(new Buffer(encodedInfo, 'base64'));
35+
}
36+
res.status(200).json(authUser);
37+
}
38+
39+
app.get('/auth/info/googlejwt', authInfoHandler);
40+
app.get('/auth/info/googleidtoken', authInfoHandler);
41+
42+
// [START listen]
43+
var PORT = process.env.PORT || 8080;
44+
app.listen(PORT, function () {
45+
console.log('App listening on port %s', PORT);
46+
console.log('Press Ctrl+C to quit.');
47+
});
48+
// [END listen]
49+
// [END app]
50+
51+
module.exports = app;

endpoints/getting-started/app.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2015-2016, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
runtime: nodejs
15+
vm: true
16+
17+
beta_settings:
18+
# Enable Google Cloud Endpoints API management.
19+
use_endpoints_api_management: true
20+
# Specify the Swagger API specification.
21+
endpoints_swagger_spec_file: swagger.yaml
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
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+
apiVersion: v1
16+
kind: Service
17+
metadata:
18+
name: esp-echo
19+
spec:
20+
ports:
21+
- port: 80
22+
targetPort: 8080
23+
protocol: TCP
24+
name: http
25+
selector:
26+
app: esp-echo
27+
type: LoadBalancer
28+
---
29+
apiVersion: extensions/v1beta1
30+
kind: Deployment
31+
metadata:
32+
name: esp-echo
33+
spec:
34+
replicas: 1
35+
template:
36+
metadata:
37+
labels:
38+
app: esp-echo
39+
spec:
40+
containers:
41+
# [START esp]
42+
- name: esp
43+
image: b.gcr.io/endpoints/endpoints-runtime:0.3
44+
args: [
45+
"-p", "8080",
46+
"-a", "127.0.0.1:8081",
47+
"-s", "SERVICE_NAME",
48+
"-v", "SERVICE_VERSION",
49+
]
50+
# [END esp]
51+
ports:
52+
- containerPort: 8080
53+
- name: echo
54+
image: gcr.io/google-samples/node-echo:1.0
55+
ports:
56+
- containerPort: 8081
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "appengine-endpoints",
3+
"description": "Endpoints Node.js sample for Google App Engine",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"engines": {
9+
"node": "~4.2"
10+
},
11+
"scripts": {
12+
"start": "node app.js",
13+
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
14+
},
15+
"dependencies": {
16+
"express": "^4.13.4",
17+
"body-parser": "^1.15.0"
18+
},
19+
"devDependencies": {
20+
"mocha": "^2.5.3"
21+
}
22+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# [START swagger]
2+
swagger: "2.0"
3+
info:
4+
description: "A simple Google Cloud Endpoints API example."
5+
title: "Endpoints Example"
6+
version: "1.0.0"
7+
host: "YOUR-PROJECT-ID.appspot.com"
8+
# [END swagger]
9+
basePath: "/"
10+
consumes:
11+
- "application/json"
12+
produces:
13+
- "application/json"
14+
schemes:
15+
- "https"
16+
paths:
17+
"/echo":
18+
post:
19+
description: "Echo back a given message."
20+
operationId: "echo"
21+
produces:
22+
- "application/json"
23+
responses:
24+
200:
25+
description: "Echo"
26+
schema:
27+
$ref: "#/definitions/echoMessage"
28+
parameters:
29+
- description: "Message to echo"
30+
in: body
31+
name: message
32+
required: true
33+
schema:
34+
$ref: "#/definitions/echoMessage"
35+
"/auth/info/googlejwt":
36+
get:
37+
description: "Returns the requests' authentication information."
38+
operationId: "auth_info_google_jwt"
39+
produces:
40+
- "application/json"
41+
responses:
42+
200:
43+
description: "Authenication info."
44+
schema:
45+
$ref: "#/definitions/authInfoResponse"
46+
x-security:
47+
- google_jwt:
48+
audiences:
49+
# This must match the "aud" field in the JWT. You can add multiple
50+
# audiences to accept JWTs from multiple clients.
51+
- "echo.endpoints.sample.google.com"
52+
"/auth/info/googleidtoken":
53+
get:
54+
description: "Returns the requests' authentication information."
55+
operationId: "authInfoGoogleIdToken"
56+
produces:
57+
- "application/json"
58+
responses:
59+
200:
60+
description: "Authenication info."
61+
schema:
62+
$ref: "#/definitions/authInfoResponse"
63+
x-security:
64+
- google_id_token:
65+
audiences:
66+
# Your OAuth2 client's Client ID must be added here. You can add
67+
# multiple client IDs to accept tokens from multiple clients.
68+
- "YOUR-CLIENT-ID"
69+
definitions:
70+
echoMessage:
71+
properties:
72+
message:
73+
type: "string"
74+
authInfoResponse:
75+
properties:
76+
id:
77+
type: "string"
78+
email:
79+
type: "string"
80+
# This section requires all requests to any path to require an API key.
81+
security:
82+
- api_key: []
83+
securityDefinitions:
84+
# This section configures basic authentication with an API key.
85+
api_key:
86+
type: "apiKey"
87+
name: "key"
88+
in: "query"
89+
# This section configures authentication using Google API Service Accounts
90+
# to sign a json web token. This is mostly used for server-to-server
91+
# communication.
92+
google_jwt:
93+
authorizationUrl: ""
94+
flow: "implicit"
95+
type: "oauth2"
96+
# This must match the 'iss' field in the JWT.
97+
x-issuer: "jwt-client.endpoints.sample.google.com"
98+
# Update this with your service account's email address.
99+
x-jwks_uri: "https://www.googleapis.com/service_accounts/v1/jwk/YOUR-SERVICE-ACCOUNT-EMAIL"
100+
# This section configures authentication using Google OAuth2 ID Tokens.
101+
# ID Tokens can be obtained using OAuth2 clients, and can be used to access
102+
# your API on behalf of a particular user.
103+
google_id_token:
104+
authorizationUrl: ""
105+
flow: "implicit"
106+
type: "oauth2"
107+
x-issuer: "accounts.google.com"
108+
x-jwks_uri: "https://www.googleapis.com/oauth2/v1/certs"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var express = require('express');
17+
var path = require('path');
18+
var proxyquire = require('proxyquire').noPreserveCache();
19+
var request = require('supertest');
20+
21+
var SAMPLE_PATH = path.join(__dirname, '../app.js');
22+
23+
function getSample () {
24+
var testApp = express();
25+
sinon.stub(testApp, 'listen').callsArg(1);
26+
var expressMock = sinon.stub().returns(testApp);
27+
28+
var app = proxyquire(SAMPLE_PATH, {
29+
express: expressMock
30+
});
31+
return {
32+
app: app,
33+
mocks: {
34+
express: expressMock
35+
}
36+
};
37+
}
38+
39+
describe('appengine/endpoints/app.js', function () {
40+
var sample;
41+
42+
beforeEach(function () {
43+
sample = getSample();
44+
45+
assert(sample.mocks.express.calledOnce);
46+
assert(sample.app.listen.calledOnce);
47+
assert.equal(sample.app.listen.firstCall.args[0], process.env.PORT || 8080);
48+
});
49+
50+
it('should echo a message', function (done) {
51+
request(sample.app)
52+
.post('/echo')
53+
.send({ message: 'foo' })
54+
.expect(200)
55+
.expect(function (response) {
56+
assert.equal(response.body.message, 'foo');
57+
})
58+
.end(done);
59+
});
60+
61+
it('should try to parse encoded info', function (done) {
62+
request(sample.app)
63+
.get('/auth/info/googlejwt')
64+
.expect(200)
65+
.expect(function (response) {
66+
assert.deepEqual(response.body, { id: 'anonymous' });
67+
})
68+
.end(done);
69+
});
70+
71+
it('should successfully parse encoded info', function (done) {
72+
request(sample.app)
73+
.get('/auth/info/googlejwt')
74+
.set('X-Endpoint-API-UserInfo', new Buffer(JSON.stringify({ id: 'foo' })).toString('base64'))
75+
.expect(200)
76+
.expect(function (response) {
77+
assert.deepEqual(response.body, { id: 'foo' });
78+
})
79+
.end(done);
80+
});
81+
});

0 commit comments

Comments
 (0)