Skip to content

Commit

Permalink
update blueprint (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
fogfish authored Mar 15, 2024
1 parent 3df2387 commit a4c8785
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2
- uses: actions/checkout@v4.1.1

- id: deploy
uses: fogfish/deploy-cdk-go@latest
with:
go-version: "1.20"
go-version: "1.21"
stack: ${{ matrix.stack }}
version: latest
issue-to-create: ./.github/issue-spawn-latest.md
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/carry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2
- uses: actions/checkout@v4.1.1

- id: deploy
uses: fogfish/deploy-cdk-go@latest
with:
go-version: "1.20"
go-version: "1.21"
stack: ${{ matrix.stack }}
version: ${{ github.event.release.name }}
issue-to-create: ./.github/issue-spawn-release.md
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/check-clean.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2
- uses: actions/checkout@v4.1.1

- uses: fogfish/deploy-cdk-go@latest
with:
go-version: "1.20"
go-version: "1.21"
command: destroy
stack: ${{ matrix.stack }}
version: pr${{ github.event.number }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/check-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v3
- uses: actions/checkout@v4.1.1

- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
cache: true

- uses: dominikh/staticcheck-action@v1.2.0
- uses: dominikh/staticcheck-action@v1.3.0
with:
install-go: false
4 changes: 2 additions & 2 deletions .github/workflows/check-spawn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4.1.1

- id: deploy
uses: fogfish/deploy-cdk-go@latest
with:
go-version: "1.20"
go-version: "1.21"
stack: ${{ matrix.stack }}
version: pr${{ github.event.number }}
issue-to-comment: ${{ github.event.number }}
Expand Down
12 changes: 5 additions & 7 deletions .github/workflows/check-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
runs-on: ubuntu-latest
steps:

- uses: actions/checkout@v2
- uses: actions/checkout@v4.1.1

- uses: actions/setup-go@v3
- uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
cache: true

- name: go build
Expand All @@ -34,10 +34,8 @@ jobs:
run: |
go test -v -coverprofile=profile.cov $(go list ./... | grep -v /examples/)
env:
## GOPATH required to build serverless app inside unittest
GOPATH: /home/runner/go
GOCACHE: /home/runner/.cache/go-build
GOMODCACHE: /home/runner/go/pkg/mod
## GOPATH required to build serverless app inside unittest
GOPATH: /home/runner/work/${{ github.event.repository.name }}/go

- uses: shogo82148/actions-goveralls@v1
continue-on-error: true
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ node_modules/
.vscode/

assay-it/

cdk.context.json
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ github.com/.../the-beautiful-app
| |
| ├─ api // public objects used by API
| | └─ pet.go
| ├─ curl // client library
| | └─ petshop.go
| └─ suites // testing suites for api endpoint(s)
|
├─ cmd // executables of the project
Expand Down Expand Up @@ -176,10 +178,24 @@ cdk deploy

In few seconds, the application becomes available at

```
```bash
curl https://xxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/api
```

The api is protected by AWS IAM, request has to be signed.
Either use example client `cmd/petshop-cli` or curl directly

```bash
curl $BLUEPRINT/petshop/pets \
-XGET \
-H "Accept: application/json" \
--aws-sigv4 "aws:amz:eu-west-1:execute-api" \
--user "$AWS_ACCESS_KEY_ID":"$AWS_SECRET_ACCESS_KEY"
```

See [all available endpoints](./http/petshop.go).


**test in production**

```bash
Expand Down
18 changes: 11 additions & 7 deletions cloud/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/aws/aws-cdk-go/awscdk/v2"
"github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway"
"github.com/aws/aws-cdk-go/awscdk/v2/awsapigatewayv2"
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/fogfish/scud"
Expand Down Expand Up @@ -35,8 +35,11 @@ func main() {
//
// Stack
//
stackID := fmt.Sprintf("blueprint-golang-%s", vsn(app))
stack := awscdk.NewStack(app, jsii.String(stackID), config)
vsn := vsn(app)
stack := awscdk.NewStack(app,
jsii.String(fmt.Sprintf("blueprint-golang-%s", vsn)),
config,
)

NewBlueprint(stack)

Expand All @@ -46,16 +49,17 @@ func main() {
// NewBlueprint create example REST api
func NewBlueprint(scope constructs.Construct) {
gateway := scud.NewGateway(scope, jsii.String("Gateway"),
&awsapigateway.RestApiProps{
RestApiName: jsii.String("petshop"),
&scud.GatewayProps{
HttpApiProps: &awsapigatewayv2.HttpApiProps{},
},
)
gateway.WithAuthorizerIAM()

myfun := scud.NewFunctionGo(scope, jsii.String("MyFun"),
handler := scud.NewFunctionGo(scope, jsii.String("Handler"),
&scud.FunctionGoProps{
SourceCodePackage: "github.com/fogfish/blueprint-serverless-golang",
SourceCodeLambda: "cmd/lambda/petshop",
},
)
gateway.AddResource("petshop", myfun)
gateway.AddResource("/petshop", handler)
}
13 changes: 6 additions & 7 deletions cloud/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ func TestBlueprint(t *testing.T) {
NewBlueprint(stack)

require := map[*string]*float64{
jsii.String("AWS::ApiGateway::RestApi"): jsii.Number(1),
jsii.String("AWS::ApiGateway::Deployment"): jsii.Number(1),
jsii.String("AWS::ApiGateway::Stage"): jsii.Number(1),
jsii.String("AWS::ApiGateway::Method"): jsii.Number(5),
jsii.String("AWS::IAM::Role"): jsii.Number(3),
jsii.String("AWS::Lambda::Function"): jsii.Number(2),
jsii.String("Custom::LogRetention"): jsii.Number(1),
jsii.String("AWS::ApiGatewayV2::Api"): jsii.Number(1),
jsii.String("AWS::ApiGatewayV2::Stage"): jsii.Number(2),
jsii.String("AWS::ApiGatewayV2::Route"): jsii.Number(1),
jsii.String("AWS::IAM::Role"): jsii.Number(2),
jsii.String("AWS::Lambda::Function"): jsii.Number(2),
jsii.String("Custom::LogRetention"): jsii.Number(1),
}

template := assertions.Template_FromStack(stack, nil)
Expand Down
56 changes: 56 additions & 0 deletions cmd/petshop-cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/fogfish/blueprint-serverless-golang/http/curl"
"github.com/fogfish/gurl/awsapi"
"github.com/fogfish/gurl/v2/http"
)

// go run main.go https://XXXXXXXXXX.execute-api.eu-west-1.amazonaws.com
func main() {
fmt.Println(os.Args)

cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
panic(err)
}

curl := curl.NewPetShop(
http.New(awsapi.WithSignatureV4(cfg)),
os.Args[1],
)

pets, err := curl.List(context.Background())
if err != nil {
panic(err)
}
output(pets)

if pets.Next != nil {
pets, err = curl.Continue(context.Background(), *pets.Next)
if err != nil {
panic(err)
}
output(pets)
}

if len(pets.Pets) > 0 {
pet, err := curl.Pet(context.Background(), pets.Pets[0].Url)
if err != nil {
panic(err)
}
output(pet)
}
}

func output(x any) {
b, _ := json.MarshalIndent(x, "|", " ")
os.Stdout.Write(b)
os.Stdout.Write([]byte("\n\n"))
}
24 changes: 20 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
module github.com/fogfish/blueprint-serverless-golang

go 1.20
go 1.21.0

toolchain go1.22.0

require (
github.com/ajg/form v1.5.2-0.20200323032839-9aeb3cf462e1 // indirect
github.com/aws/aws-cdk-go/awscdk/v2 v2.132.0
github.com/aws/aws-cdk-go/awscdk/v2 v2.133.0
github.com/aws/aws-lambda-go v1.46.0
github.com/aws/constructs-go/constructs/v10 v10.3.0
github.com/aws/jsii-runtime-go v1.95.0
github.com/fogfish/scud v0.6.0
github.com/fogfish/scud v0.7.1
)

require (
github.com/aws/aws-sdk-go-v2/config v1.27.7
github.com/fogfish/faults v0.2.0
github.com/fogfish/gouldian/v2 v2.0.5
github.com/fogfish/guid/v2 v2.0.4
github.com/fogfish/gurl/v2 v2.8.2
github.com/fogfish/gurl/awsapi v0.0.1
github.com/fogfish/gurl/v2 v2.8.3
github.com/fogfish/it/v2 v2.0.1
github.com/fogfish/schemaorg v1.22.0
github.com/fogfish/skiplist v0.15.0
Expand All @@ -24,6 +28,18 @@ require (

require (
github.com/Masterminds/semver/v3 v3.2.1 // indirect
github.com/aws/aws-sdk-go-v2 v1.25.3 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.7 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.4 // indirect
github.com/aws/smithy-go v1.20.1 // indirect
github.com/cdklabs/awscdk-asset-awscli-go/awscliv1/v2 v2.2.202 // indirect
github.com/cdklabs/awscdk-asset-kubectl-go/kubectlv20/v2 v2.1.2 // indirect
github.com/cdklabs/awscdk-asset-node-proxy-agent-go/nodeproxyagentv6/v2 v2.0.1 // indirect
Expand Down
Loading

0 comments on commit a4c8785

Please sign in to comment.