Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/googlecloudplatform/cloud-run-microservice-template-go
A template repository for a Cloud Run microservice, written in Go.
https://github.com/googlecloudplatform/cloud-run-microservice-template-go
cloudrun google-cloud
Last synced: 7 days ago
JSON representation
A template repository for a Cloud Run microservice, written in Go.
- Host: GitHub
- URL: https://github.com/googlecloudplatform/cloud-run-microservice-template-go
- Owner: GoogleCloudPlatform
- License: apache-2.0
- Created: 2021-06-04T17:32:58.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-12T05:05:36.000Z (27 days ago)
- Last Synced: 2024-12-18T08:40:01.371Z (21 days ago)
- Topics: cloudrun, google-cloud
- Language: Go
- Homepage:
- Size: 246 KB
- Stars: 68
- Watchers: 20
- Forks: 18
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Cloud Run Template Microservice
A template repository for a Cloud Run microservice, written in Go.
[![Run on Google Cloud](https://deploy.cloud.run/button.svg)](https://deploy.cloud.run)
## Prerequisite
* Enable the Cloud Run API via the [console](https://console.cloud.google.com/apis/library/run.googleapis.com?_ga=2.124941642.1555267850.1615248624-203055525.1615245957) or CLI:
```bash
gcloud services enable run.googleapis.com
```## Features
* **gorilla/mux**: A request router and dispatcher
* **Buildpack support** Tooling to build production-ready container images from source code and without a Dockerfile
* **Dockerfile**: Container build instructions
* **SIGTERM handler**: Catch termination signal for cleanup before Cloud Run stops the container
* **Service metadata**: Access service metadata, project Id and region, at runtime
* **Structured logging w/ Log Correlation** JSON formatted logger, parsable by Cloud Logging, with [automatic correlation of container logs to a request log](https://cloud.google.com/run/docs/logging#correlate-logs).
* **Unit and System tests** Basic unit and system tests setup for the microservice## Local Development
### Cloud Code
This template works with [Cloud Code](https://cloud.google.com/code), an IDE extension
to let you rapidly iterate, debug, and run code on Kubernetes and Cloud Run.Learn how to use Cloud Code for:
* Local development - [VSCode](https://cloud.google.com/code/docs/vscode/developing-a-cloud-run-service), [IntelliJ](https://cloud.google.com/code/docs/intellij/developing-a-cloud-run-service)
* Local debugging - [VSCode](https://cloud.google.com/code/docs/vscode/debugging-a-cloud-run-service), [IntelliJ](https://cloud.google.com/code/docs/intellij/debugging-a-cloud-run-service)
* Deploying a Cloud Run service - [VSCode](https://cloud.google.com/code/docs/vscode/deploying-a-cloud-run-service), [IntelliJ](https://cloud.google.com/code/docs/intellij/deploying-a-cloud-run-service)
* Creating a new application from a custom template (`.template/templates.json` allows for use as an app template) - [VSCode](https://cloud.google.com/code/docs/vscode/create-app-from-custom-template), [IntelliJ](https://cloud.google.com/code/docs/intellij/create-app-from-custom-template)### CLI tooling
#### Local development
1. Set Project Id:
```bash
export GOOGLE_CLOUD_PROJECT=
```2. Build and Start the server:
```bash
go build -o server && ./server
```#### Deploying a Cloud Run service
1. Set Project Id:
```bash
export GOOGLE_CLOUD_PROJECT=
```1. Enable the Artifact Registry API:
```bash
gcloud services enable artifactregistry.googleapis.com
```1. Create an Artifact Registry repo:
```bash
export REPOSITORY="samples"
export REGION=us-central1
gcloud artifacts repositories create $REPOSITORY --location $REGION --repository-format "docker"
```
2. Use the gcloud credential helper to authorize Docker to push to your
Container Registry:```bash
gcloud auth configure-docker
```3. Build and push the container using docker:
```bash
gcloud builds submit --pack image=us-central1-docker.pkg.dev/$GOOGLE_CLOUD_PROJECT/samples/microservice-template:manual
```4. Deploy to Cloud Run:
```bash
gcloud run deploy microservice-template \
--image us-central1-docker.pkg.dev/$GOOGLE_CLOUD_PROJECT/samples/microservice-template:manual
```### Run sample tests
1. [Pass credentials via `GOOGLE_APPLICATION_CREDENTIALS` env var](https://cloud.google.com/docs/authentication/production#passing_variable):
```bash
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
```2. Set Project Id:
```bash
export GOOGLE_CLOUD_PROJECT=
```3. Run unit tests
```bash
go test ./...
```4. Run system tests
```bash
gcloud builds submit \
--config advance.cloudbuild.yaml \
--substitutions=COMMIT_SHA=manual,REPO_NAME=manual
```The Cloud Build configuration file will build and deploy the containerized
service to Cloud Run, run tests, then clean up testing resources. This
configuration restricts public access to the test service. Therefore,
service accounts need to have the permission to issue ID tokens for request
authorization:
* Enable Cloud Run, Cloud Build, Artifact Registry, and IAM APIs:
```bash
gcloud services enable run.googleapis.com cloudbuild.googleapis.com iamcredentials.googleapis.com artifactregistry.googleapis.com
```* Set environment variables.
```bash
export PROJECT_ID="$(gcloud config get-value project)"
export PROJECT_NUMBER="$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')"
```* Create an Artifact Registry repo (or use another already created repo):
```bash
export REPOSITORY="samples"
export REGION=us-central1
gcloud artifacts repositories create $REPOSITORY --location $REGION --repository-format "docker"
```
* Create service account `token-creator` with `Service Account Token Creator` and `Cloud Run Invoker` roles.```bash
gcloud iam service-accounts create token-creatorgcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:token-creator@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountTokenCreator"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:token-creator@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/run.invoker"
```* Add `Service Account Token Creator` role to the Cloud Build service account.
```bash
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:[email protected]" \
--role="roles/iam.serviceAccountTokenCreator"
```## Maintenance & Support
This repo performs basic periodic testing for maintenance. Please use the issue tracker for bug reports, features requests and submitting pull requests.
## Contributions
Please see the [contributing guidelines](CONTRIBUTING.md)
## License
This library is licensed under Apache 2.0. Full license text is available in [LICENSE](LICENSE).