Rock, Paper,
Scissors
https://crossnative.github.io/cloudland-rps/
The Plan
Build a RESTful API forRock, Paper, Scissors
Learn more about Go and Spring
Learn cloud-native Development
#
#
#
How?
2 Sessions: Go and Spring
Collaborative programming of the API
Frontend is provided
You may workin groups
Successive fishbowl fordiscussing results
#
#
#
#
#
What's
cloud-native?
There is no cloud.
It's just someone else's computers.
Cloud native Technologies
empowerto build and run
scalable applications
in modern, dynamic environments.
Cloud Native Computing Foundation
The 12-Factor-App and Beyond
1. One Codebase, One App
2. API First
3. Dependency Management
4. Design, Build, Release, Run
5. Configuration, Credentials and
Code
6. Logs
7. Disposability
8. Backing Services
9. Environment Parity
10. Administrative Processes
11. Port Binding
12. Stateless Processes
13. Concurrency
14. Telemetry
15. Authentication and Authorization
Getting Started
Project Overview
Playervs. ComputerAPI Calls
>_ Demo
Split into Session
# Go Session
# Spring Session
Go Session
Prerequisites Go Session
Editor
Clone ,
checkout branch go-start
Go
optional: forhot reload
go install github.com/cosmtrek/air@latest
Frontend
# Visual Studio Code
# Postman
# github.com/crossnative/cloudland-rps
# Go Compiler
# air
# Node.js
First Steps in Go
1. Get up and running
2. Complete REST API
3. oradd
Make Cloud Native yourFeatures
Go Cloud Native Path
...
# Configure Port from
Environment Variable
# Handle Errors properly
# Middleware forLogging
and Recovery
# Add a Health Check
# Run in Container
# Use PostgreSQL for
Storage
# Deploy to the cloud
# Add OpenTelemetry
#
Cheatsheet Middleware forLogging
and Recovery
Using middlewares from chi routerorothers.
What does the recovery middleware do?
Bonus: How can you set a request timeout using
middleware?
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
#
#
Cheatsheet Configure Port
from Environment Variable
Using the Standard Library
Using envconfig
github.com/kelseyhightower/envconfig
func main() {
http.ListenAndServe(fmt.Sprintf(":%v", os.Getenv("PORT")), r)
}
// Application Configuration
type Config struct {
Port string `envconfig:"PORT" default:"8080"`
}
func main() {
http.ListenAndServe(fmt.Sprintf(":%v", c.Port), r)
}
Cheatsheet Handle Errors properly
Using the Standard Library
Using Problem forHTTPAPIs
Use with schneider.vip/problem
if err != nil {
http.Error(w, "Oops, that went wrong!", http.StatusInternalServerError)
return
}
rfc7807
if err != nil {
problem.New(
problem.Wrap(err),
problem.Status(http.StatusInternalServerError)
).WriteTo(w)
return
}
Cheatsheet Run in Container
# 1. RPS Builder
FROM golang as builder
WORKDIR /app
ADD . /app
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o build/rps .
# 2. RPS Container
FROM gcr.io/distroless/static
COPY --from=builder /app/build/rps /usr/bin/
EXPOSE 8080
ENTRYPOINT ["/usr/bin/rps"]
Cheatsheet Add a Health Check
See github.com/hellofresh/health-go/v5.
// Register Health Check
h, _ := health.New(health.WithChecks(
health.Config{
Name: "db",
Timeout: time.Second * 2,
SkipOnErr: false,
Check: healthPgx.New(healthPgx.Config{
DSN: config.Db,
}),
},
))
// Register Health Check Handler Function
r.Get("/health", h.HandlerFunc)
Cheatsheet Use PostgreSQL for
Storage
Sample application
# SQL Tutorial with Go
# PostgreSQL DriverPGX
# offer
Cheatsheet Cloud Deployment
Example Deployment forrender
services:
- type: web
name: backend-go
env: go
plan: free
rootDir: backend-go
buildCommand: go build -o rps .
startCommand: ./rps
Cheatsheet OpenTelemetry
Use otelchi
Spring Session
Prerequisites Spring Session
Install some IDE (e.g. or )
Install
Install Java JDK(e.g. )
Clone and checkout branch
spring-start
Frontend
(npm is included)
# IntelliJ Visual Studio Code
# Postman
# Temurin
# github.com/crossnative/cloudland-rps
# Node.js
First Steps Spring Session
1. Run local Frontend togetherwith example backend
https://backend-spring.onrender.com/api/v1
2. Build and Run Spring Backend
3. Implement REST API
4. oradd
Make Cloud Native furtherFeatures
Spring Cloud Native Path
...
# Configurable
Application Port
# Improve ErrorHandling
# Configure proper
Logging
# Health Check
# Run in Container
# Use Database for
persistence
# Deploy to the Cloud
# Add OpenTelemetry
#
Cheatsheet ErrorHandling
# Use JSON Problem Details (RFC 7807)
# Create a Http ErrorHandler
Cheatsheet Run in Container
With Buildpacks run
./mvnw spring-boot:build-image -Dspring-
boot.build-image.imageName=myorg/myapp
# Spring Official Guide
# Baeldung: Dockerizing a Spring Boot Application
Feature Ideas Rock, Paper, Scissors
Add a leaderboard with top 10 Players
Make Game realtime (e.g. WebSocket, ...)
Enhance to
Add rooms people can play many Games in
...
#
#
# Rock, Paper, Scissors, Spock, Lizard
#
#
Interesting Links
# The Twelve FactorApp
# Beyond the Twelve FactorApp
# Cloud-native entwickeln mit Go
# Building a MultiplayerGame with API
Gateway+Websockets, Go and DynamoDB
Jan Stamer
Lena Grimm
Fynn JasparStempel
jan.stamer@crossnative.com
lena.grimm@crossnative.com
fynn.jaspar.stempel@crossnative.com

CloudLand 2023: Rock, Paper, Scissors Cloud Competition - Go vs. Java

  • 1.
  • 2.
  • 3.
    The Plan Build aRESTful API forRock, Paper, Scissors Learn more about Go and Spring Learn cloud-native Development # # #
  • 4.
    How? 2 Sessions: Goand Spring Collaborative programming of the API Frontend is provided You may workin groups Successive fishbowl fordiscussing results # # # # #
  • 5.
  • 6.
    There is nocloud. It's just someone else's computers.
  • 7.
    Cloud native Technologies empowertobuild and run scalable applications in modern, dynamic environments. Cloud Native Computing Foundation
  • 8.
    The 12-Factor-App andBeyond 1. One Codebase, One App 2. API First 3. Dependency Management 4. Design, Build, Release, Run 5. Configuration, Credentials and Code 6. Logs 7. Disposability 8. Backing Services 9. Environment Parity 10. Administrative Processes 11. Port Binding 12. Stateless Processes 13. Concurrency 14. Telemetry 15. Authentication and Authorization
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    Split into Session #Go Session # Spring Session
  • 14.
  • 15.
    Prerequisites Go Session Editor Clone, checkout branch go-start Go optional: forhot reload go install github.com/cosmtrek/air@latest Frontend # Visual Studio Code # Postman # github.com/crossnative/cloudland-rps # Go Compiler # air # Node.js
  • 16.
    First Steps inGo 1. Get up and running 2. Complete REST API 3. oradd Make Cloud Native yourFeatures
  • 17.
    Go Cloud NativePath ... # Configure Port from Environment Variable # Handle Errors properly # Middleware forLogging and Recovery # Add a Health Check # Run in Container # Use PostgreSQL for Storage # Deploy to the cloud # Add OpenTelemetry #
  • 18.
    Cheatsheet Middleware forLogging andRecovery Using middlewares from chi routerorothers. What does the recovery middleware do? Bonus: How can you set a request timeout using middleware? r := chi.NewRouter() r.Use(middleware.Logger) r.Use(middleware.Recoverer) # #
  • 19.
    Cheatsheet Configure Port fromEnvironment Variable Using the Standard Library Using envconfig github.com/kelseyhightower/envconfig func main() { http.ListenAndServe(fmt.Sprintf(":%v", os.Getenv("PORT")), r) } // Application Configuration type Config struct { Port string `envconfig:"PORT" default:"8080"` } func main() { http.ListenAndServe(fmt.Sprintf(":%v", c.Port), r) }
  • 20.
    Cheatsheet Handle Errorsproperly Using the Standard Library Using Problem forHTTPAPIs Use with schneider.vip/problem if err != nil { http.Error(w, "Oops, that went wrong!", http.StatusInternalServerError) return } rfc7807 if err != nil { problem.New( problem.Wrap(err), problem.Status(http.StatusInternalServerError) ).WriteTo(w) return }
  • 21.
    Cheatsheet Run inContainer # 1. RPS Builder FROM golang as builder WORKDIR /app ADD . /app RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o build/rps . # 2. RPS Container FROM gcr.io/distroless/static COPY --from=builder /app/build/rps /usr/bin/ EXPOSE 8080 ENTRYPOINT ["/usr/bin/rps"]
  • 22.
    Cheatsheet Add aHealth Check See github.com/hellofresh/health-go/v5. // Register Health Check h, _ := health.New(health.WithChecks( health.Config{ Name: "db", Timeout: time.Second * 2, SkipOnErr: false, Check: healthPgx.New(healthPgx.Config{ DSN: config.Db, }), }, )) // Register Health Check Handler Function r.Get("/health", h.HandlerFunc)
  • 23.
    Cheatsheet Use PostgreSQLfor Storage Sample application # SQL Tutorial with Go # PostgreSQL DriverPGX # offer
  • 24.
    Cheatsheet Cloud Deployment ExampleDeployment forrender services: - type: web name: backend-go env: go plan: free rootDir: backend-go buildCommand: go build -o rps . startCommand: ./rps
  • 25.
  • 26.
  • 27.
    Prerequisites Spring Session Installsome IDE (e.g. or ) Install Install Java JDK(e.g. ) Clone and checkout branch spring-start Frontend (npm is included) # IntelliJ Visual Studio Code # Postman # Temurin # github.com/crossnative/cloudland-rps # Node.js
  • 28.
    First Steps SpringSession 1. Run local Frontend togetherwith example backend https://backend-spring.onrender.com/api/v1 2. Build and Run Spring Backend 3. Implement REST API 4. oradd Make Cloud Native furtherFeatures
  • 29.
    Spring Cloud NativePath ... # Configurable Application Port # Improve ErrorHandling # Configure proper Logging # Health Check # Run in Container # Use Database for persistence # Deploy to the Cloud # Add OpenTelemetry #
  • 30.
    Cheatsheet ErrorHandling # UseJSON Problem Details (RFC 7807) # Create a Http ErrorHandler
  • 31.
    Cheatsheet Run inContainer With Buildpacks run ./mvnw spring-boot:build-image -Dspring- boot.build-image.imageName=myorg/myapp # Spring Official Guide # Baeldung: Dockerizing a Spring Boot Application
  • 32.
    Feature Ideas Rock,Paper, Scissors Add a leaderboard with top 10 Players Make Game realtime (e.g. WebSocket, ...) Enhance to Add rooms people can play many Games in ... # # # Rock, Paper, Scissors, Spock, Lizard # #
  • 33.
    Interesting Links # TheTwelve FactorApp # Beyond the Twelve FactorApp # Cloud-native entwickeln mit Go # Building a MultiplayerGame with API Gateway+Websockets, Go and DynamoDB
  • 34.
    Jan Stamer Lena Grimm FynnJasparStempel jan.stamer@crossnative.com lena.grimm@crossnative.com fynn.jaspar.stempel@crossnative.com