https://github.com/gabihodoroaga/http-grpc-websocket
How to run HTTP, gRPC, websocket all on the same port on Google Cloud Run
https://github.com/gabihodoroaga/http-grpc-websocket
cloud-run gcp google-cloud-platform grpc https websocket
Last synced: 2 months ago
JSON representation
How to run HTTP, gRPC, websocket all on the same port on Google Cloud Run
- Host: GitHub
- URL: https://github.com/gabihodoroaga/http-grpc-websocket
- Owner: gabihodoroaga
- Created: 2022-08-07T08:09:19.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-08-09T17:32:03.000Z (almost 4 years ago)
- Last Synced: 2024-11-14T20:07:53.636Z (over 1 year ago)
- Topics: cloud-run, gcp, google-cloud-platform, grpc, https, websocket
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# http-grpc-websocket
A demo project about how you can run HTTP, gRPC and websocket in Cloud Run using
a single port. You can follow this tutorial [HTTP, gRPC, and websocket on Google Cloud Run](https://hodo.dev/posts/post-41-gcp-cloudrun-grpc-http-ws/) to find out more.
## Run and test locally
Run
```bash
go run main.go
```
On a separate terminal,
test HTTP
```bash
curl http://localhost:8080/ping
```
test gRPC
```bash
go run clients/grpc/main.go
```
test Websocket
```bash
go run clients/ws/main.go
```
## Run and test on Cloud Run with authentication
Set your variables
```bash
PROJECT_ID=$(gcloud config list project --format='value(core.project)')
REGION=us-central1
```
Create a service account
```bash
gcloud iam service-accounts create demo-grpc-sa \
--display-name "Demo service account for gRPC on Cloud Run"
```
Create a new key for this service account
```bash
gcloud iam service-accounts keys create key.json \
--iam-account demo-grpc-sa@${PROJECT_ID}.iam.gserviceaccount.com
```
Build the image
```bash
gcloud builds submit --tag gcr.io/$PROJECT_ID/grpcwebapp --project $PROJECT_ID .
```
Deploy the service
```bash
gcloud run deploy grpcwebapp \
--image gcr.io/$PROJECT_ID/grpcwebapp \
--set-env-vars=AUTH_SERVICE_ACCOUNTS="demo-grpc-sa@${PROJECT_ID}.iam.gserviceaccount.com",AUTH_AUDIENCE=webapp \
--allow-unauthenticated \
--timeout=10m \
--project $PROJECT_ID \
--region $REGION
```
Get the public URL
```bash
SERVICE_URL=$(gcloud run services describe grpcwebapp --platform managed --region $REGION --format 'value(status.url)')
echo $SERVICE_URL
SERVICE_HOST=$(echo "$SERVICE_URL" | awk -F/ '{print $3}')
echo $SERVICE_HOST
```
test HTTPS
```bash
curl -v $SERVICE_URL/ping
```
test gRPC
```bash
go run clients/grpc/main.go --server "$SERVICE_HOST:443" --key key.json --insecure=false
```
test Websocket
```bash
go run clients/ws/main.go --server "wss://$SERVICE_HOST/ws" --key key.json
```
## Generate protobuffer files.
```bash
protoc \
--proto_path=grpc/proto \
--go_out=plugins=grpc:. \
./grpc/proto/*.proto
```
## References
This example was based on this post [Serving gRPC+HTTP/2 from the same Cloud Run container](https://ahmet.im/blog/grpc-http-mux-go/)
written by Ahmet Alp Balkan (https://github.com/ahmetb).