forked from lavishsheth/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Getting Started with API Gateway: Challenge Lab
130 lines (95 loc) · 2.72 KB
/
Getting Started with API Gateway: Challenge Lab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
mkdir btecky
cd btecky
cat > index.js <<EOF
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (req, res) => {
let message = req.query.message || req.body.message || 'Hello World!';
res.status(200).send(message);
};
EOF
cat > package.json <<EOF
{
"name": "sample-http",
"version": "0.0.1"
}
EOF
gcloud functions deploy GCFunction \
--runtime=nodejs18 \
--trigger-http \
--allow-unauthenticated \
--entry-point=helloWorld \
--region=us-central1 \
--max-instances 5 \
--source=./
gcloud services enable apigateway.googleapis.com
gcloud pubsub topics create demo-topic
cat > index.js <<EOF
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
const {PubSub} = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('demo-topic');
exports.helloWorld = (req, res) => {
// Send a message to the topic
topic.publishMessage({data: Buffer.from('Hello from Cloud Functions!')});
res.status(200).send("Message sent to Topic demo-topic!");
};
EOF
cat > package.json <<EOF
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"@google-cloud/pubsub": "^3.4.1"
}
}
EOF
gcloud functions deploy GCFunction \
--runtime=nodejs18 \
--trigger-http \
--allow-unauthenticated \
--entry-point=helloWorld \
--region=us-central1 \
--max-instances 5 \
--source=./
cat > openapispec.yaml <<EOF
swagger: '2.0'
info:
title: GCFunction API
description: Sample API on API Gateway with a Google Cloud Functions backend
version: 1.0.0
schemes:
- https
produces:
- application/json
paths:
/GCFunction:
get:
summary: gcfunction
operationId: gcfunction
x-google-backend:
address: https://us-central1-$DEVSHELL_PROJECT_ID.cloudfunctions.net/GCFunction
responses:
'200':
description: A successful response
schema:
type: string
EOF
PROJECT_NUMBER=$(gcloud projects describe $DEVSHELL_PROJECT_ID --format="value(projectNumber)")
export API_ID="gcfunction-api-$(cat /dev/urandom | tr -dc 'a-z' | fold -w ${1:-8} | head -n 1)"
gcloud api-gateway apis create $API_ID --project=$DEVSHELL_PROJECT_ID
gcloud api-gateway api-configs create gcfunction-api \
--api=$API_ID --openapi-spec=openapispec.yaml \
--project=$DEVSHELL_PROJECT_ID --backend-auth-service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com
gcloud api-gateway gateways create gcfunction-api \
--api=$API_ID --api-config=gcfunction-api \
--location=us-central1 --project=$DEVSHELL_PROJECT_ID