Skip to content

Commit a4c8785

Browse files
authored
update blueprint (#48)
1 parent 3df2387 commit a4c8785

19 files changed

+241
-60
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ jobs:
1616
runs-on: ubuntu-latest
1717
steps:
1818

19-
- uses: actions/checkout@v2
19+
- uses: actions/checkout@v4.1.1
2020

2121
- id: deploy
2222
uses: fogfish/deploy-cdk-go@latest
2323
with:
24-
go-version: "1.20"
24+
go-version: "1.21"
2525
stack: ${{ matrix.stack }}
2626
version: latest
2727
issue-to-create: ./.github/issue-spawn-latest.md

.github/workflows/carry.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515

16-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v4.1.1
1717

1818
- id: deploy
1919
uses: fogfish/deploy-cdk-go@latest
2020
with:
21-
go-version: "1.20"
21+
go-version: "1.21"
2222
stack: ${{ matrix.stack }}
2323
version: ${{ github.event.release.name }}
2424
issue-to-create: ./.github/issue-spawn-release.md

.github/workflows/check-clean.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ jobs:
1717
runs-on: ubuntu-latest
1818
steps:
1919

20-
- uses: actions/checkout@v2
20+
- uses: actions/checkout@v4.1.1
2121

2222
- uses: fogfish/deploy-cdk-go@latest
2323
with:
24-
go-version: "1.20"
24+
go-version: "1.21"
2525
command: destroy
2626
stack: ${{ matrix.stack }}
2727
version: pr${{ github.event.number }}

.github/workflows/check-code.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ jobs:
1414
runs-on: ubuntu-latest
1515
steps:
1616

17-
- uses: actions/checkout@v3
17+
- uses: actions/checkout@v4.1.1
1818

19-
- uses: actions/setup-go@v3
19+
- uses: actions/setup-go@v5
2020
with:
21-
go-version: "1.20"
21+
go-version: "1.21"
2222
cache: true
2323

24-
- uses: dominikh/staticcheck-action@v1.2.0
24+
- uses: dominikh/staticcheck-action@v1.3.0
2525
with:
2626
install-go: false

.github/workflows/check-spawn.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ jobs:
1818

1919
runs-on: ubuntu-latest
2020
steps:
21-
- uses: actions/checkout@v2
21+
- uses: actions/checkout@v4.1.1
2222

2323
- id: deploy
2424
uses: fogfish/deploy-cdk-go@latest
2525
with:
26-
go-version: "1.20"
26+
go-version: "1.21"
2727
stack: ${{ matrix.stack }}
2828
version: pr${{ github.event.number }}
2929
issue-to-comment: ${{ github.event.number }}

.github/workflows/check-test.yml

+5-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ jobs:
1919
runs-on: ubuntu-latest
2020
steps:
2121

22-
- uses: actions/checkout@v2
22+
- uses: actions/checkout@v4.1.1
2323

24-
- uses: actions/setup-go@v3
24+
- uses: actions/setup-go@v5
2525
with:
26-
go-version: "1.20"
26+
go-version: "1.21"
2727
cache: true
2828

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

4240
- uses: shogo82148/actions-goveralls@v1
4341
continue-on-error: true

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ node_modules/
1818
.vscode/
1919

2020
assay-it/
21+
22+
cdk.context.json

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ github.com/.../the-beautiful-app
122122
| |
123123
| ├─ api // public objects used by API
124124
| | └─ pet.go
125+
| ├─ curl // client library
126+
| | └─ petshop.go
125127
| └─ suites // testing suites for api endpoint(s)
126128
|
127129
├─ cmd // executables of the project
@@ -176,10 +178,24 @@ cdk deploy
176178

177179
In few seconds, the application becomes available at
178180

179-
```
181+
```bash
180182
curl https://xxxxxxxxxx.execute-api.eu-west-1.amazonaws.com/api
181183
```
182184

185+
The api is protected by AWS IAM, request has to be signed.
186+
Either use example client `cmd/petshop-cli` or curl directly
187+
188+
```bash
189+
curl $BLUEPRINT/petshop/pets \
190+
-XGET \
191+
-H "Accept: application/json" \
192+
--aws-sigv4 "aws:amz:eu-west-1:execute-api" \
193+
--user "$AWS_ACCESS_KEY_ID":"$AWS_SECRET_ACCESS_KEY"
194+
```
195+
196+
See [all available endpoints](./http/petshop.go).
197+
198+
183199
**test in production**
184200

185201
```bash

cloud/blueprint.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"os"
66

77
"github.com/aws/aws-cdk-go/awscdk/v2"
8-
"github.com/aws/aws-cdk-go/awscdk/v2/awsapigateway"
8+
"github.com/aws/aws-cdk-go/awscdk/v2/awsapigatewayv2"
99
"github.com/aws/constructs-go/constructs/v10"
1010
"github.com/aws/jsii-runtime-go"
1111
"github.com/fogfish/scud"
@@ -35,8 +35,11 @@ func main() {
3535
//
3636
// Stack
3737
//
38-
stackID := fmt.Sprintf("blueprint-golang-%s", vsn(app))
39-
stack := awscdk.NewStack(app, jsii.String(stackID), config)
38+
vsn := vsn(app)
39+
stack := awscdk.NewStack(app,
40+
jsii.String(fmt.Sprintf("blueprint-golang-%s", vsn)),
41+
config,
42+
)
4043

4144
NewBlueprint(stack)
4245

@@ -46,16 +49,17 @@ func main() {
4649
// NewBlueprint create example REST api
4750
func NewBlueprint(scope constructs.Construct) {
4851
gateway := scud.NewGateway(scope, jsii.String("Gateway"),
49-
&awsapigateway.RestApiProps{
50-
RestApiName: jsii.String("petshop"),
52+
&scud.GatewayProps{
53+
HttpApiProps: &awsapigatewayv2.HttpApiProps{},
5154
},
5255
)
56+
gateway.WithAuthorizerIAM()
5357

54-
myfun := scud.NewFunctionGo(scope, jsii.String("MyFun"),
58+
handler := scud.NewFunctionGo(scope, jsii.String("Handler"),
5559
&scud.FunctionGoProps{
5660
SourceCodePackage: "github.com/fogfish/blueprint-serverless-golang",
5761
SourceCodeLambda: "cmd/lambda/petshop",
5862
},
5963
)
60-
gateway.AddResource("petshop", myfun)
64+
gateway.AddResource("/petshop", handler)
6165
}

cloud/blueprint_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ func TestBlueprint(t *testing.T) {
1515
NewBlueprint(stack)
1616

1717
require := map[*string]*float64{
18-
jsii.String("AWS::ApiGateway::RestApi"): jsii.Number(1),
19-
jsii.String("AWS::ApiGateway::Deployment"): jsii.Number(1),
20-
jsii.String("AWS::ApiGateway::Stage"): jsii.Number(1),
21-
jsii.String("AWS::ApiGateway::Method"): jsii.Number(5),
22-
jsii.String("AWS::IAM::Role"): jsii.Number(3),
23-
jsii.String("AWS::Lambda::Function"): jsii.Number(2),
24-
jsii.String("Custom::LogRetention"): jsii.Number(1),
18+
jsii.String("AWS::ApiGatewayV2::Api"): jsii.Number(1),
19+
jsii.String("AWS::ApiGatewayV2::Stage"): jsii.Number(2),
20+
jsii.String("AWS::ApiGatewayV2::Route"): jsii.Number(1),
21+
jsii.String("AWS::IAM::Role"): jsii.Number(2),
22+
jsii.String("AWS::Lambda::Function"): jsii.Number(2),
23+
jsii.String("Custom::LogRetention"): jsii.Number(1),
2524
}
2625

2726
template := assertions.Template_FromStack(stack, nil)

cmd/petshop-cli/main.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
9+
"github.com/aws/aws-sdk-go-v2/config"
10+
"github.com/fogfish/blueprint-serverless-golang/http/curl"
11+
"github.com/fogfish/gurl/awsapi"
12+
"github.com/fogfish/gurl/v2/http"
13+
)
14+
15+
// go run main.go https://XXXXXXXXXX.execute-api.eu-west-1.amazonaws.com
16+
func main() {
17+
fmt.Println(os.Args)
18+
19+
cfg, err := config.LoadDefaultConfig(context.Background())
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
curl := curl.NewPetShop(
25+
http.New(awsapi.WithSignatureV4(cfg)),
26+
os.Args[1],
27+
)
28+
29+
pets, err := curl.List(context.Background())
30+
if err != nil {
31+
panic(err)
32+
}
33+
output(pets)
34+
35+
if pets.Next != nil {
36+
pets, err = curl.Continue(context.Background(), *pets.Next)
37+
if err != nil {
38+
panic(err)
39+
}
40+
output(pets)
41+
}
42+
43+
if len(pets.Pets) > 0 {
44+
pet, err := curl.Pet(context.Background(), pets.Pets[0].Url)
45+
if err != nil {
46+
panic(err)
47+
}
48+
output(pet)
49+
}
50+
}
51+
52+
func output(x any) {
53+
b, _ := json.MarshalIndent(x, "|", " ")
54+
os.Stdout.Write(b)
55+
os.Stdout.Write([]byte("\n\n"))
56+
}

go.mod

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
module github.com/fogfish/blueprint-serverless-golang
22

3-
go 1.20
3+
go 1.21.0
4+
5+
toolchain go1.22.0
46

57
require (
68
github.com/ajg/form v1.5.2-0.20200323032839-9aeb3cf462e1 // indirect
7-
github.com/aws/aws-cdk-go/awscdk/v2 v2.132.0
9+
github.com/aws/aws-cdk-go/awscdk/v2 v2.133.0
810
github.com/aws/aws-lambda-go v1.46.0
911
github.com/aws/constructs-go/constructs/v10 v10.3.0
1012
github.com/aws/jsii-runtime-go v1.95.0
11-
github.com/fogfish/scud v0.6.0
13+
github.com/fogfish/scud v0.7.1
1214
)
1315

1416
require (
17+
github.com/aws/aws-sdk-go-v2/config v1.27.7
1518
github.com/fogfish/faults v0.2.0
1619
github.com/fogfish/gouldian/v2 v2.0.5
1720
github.com/fogfish/guid/v2 v2.0.4
18-
github.com/fogfish/gurl/v2 v2.8.2
21+
github.com/fogfish/gurl/awsapi v0.0.1
22+
github.com/fogfish/gurl/v2 v2.8.3
1923
github.com/fogfish/it/v2 v2.0.1
2024
github.com/fogfish/schemaorg v1.22.0
2125
github.com/fogfish/skiplist v0.15.0
@@ -24,6 +28,18 @@ require (
2428

2529
require (
2630
github.com/Masterminds/semver/v3 v3.2.1 // indirect
31+
github.com/aws/aws-sdk-go-v2 v1.25.3 // indirect
32+
github.com/aws/aws-sdk-go-v2/credentials v1.17.7 // indirect
33+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.15.3 // indirect
34+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.3 // indirect
35+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.3 // indirect
36+
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
37+
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.1 // indirect
38+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.5 // indirect
39+
github.com/aws/aws-sdk-go-v2/service/sso v1.20.2 // indirect
40+
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.2 // indirect
41+
github.com/aws/aws-sdk-go-v2/service/sts v1.28.4 // indirect
42+
github.com/aws/smithy-go v1.20.1 // indirect
2743
github.com/cdklabs/awscdk-asset-awscli-go/awscliv1/v2 v2.2.202 // indirect
2844
github.com/cdklabs/awscdk-asset-kubectl-go/kubectlv20/v2 v2.1.2 // indirect
2945
github.com/cdklabs/awscdk-asset-node-proxy-agent-go/nodeproxyagentv6/v2 v2.0.1 // indirect

0 commit comments

Comments
 (0)