From ece080e904224a8dc4bad259f914bb36aa0d353e Mon Sep 17 00:00:00 2001 From: Jack Kleeman Date: Tue, 23 Jul 2024 13:01:19 +0100 Subject: [PATCH] Add go example (#171) * Add go example * Review comments * Remove dockerfile --- .github/workflows/pre-release.yml | 34 +++++++++++++++++++++++-------- .github/workflows/release.yml | 1 + .github/workflows/test.yml | 31 ++++++++++++++++++++++------ .tools/prepare_release_zip.sh | 1 + .tools/run_go_tests.sh | 13 ++++++++++++ .tools/update_go_examples.sh | 14 +++++++++++++ templates/go/.gitignore | 1 + templates/go/README.md | 9 ++++++++ templates/go/go.mod | 14 +++++++++++++ templates/go/go.sum | 26 +++++++++++++++++++++++ templates/go/greeter.go | 14 +++++++++++++ templates/go/main.go | 20 ++++++++++++++++++ 12 files changed, 164 insertions(+), 14 deletions(-) create mode 100755 .tools/run_go_tests.sh create mode 100755 .tools/update_go_examples.sh create mode 100644 templates/go/.gitignore create mode 100644 templates/go/README.md create mode 100644 templates/go/go.mod create mode 100644 templates/go/go.sum create mode 100644 templates/go/greeter.go create mode 100644 templates/go/main.go diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 9397ae7e..073f6b5a 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -4,19 +4,23 @@ on: workflow_dispatch: inputs: restateVersion: - description: 'Restate runtime version (without prepending v). Leave empty if you do not want to update it.' + description: "Restate runtime version (without prepending v). Leave empty if you do not want to update it." required: false type: string sdkTypescriptVersion: - description: 'sdk-typescript version (without prepending v). Leave empty if you do not want to update it.' + description: "sdk-typescript version (without prepending v). Leave empty if you do not want to update it." required: false type: string sdkJavaVersion: - description: 'sdk-java version (without prepending v). Leave empty if you do not want to update it.' + description: "sdk-java version (without prepending v). Leave empty if you do not want to update it." + required: false + type: string + sdkGoVersion: + description: "sdk-go version (WITH the prepending v). Leave empty if you do not want to update it." required: false type: string cdkVersion: - description: 'cdk version (without prepending v). Leave empty if you do not want to update it.' + description: "cdk version (without prepending v). Leave empty if you do not want to update it." required: false type: string @@ -40,7 +44,7 @@ jobs: if: github.event.inputs.sdkTypescriptVersion != '' with: node-version: "19.x" - registry-url: 'https://registry.npmjs.org' + registry-url: "https://registry.npmjs.org" - name: Bump sdk-typescript if: github.event.inputs.sdkTypescriptVersion != '' run: ./.tools/update_node_examples.sh ${{ inputs.sdkTypescriptVersion }} @@ -55,8 +59,8 @@ jobs: - uses: actions/setup-java@v3 if: github.event.inputs.sdkJavaVersion != '' with: - distribution: 'temurin' - java-version: '17' + distribution: "temurin" + java-version: "17" - name: Setup Gradle if: github.event.inputs.sdkJavaVersion != '' uses: gradle/actions/setup-gradle@v3 @@ -67,13 +71,27 @@ jobs: if: github.event.inputs.sdkJavaVersion != '' run: ./.tools/run_jvm_tests.sh + # Bump Go SDK + - uses: actions/setup-go@v5 + if: github.event.inputs.sdkGoVersion != '' + with: + go-version: "1.22" + - name: Bump sdk-go + if: github.event.inputs.sdkGoVersion != '' + run: ./.tools/update_go_examples.sh ${{ inputs.sdkGoVersion }} + - name: Run Go tests + if: github.event.inputs.sdkGoVersion != '' + run: ./.tools/run_go_tests.sh + - name: Create Pull Request uses: peter-evans/create-pull-request@v5 with: - title: "[GithubActions] Update Restate ${{ inputs.restateVersion != '' && format('Runtime {0} ', inputs.restateVersion) }}${{ inputs.sdkTypescriptVersion != '' && format('SDK-Typescript {0} ', inputs.sdkTypescriptVersion) }}${{ inputs.sdkJavaVersion != '' && format('SDK-Java {0} ', inputs.sdkJavaVersion) }}${{ inputs.cdkVersion != '' && format('CDK {0} ', inputs.cdkVersion) }}" + title: "[GithubActions] Update Restate ${{ inputs.restateVersion != '' && format('Runtime {0} ', inputs.restateVersion) }}${{ inputs.sdkTypescriptVersion != '' && format('SDK-Typescript {0} ', inputs.sdkTypescriptVersion) }}${{ inputs.sdkJavaVersion != '' && format('SDK-Java {0} ', inputs.sdkJavaVersion) }}${{ inputs.cdkVersion != '' && format('CDK {0} ', inputs.cdkVersion) }}${{ inputs.sdkGoVersion != '' && format('SDK-Go {0} ', inputs.sdkGoVersion) }}" commit-message: "[GithubActions] Update Restate ${{ inputs.restateVersion != '' && format('Runtime {0} ', inputs.restateVersion) }}${{ inputs.sdkTypescriptVersion != '' && format('SDK-Typescript {0} ', inputs.sdkTypescriptVersion) }}${{ inputs.sdkJavaVersion != '' && format('SDK-Java {0} ', inputs.sdkJavaVersion) }}${{ inputs.cdkVersion != '' && format('CDK {0} ', inputs.cdkVersion) }}" add-paths: | **/package.json **/package-lock.json **/build.gradle.kts **/pom.xml + **/go.mod + **/go.sum diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 65c324ef..be307b33 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -32,6 +32,7 @@ jobs: typescript-bun-hello-world.zip typescript-cloudflare-worker-hello-world.zip typescript-deno-hello-world.zip + go-hello-world.zip kotlin-hello-world-lambda-cdk.zip java-tour-of-restate.zip typescript-tour-of-restate.zip diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b7ad3c98..d89d43a9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,9 +2,9 @@ name: Build and test on: push: - branches: [ main ] + branches: [main] pull_request: - branches: [ main ] + branches: [main] jobs: build-jvm: @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - jvm-version: [ 17 ] + jvm-version: [17] steps: - uses: actions/checkout@v3 @@ -21,7 +21,7 @@ jobs: - name: Use JVM ${{ matrix.jvm-version }} uses: actions/setup-java@v4 with: - distribution: 'temurin' + distribution: "temurin" java-version: ${{ matrix.jvm-version }} - name: Setup Gradle @@ -45,18 +45,37 @@ jobs: uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - registry-url: 'https://registry.npmjs.org' + registry-url: "https://registry.npmjs.org" - name: Run tests run: ./.tools/run_node_tests.sh + build-go: + # prevent from running on forks + if: github.repository_owner == 'restatedev' + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [1.22] + + steps: + - uses: actions/checkout@v3 + + - name: Use Go ${{ matrix.go-version }} + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go-version }} + + - name: Run tests + run: ./.tools/run_go_tests.sh + build-py: # prevent from running on forks if: github.repository_owner == 'restatedev' runs-on: ubuntu-latest strategy: matrix: - python: [ "3.12" ] + python: ["3.12"] steps: - uses: actions/checkout@v3 diff --git a/.tools/prepare_release_zip.sh b/.tools/prepare_release_zip.sh index 29104b8b..9cc8ffb2 100755 --- a/.tools/prepare_release_zip.sh +++ b/.tools/prepare_release_zip.sh @@ -23,6 +23,7 @@ create_release_zip templates/typescript typescript-hello-world create_release_zip templates/bun typescript-bun-hello-world create_release_zip templates/cloudflare-worker typescript-cloudflare-worker-hello-world create_release_zip templates/deno typescript-deno-hello-world +create_release_zip templates/go go-hello-world create_release_zip templates/typescript-lambda-cdk typescript-hello-world-lambda-cdk create_release_zip templates/kotlin-gradle-lambda-cdk kotlin-hello-world-lambda-cdk create_release_zip templates/python python-hello-world diff --git a/.tools/run_go_tests.sh b/.tools/run_go_tests.sh new file mode 100755 index 00000000..1b49bd35 --- /dev/null +++ b/.tools/run_go_tests.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +SELF_PATH=${BASH_SOURCE[0]:-"$(command -v -- "$0")"} +PROJECT_ROOT="$(dirname "$SELF_PATH")/.." + +function go_build_check() { + pushd $1 + go build -o /dev/null . + go vet ./... + popd +} + +go_build_check $PROJECT_ROOT/templates/go diff --git a/.tools/update_go_examples.sh b/.tools/update_go_examples.sh new file mode 100755 index 00000000..bdf9184a --- /dev/null +++ b/.tools/update_go_examples.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +NEW_VERSION=$1 +SELF_PATH=${BASH_SOURCE[0]:-"$(command -v -- "$0")"} +PROJECT_ROOT="$(dirname "$SELF_PATH")/.." + +function bump_go_sdk() { + pushd $1 + go get github.com/restatedev/sdk-go@$NEW_VERSION + go mod tidy + popd +} + +bump_go_sdk $PROJECT_ROOT/templates/go diff --git a/templates/go/.gitignore b/templates/go/.gitignore new file mode 100644 index 00000000..4023f209 --- /dev/null +++ b/templates/go/.gitignore @@ -0,0 +1 @@ +go diff --git a/templates/go/README.md b/templates/go/README.md new file mode 100644 index 00000000..466cbfb1 --- /dev/null +++ b/templates/go/README.md @@ -0,0 +1,9 @@ +# Hello world - Go example + +Sample project configuration of a Restate service using the Go SDK. + +You can run locally with `go run .` and register to Restate with +`restate dep add http://localhost:9080`. Then you can invoke with `curl localhost:8080/Greeter/Greet --json '"hello"'`. + +You can build a docker image using [ko](https://github.com/ko-build/ko): +`ko build --platform=all` diff --git a/templates/go/go.mod b/templates/go/go.mod new file mode 100644 index 00000000..fa513ab3 --- /dev/null +++ b/templates/go/go.mod @@ -0,0 +1,14 @@ +module github.com/restatedev/examples/templates/go + +go 1.22.5 + +require github.com/restatedev/sdk-go v0.9.1 + +require ( + github.com/golang-jwt/jwt/v5 v5.2.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mr-tron/base58 v1.2.0 // indirect + golang.org/x/net v0.21.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect +) diff --git a/templates/go/go.sum b/templates/go/go.sum new file mode 100644 index 00000000..8b9673ce --- /dev/null +++ b/templates/go/go.sum @@ -0,0 +1,26 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= +github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/restatedev/sdk-go v0.9.1 h1:IWn+f3t8ow996U47nnMcFQwP7Yn8X35IAIdKHHZC504= +github.com/restatedev/sdk-go v0.9.1/go.mod h1:wv6BuURHPiFOe5lomBGA6VETSWvAv8iLIKWBY7JUFSU= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/templates/go/greeter.go b/templates/go/greeter.go new file mode 100644 index 00000000..06f451ec --- /dev/null +++ b/templates/go/greeter.go @@ -0,0 +1,14 @@ +package main + +import ( + "fmt" + + restate "github.com/restatedev/sdk-go" +) + +// Greeter is a struct which represents a Restate service; reflection will turn exported methods into service handlers +type Greeter struct{} + +func (Greeter) Greet(ctx restate.Context, greeting string) (string, error) { + return fmt.Sprintf("%s!", greeting), nil +} diff --git a/templates/go/main.go b/templates/go/main.go new file mode 100644 index 00000000..62e04fd3 --- /dev/null +++ b/templates/go/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "context" + "log/slog" + "os" + + restate "github.com/restatedev/sdk-go" + "github.com/restatedev/sdk-go/server" +) + +func main() { + server := server.NewRestate(). + Bind(restate.Service(Greeter{})) + + if err := server.Start(context.Background(), ":9080"); err != nil { + slog.Error("application exited unexpectedly", "err", err.Error()) + os.Exit(1) + } +}