├── app.json ├── go.mod ├── Dockerfile ├── README.md ├── cloudbuild.yaml ├── deployment.yaml.tmpl ├── types.go ├── main.go └── .github └── workflows └── deploy.yml /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cloudbowl-samples-go" 3 | } 4 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | // +heroku goVersion 1.14 2 | module github.com/GoogleCloudPlatform/cloudbowl-microservice-game/samples/go 3 | 4 | go 1.14 5 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.13-alpine AS build 2 | WORKDIR /src/app 3 | COPY . . 4 | RUN go mod download 5 | RUN go build -o /bin/app 6 | 7 | FROM alpine 8 | COPY --from=build /bin/app /bin/app 9 | ENTRYPOINT ["/bin/app"] 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CloudBowl - Sample - Go 2 | --------------------------------- 3 | 4 | To make changes, edit the `play` method in [`main.go`](./main.go) 5 | 6 | To run the server locally: 7 | 8 | ``` 9 | go run . 10 | ``` 11 | 12 | 13 | -------------------------------------------------------------------------------- /cloudbuild.yaml: -------------------------------------------------------------------------------- 1 | steps: 2 | - name: 'gcr.io/k8s-skaffold/pack' 3 | entrypoint: 'pack' 4 | args: ['build', '--builder=gcr.io/buildpacks/builder:v1', '--path', 'samples/go', '--publish', 'gcr.io/$PROJECT_ID/cloudbowl-samples-go:$COMMIT_SHA'] 5 | 6 | - name: 'gcr.io/cloud-builders/gcloud' 7 | args: ['run', 'deploy', '--image=gcr.io/$PROJECT_ID/cloudbowl-samples-go:$COMMIT_SHA', '--platform=managed', '--project=$PROJECT_ID', '--region=us-central1', '--allow-unauthenticated', '--memory=256Mi', 'cloudbowl-samples-go'] 8 | -------------------------------------------------------------------------------- /deployment.yaml.tmpl: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: $SAMPLE-deployment 5 | labels: 6 | app: $SAMPLE-app 7 | spec: 8 | replicas: 1 9 | selector: 10 | matchLabels: 11 | app: $SAMPLE-app 12 | template: 13 | metadata: 14 | labels: 15 | app: $SAMPLE-app 16 | spec: 17 | containers: 18 | - name: $REPO_NAME 19 | image: gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA 20 | imagePullPolicy: IfNotPresent 21 | ports: 22 | - containerPort: 8080 23 | -------------------------------------------------------------------------------- /types.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | type ArenaUpdate struct { 4 | Links struct { 5 | Self struct { 6 | Href string `json:"href"` 7 | } `json:"self"` 8 | } `json:"_links"` 9 | Arena struct { 10 | Dimensions []int `json:"dims"` 11 | State map[string]PlayerState `json:"state"` 12 | } `json:"arena"` 13 | } 14 | 15 | type PlayerState struct { 16 | X int `json:"x"` 17 | Y int `json:"y"` 18 | Direction string `json:"direction"` 19 | WasHit bool `json:"wasHit"` 20 | Score int `json:"score"` 21 | } 22 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "log" 7 | rand2 "math/rand" 8 | "net/http" 9 | "os" 10 | ) 11 | 12 | func main() { 13 | port := "8080" 14 | if v := os.Getenv("PORT"); v != "" { 15 | port = v 16 | } 17 | http.HandleFunc("/", handler) 18 | 19 | log.Printf("starting server on port :%s", port) 20 | err := http.ListenAndServe(":"+port, nil) 21 | log.Fatalf("http listen error: %v", err) 22 | } 23 | 24 | func handler(w http.ResponseWriter, req *http.Request) { 25 | if req.Method == http.MethodGet { 26 | fmt.Fprint(w, "Let the battle begin!") 27 | return 28 | } 29 | 30 | var v ArenaUpdate 31 | defer req.Body.Close() 32 | d := json.NewDecoder(req.Body) 33 | d.DisallowUnknownFields() 34 | if err := d.Decode(&v); err != nil { 35 | log.Printf("WARN: failed to decode ArenaUpdate in response body: %v", err) 36 | w.WriteHeader(http.StatusInternalServerError) 37 | return 38 | } 39 | 40 | resp := play(v) 41 | fmt.Fprint(w, resp) 42 | } 43 | 44 | func play(input ArenaUpdate) (response string) { 45 | log.Printf("IN: %#v", input) 46 | 47 | commands := []string{"F", "R", "L", "T"} 48 | rand := rand2.Intn(4) 49 | 50 | // TODO add your implementation here to replace the random response 51 | return commands[rand] 52 | } 53 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - main 5 | 6 | env: 7 | PROJECT_ID: ${{ secrets.GKE_PROJECT }} 8 | GKE_CLUSTER: ${{ secrets.GKE_CLUSTER }} 9 | GKE_REGION: ${{ secrets.GKE_REGION }} 10 | SAMPLE: ${{ secrets.SAMPLE }} 11 | REPO_NAME: ${{ github.event.repository.name }} 12 | COMMIT_SHA: ${{ github.sha }} 13 | 14 | jobs: 15 | deploy: 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | 21 | - uses: google-github-actions/auth@v0.4.0 22 | with: 23 | credentials_json: ${{ secrets.GKE_SA_KEY }} 24 | 25 | - uses: google-github-actions/setup-gcloud@v0.3.0 26 | with: 27 | project_id: ${{ secrets.GKE_PROJECT }} 28 | 29 | - run: |- 30 | gcloud auth configure-docker -q 31 | gcloud container clusters get-credentials "$GKE_CLUSTER" --region "$GKE_REGION" 32 | 33 | - uses: buildpacks/github-actions/setup-pack@v4.1.0 34 | 35 | - run: |- 36 | pack build --builder=gcr.io/buildpacks/builder:v1 --publish \ 37 | gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA 38 | 39 | - uses: nowactions/envsubst@v1 40 | with: 41 | input: ./deployment.yaml.tmpl 42 | output: ./deployment.yaml 43 | 44 | - run: |- 45 | kubectl apply -f deployment.yaml 46 | --------------------------------------------------------------------------------