Skip to content

Commit

Permalink
Reorganize rest_server template with new layout and subfolder convent…
Browse files Browse the repository at this point in the history
…ion (#180)

Addresses #173, also reformats
subdirectory contents in accordance to
#166.

Template structure:
```
$ tree -all
.
├── contents
│   ├── go.mod
│   ├── go.sum
│   ├── main.go
│   ├── main_test.go
│   └── README.md
├── deployments
│   ├── config
│   │   └── autopush-rest-server.env
│   ├── contents
│   │   └── Dockerfile
│   ├── README.md
│   ├── spec.yaml
│   └── workflows
│       ├── ci-rest-server.yaml
│       └── deploy-rest-server.yaml
├── README.md
└── spec.yaml

```
Rendered template structure:
```
$ tree -all
.
├── Dockerfile
├── .github
│   ├── config
│   │   └── autopush-rest-server.env
│   └── workflows
│       ├── ci-rest-server.yaml
│       └── deploy-rest-server.yaml
├── go.mod
├── go.sum
├── main.go
├── main_test.go
└── README.md
```
  • Loading branch information
kevya-google authored and chloechien committed Sep 12, 2023
1 parent 26c81b3 commit 9241a64
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 75 deletions.
41 changes: 7 additions & 34 deletions t/rest_server/README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,16 @@
# Example: REST server
# Template: REST server

Simple HTTP/JSON REST server implemented in Go, using go-chi for HTTP routing.
Template for a simple HTTP/JSON REST server implemented in Go, using go-chi for HTTP routing.

To run this, cd to the root of this git repo, then run these steps:
How to render this template:

1. cd into an empty directory
1. [Install the abc binary](https://github.com/abcxyz/abc#installation).

```shell
$ mkdir ~/template_tmp
$ cd ~/template_tmp
```

1. Install the `abc` binary

```shell
$ go install github.com/abcxyz/abc/cmd/abc@latest
$ abc --help
```

This only works if you have go installed (https://go.dev/doc/install) and have the Go binary directory in your $PATH (try PATH=$PATH:~/go/bin).

1. Execute the template defined in the `t` directory.
This will output a file named `main.go` in your working directory containing
the transformed program.
1. Render via:

```shell
$ abc templates render github.com/abcxyz/abc.git//t/rest_server
```

1. Run the transformed program:

```shell
$ go run .
[yyyy/mm/dd hh:mm:ss] starting server on 8080
```
1. Follow the steps in the rendered README.md to run the server.

1. In a separate shell, run:

```shell
$ curl localhost:8080
{"message":"hello world"}
```
1. Optionally, render the CI/CD workflows as well (see subfolder README for more details).
17 changes: 17 additions & 0 deletions t/rest_server/contents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Simple REST server

This is a simple HTTP/JSON REST server implemented in Go, using go-chi for HTTP routing.

How to run this server:

1. Run the server:

```shell
$ go run .
[yyyy/mm/dd hh:mm:ss] starting server on 8080
```
1. In a separate shell, run:
```shell
$ curl localhost:8080
{"message":"hello world"}
```
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions t/rest_server/deployments/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# REST server workflows template

This directory contains the CI/CD GitHub Action workflows for the REST server template.

$ abc templates render -input="automation_service_account=[automation_service_account]" \
-input="wif_provider=[wif_provider]" \
-input="ar_repository=[ar_repository]" \
-input="ar_location=[ar_location]" \
-input="cr_service=[cr_service]" \
-input="region=[region]" \
-input="project_id=[project_id]" \
github.com/abcxyz/abc.git//t/rest_server/workflows
File renamed without changes.
File renamed without changes.
68 changes: 68 additions & 0 deletions t/rest_server/deployments/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2023 The Authors (see AUTHORS file)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: 'cli.abcxyz.dev/v1alpha1'
kind: 'Template'
desc: 'REST server workflows template'
inputs:
- name: 'automation_service_account'
desc:
'Automation Service Account (ex:
[account_name]@[project_id].iam.gserviceaccount.com)'
rules:
- rule: 'gcp_matches_service_account(automation_service_account)'
message: 'Service account should match format:
[account_name]@[project_id].iam.gserviceaccount.com)'
- name: 'wif_provider'
desc:
'Workload Identity Federation Provider (ex:
projects/[project_id]/locations/global/workloadIdentityPools/[WIF_pool_name]/providers/[provider_name])'
- name: 'ar_repository'
desc: 'Artifact Registry Repository (ex: ci-images)'
- name: 'ar_location'
desc: 'Artifact Registry Location (ex: us-docker.pkg.dev)'
- name: 'cr_service'
desc: 'Cloud Run Service (ex: hello-jupiter-api)'
- name: 'region'
desc: 'Cloud Run Region (ex: us-west1)'
- name: 'project_id'
desc: 'GCP Project ID'
rules:
- rule: 'gcp_matches_project_id(project_id)'
message: 'Must be a GCP project ID'
steps:
- desc: 'Include workflow, deployment files'
action: 'include'
params:
paths: ['workflows', 'config', 'contents']
as: ['.github/workflows', '.github/config', '.']
- desc: 'Replace sentinel values with real input values'
action: 'string_replace'
params:
paths: ['.']
replacements:
- to_replace: 'AUTOMATION_SERVICE_ACCOUNT=[automation_service_account]'
with: 'AUTOMATION_SERVICE_ACCOUNT={{.automation_service_account}}'
- to_replace: 'WIF_PROVIDER=[wif_provider]'
with: 'WIF_PROVIDER={{.wif_provider}}'
- to_replace: 'AR_REPOSITORY=[ar_repository]'
with: 'AR_REPOSITORY={{.ar_repository}}'
- to_replace: 'AR_LOCATION=[ar_location]'
with: 'AR_LOCATION={{.ar_location}}'
- to_replace: 'CR_SERVICE=[cr_service]'
with: 'CR_SERVICE={{.cr_service}}'
- to_replace: 'REGION=[region]'
with: 'REGION={{.region}}'
- to_replace: 'PROJECT_ID=[project_id]'
with: 'PROJECT_ID={{.project_id}}'
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'ci'
name: 'ci rest server'

on:
push:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

name: 'deploy'
name: 'deploy rest server'
on:
workflow_dispatch:
inputs:
Expand All @@ -32,7 +32,7 @@ concurrency:
group: '${{ github.workflow }}-${{ github.head_ref || github.ref }}'
cancel-in-progress: true
# Define all env variables in the bootstrap files under
# .github/config/{{ environment }}.env
# .github/config/{{ environment }}-rest-server.env
# More environments are expected to be added in the future.
jobs:
image-release:
Expand All @@ -47,7 +47,7 @@ jobs:
- name: 'Load variables from .env file'
uses: 'abcxyz/pkg/.github/actions/override-workflow-variables@ca5c712fad5863b8f8fd40b2a8e220f8b41bde01' # ratchet:abcxyz/pkg/.github/actions/override-workflow-variables@main
with:
env_file: '.github/config/${{inputs.deployment_environment}}.env'
env_file: '.github/config/${{inputs.deployment_environment}}-rest-server.env'
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@e8df18b60c5dd38ba618c121b779307266153fbf' # ratchet:google-github-actions/auth@v1
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
- name: 'Load variables from .env file'
uses: 'abcxyz/pkg/.github/actions/override-workflow-variables@ca5c712fad5863b8f8fd40b2a8e220f8b41bde01' # ratchet:abcxyz/pkg/.github/actions/override-workflow-variables@main
with:
env_file: '.github/config/${{inputs.deployment_environment}}.env'
env_file: '.github/config/${{inputs.deployment_environment}}-rest-server.env'
- name: 'Google Auth'
id: 'auth'
uses: 'google-github-actions/auth@e8df18b60c5dd38ba618c121b779307266153fbf' # ratchet:google-github-actions/auth@v1
Expand Down
39 changes: 3 additions & 36 deletions t/rest_server/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,10 @@

apiVersion: 'cli.abcxyz.dev/v1alpha1'
kind: 'Template'
desc: 'A template for a simple HTTP/JSON REST server.'
inputs:
- name: 'automation_service_account'
desc: 'Automation Service Account (ex: [account_name]@[project_id].iam.gserviceaccount.com)'
- name: 'wif_provider'
desc: 'Workload Identity Federation Provider (ex: projects/[project_id]/locations/global/workloadIdentityPools/[WIF_pool_name]/providers/[provider_name])'
- name: 'ar_repository'
desc: 'Artifact Registry Repository (ex: ci-images)'
- name: 'ar_location'
desc: 'Artifact Registry Location (ex: us-docker.pkg.dev)'
- name: 'cr_service'
desc: 'Cloud Run Service (ex: hello-jupiter-api)'
- name: 'region'
desc: 'Cloud Run Region (ex: us-west1)'
- name: 'project_id'
desc: 'GCP Project ID'
desc: 'A template for a simple HTTP/JSON REST server'
steps:
- desc: 'Include some files and directories'
action: 'include'
params:
paths: ['.']
- desc: 'Replace sentinel values with real input values'
action: 'string_replace'
params:
paths: ['.github/config']
replacements:
- to_replace: 'AUTOMATION_SERVICE_ACCOUNT=[automation_service_account]'
with: 'AUTOMATION_SERVICE_ACCOUNT={{.automation_service_account}}'
- to_replace: 'WIF_PROVIDER=[wif_provider]'
with: 'WIF_PROVIDER={{.wif_provider}}'
- to_replace: 'AR_REPOSITORY=[ar_repository]'
with: 'AR_REPOSITORY={{.ar_repository}}'
- to_replace: 'AR_LOCATION=[ar_location]'
with: 'AR_LOCATION={{.ar_location}}'
- to_replace: 'CR_SERVICE=[cr_service]'
with: 'CR_SERVICE={{.cr_service}}'
- to_replace: 'REGION=[region]'
with: 'REGION={{.region}}'
- to_replace: 'PROJECT_ID=[project_id]'
with: 'PROJECT_ID={{.project_id}}'
paths: ['contents']
as: ['.']

0 comments on commit 9241a64

Please sign in to comment.