Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 46c4c7d

Browse files
authored
Merge pull request #23 from tzununbekov/master
CE response wrapper
2 parents 569be4c + 19452f4 commit 46c4c7d

File tree

161 files changed

+265
-9643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+265
-9643
lines changed

Gopkg.lock

Lines changed: 0 additions & 28 deletions
This file was deleted.

Gopkg.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ The AWS Lambdas execution [environment](https://docs.aws.amazon.com/lambda/lates
1212

1313
This repository contains an `example` lambda function written in bash with a AWS custom runtime described in this AWS [tutorial](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-walkthrough.html). To run this function use our [`tm`](https://github.com/triggermesh/tm) client to talk to the knative API.
1414

15-
1. Install AWS custom runtime buildtemplate:
15+
1. Install AWS custom runtime:
1616
```
17-
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/aws-custom-runtime/master/buildtemplate.yaml
17+
tm deploy task -f https://raw.githubusercontent.com/triggermesh/aws-custom-runtime/master/runtime.yaml
1818
```
1919

2020
2. Deploy function:
2121
```
22-
tm deploy service lambda-bash -f https://github.com/triggermesh/aws-custom-runtime --build-template aws-custom-runtime --build-argument DIRECTORY=example --wait
22+
tm deploy service lambda-bash -f https://github.com/triggermesh/aws-custom-runtime --runtime aws-custom-runtime --build-argument DIRECTORY=example --wait
2323
```
2424

2525
In output you'll see URL that you can use to access `example/function.sh` function
2626

2727

2828
### AWS Lambda RUST example
2929

30-
RUST custom runtime is also verified to be compatible with this buildtemplate. Though [official readme](https://github.com/awslabs/aws-lambda-rust-runtime) has build instructions, it is more convenient to use docker.
30+
RUST is also verified to be compatible with this runtime. Though [official readme](https://github.com/awslabs/aws-lambda-rust-runtime) has build instructions, it is more convenient to use docker.
3131

3232
1. Clone repository:
3333
```
@@ -41,10 +41,10 @@ docker run --rm --user "$(id -u)":"$(id -g)" -v "$PWD":/usr/src/myapp -w /usr/sr
4141
mv target/release/examples/basic target/release/examples/bootstrap
4242
```
4343

44-
3. Deploy buildtemplate function using [`tm`](https://github.com/triggermesh/tm) CLI:
44+
3. Deploy runtime using [`tm`](https://github.com/triggermesh/tm) CLI:
4545
```
46-
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/aws-custom-runtime/master/buildtemplate.yaml
47-
tm deploy service lambda-rust -f target/release/examples/ --build-template aws-custom-runtime
46+
tm deploy runtime -f https://raw.githubusercontent.com/triggermesh/aws-custom-runtime/master/runtime.yaml
47+
tm deploy service lambda-rust -f target/release/examples/ --runtime aws-custom-runtime
4848
```
4949

5050
Use your RUST AWS Lambda function on knative:
@@ -114,8 +114,8 @@ make
114114

115115
4. Deploy with [`tm`](https://github.com/triggermesh/tm) CLI:
116116
```
117-
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/aws-custom-runtime/master/buildtemplate.yaml
118-
tm deploy service lambda-cpp -f . --build-template aws-custom-runtime
117+
tm deploy task -f https://raw.githubusercontent.com/triggermesh/aws-custom-runtime/master/runtime.yaml
118+
tm deploy service lambda-cpp -f . --runtime aws-custom-runtime
119119
```
120120

121121
C++ Lambda function is running on knative platform:

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/triggermesh/aws-custom-runtime
2+
3+
go 1.15
4+
5+
require (
6+
github.com/aws/aws-lambda-go v1.8.2
7+
github.com/cloudevents/sdk-go/v2 v2.3.1
8+
github.com/google/uuid v1.1.2
9+
github.com/kelseyhightower/envconfig v1.4.0
10+
)

go.sum

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/aws/aws-lambda-go v1.8.2 h1:wC8KcAG9HyVkFjbKQ9uhp87UGZutlPn9IJPq9fYM2BQ=
4+
github.com/aws/aws-lambda-go v1.8.2/go.mod h1:zUsUQhAUjYzR8AuduJPCfhBuKWUaDbQiPOG+ouzmE1A=
5+
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
6+
github.com/cloudevents/sdk-go v1.2.0 h1:2AxI14EJUw1PclJ5gZJtzbxnHIfNMdi76Qq3P3G1BRU=
7+
github.com/cloudevents/sdk-go/v2 v2.3.1 h1:QRTu0yRA4FbznjRSds0/4Hy6cVYpWV2wInlNJSHWAtw=
8+
github.com/cloudevents/sdk-go/v2 v2.3.1/go.mod h1:4fO2UjPMYYR1/7KPJQCwTPb0lFA8zYuitkUpAZFSY1Q=
9+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
10+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
13+
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
14+
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
15+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
16+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
17+
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
18+
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
19+
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
20+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
21+
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
22+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
23+
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
24+
github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk=
25+
github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
26+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
27+
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
28+
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
29+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
30+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
31+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
32+
github.com/lightstep/tracecontext.go v0.0.0-20181129014701-1757c391b1ac h1:+2b6iGRJe3hvV/yVXrd41yVEjxuFHxasJqDhkIjS4gk=
33+
github.com/lightstep/tracecontext.go v0.0.0-20181129014701-1757c391b1ac/go.mod h1:Frd2bnT3w5FB5q49ENTfVlztJES+1k/7lyWX2+9gq/M=
34+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
35+
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
36+
github.com/onsi/ginkgo v1.10.2/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
37+
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
38+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
39+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
40+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
41+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
42+
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
43+
go.opencensus.io v0.22.0 h1:C9hSCOW830chIVkdja34wa6Ky+IzWllkUinR+BtRZd4=
44+
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
45+
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
46+
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
47+
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
48+
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
49+
go.uber.org/zap v1.10.0 h1:ORx85nbTijNz8ljznvCMR1ZBIPKFn3jQrag10X2AsuM=
50+
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
51+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
52+
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
53+
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
54+
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
55+
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
56+
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
57+
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
58+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
59+
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
60+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
61+
golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
62+
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
63+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
64+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
65+
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
66+
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
67+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
68+
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
69+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
70+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
71+
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
72+
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
73+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
74+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
75+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
76+
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
77+
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
78+
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
79+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
80+
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
81+
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
82+
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
83+
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
84+
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
85+
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
86+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
87+
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
88+
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
89+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
90+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
91+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
92+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
93+
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

main.go

Lines changed: 64 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,13 @@ import (
2727
"time"
2828

2929
"github.com/kelseyhightower/envconfig"
30-
"github.com/triggermesh/aws-custom-runtime/pkg/events/apiGateway"
30+
"github.com/triggermesh/aws-custom-runtime/pkg/events"
31+
"github.com/triggermesh/aws-custom-runtime/pkg/events/apigateway"
32+
"github.com/triggermesh/aws-custom-runtime/pkg/events/cloudevents"
33+
"github.com/triggermesh/aws-custom-runtime/pkg/events/passthrough"
3134
)
3235

33-
type message struct {
34-
id string
35-
deadline int64
36-
data []byte
37-
statusCode int
38-
}
39-
40-
type responseWrapper struct {
41-
http.ResponseWriter
42-
StatusCode int
43-
Body []byte
44-
}
45-
46-
// Specification is a set of env variables that can be used to configure runtime API
47-
type Specification struct {
48-
// Number of bootstrap processes
49-
NumberOfinvokers int `envconfig:"invoker_count" default:"4"`
50-
// Request body size limit, Mb
51-
RequestSizeLimit int64 `envconfig:"request_size_limit" default:"5"`
52-
// Funtions deadline, seconds
53-
FunctionTTL int64 `envconfig:"function_ttl" default:"10"`
54-
// Lambda runtime API port for functions
55-
InternalAPIport string `envconfig:"internal_api_port" default:"80"`
56-
// Lambda API port to put function requests and get results
57-
ExternalAPIport string `envconfig:"external_api_port" default:"8080"`
58-
// Either return function result "as is" or consider it as API Gateway JSON
59-
EventType string `envconfig:"event_type"`
60-
}
61-
6236
var (
63-
// numberOfinvokers = 4
64-
// requestSizeLimit int64 = 5
65-
// functionTTL int64 = 10
66-
6737
tasks chan message
6838
results map[string]chan message
6939

@@ -84,6 +54,43 @@ var (
8454
}
8555
)
8656

57+
// Specification is a set of env variables that can be used to configure runtime API
58+
type Specification struct {
59+
// Number of bootstrap processes
60+
NumberOfinvokers int `envconfig:"invoker_count" default:"4"`
61+
// Request body size limit, Mb
62+
RequestSizeLimit int64 `envconfig:"request_size_limit" default:"5"`
63+
// Funtions deadline, seconds
64+
FunctionTTL int64 `envconfig:"function_ttl" default:"10"`
65+
// Lambda runtime API port for functions
66+
InternalAPIport string `envconfig:"internal_api_port" default:"80"`
67+
// Lambda API port to put function requests and get results
68+
ExternalAPIport string `envconfig:"external_api_port" default:"8080"`
69+
// Parent Knative Service name
70+
Service string `envconfig:"k_service"`
71+
72+
// Apply response wrapping before sending it back to the client.
73+
// Common case - AWS Lambda functions usually returns data formatted for API Gateway service.
74+
// Set "RESPONSE_WRAPPER: API_GATEWAY" and receive events as if they were processed by API Gateway.
75+
// Opposite scenario - return responses in CloudEvent format: "RESPONSE_WRAPPER: CLOUDEVENTS"
76+
// NOTE: Response wrapper does both encoding and decoding depending on the type. We should consider
77+
// separating wrappers by their function.
78+
ResponseWrapper string `envconfig:"response_wrapper"`
79+
}
80+
81+
type message struct {
82+
id string
83+
deadline int64
84+
data []byte
85+
statusCode int
86+
}
87+
88+
type responseWrapper struct {
89+
http.ResponseWriter
90+
StatusCode int
91+
Body []byte
92+
}
93+
8794
func (rw *responseWrapper) Write(data []byte) (int, error) {
8895
rw.Body = data
8996
return len(data), nil
@@ -227,16 +234,24 @@ func responseHandler(w http.ResponseWriter, r *http.Request) {
227234
}
228235

229236
func (s *Specification) mapEvent(h http.Handler) http.Handler {
237+
var mapper events.Mapper
238+
239+
switch s.ResponseWrapper {
240+
case "API_GATEWAY":
241+
mapper = apigateway.NewMapper()
242+
case "CLOUDEVENTS":
243+
mapper = cloudevents.NewMapper(s.Service)
244+
default:
245+
mapper = passthrough.NewMapper()
246+
}
247+
230248
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
231-
rw := responseWrapper{w, 200, []byte{}}
232-
switch s.EventType {
233-
case "API_GATEWAY":
234-
apiGateway.Request(r)
235-
h.ServeHTTP(&rw, r)
236-
apiGateway.Response(w, rw.StatusCode, rw.Body)
237-
default:
238-
h.ServeHTTP(w, r)
249+
rw := responseWrapper{
250+
ResponseWriter: w,
239251
}
252+
mapper.Request(r)
253+
h.ServeHTTP(&rw, r)
254+
mapper.Response(w, rw.StatusCode, rw.Body)
240255
})
241256
}
242257

@@ -266,23 +281,21 @@ func api() error {
266281
}
267282

268283
func main() {
269-
tasks = make(chan message, 100)
270-
results = make(map[string]chan message)
271-
defer close(tasks)
272-
273284
var spec Specification
274-
275-
err := envconfig.Process("", &spec)
276-
if err != nil {
285+
if err := envconfig.Process("", &spec); err != nil {
277286
log.Fatalf("Cannot process env variables: %v", err)
278287
}
279288
log.Printf("%+v\n", spec)
280289

281-
log.Println("Setup app env")
290+
log.Println("Setting up runtime env")
282291
if err := spec.setupEnv(); err != nil {
283292
log.Fatalf("Cannot setup runime env: %v", err)
284293
}
285294

295+
tasks = make(chan message, 100)
296+
results = make(map[string]chan message)
297+
defer close(tasks)
298+
286299
log.Println("Starting API")
287300
go func() {
288301
if err := api(); err != nil {
@@ -307,7 +320,7 @@ func main() {
307320
taskHandler := http.HandlerFunc(spec.newTask)
308321
taskRouter.Handle("/", spec.mapEvent(taskHandler))
309322
log.Println("Listening...")
310-
err = http.ListenAndServe(":"+spec.ExternalAPIport, taskRouter)
323+
err := http.ListenAndServe(":"+spec.ExternalAPIport, taskRouter)
311324
if err != nil && err != http.ErrServerClosed {
312325
log.Fatalf("Runtime external API error: %v", err)
313326
}

pkg/events/apiGateway/mapper.go renamed to pkg/events/apigateway/mapper.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package apiGateway
1+
package apigateway
22

33
import (
44
"bytes"
@@ -11,7 +11,13 @@ import (
1111
"github.com/aws/aws-lambda-go/events"
1212
)
1313

14-
func Request(r *http.Request) {
14+
type APIGateway struct{}
15+
16+
func NewMapper() *APIGateway {
17+
return &APIGateway{}
18+
}
19+
20+
func (a *APIGateway) Request(r *http.Request) {
1521
event := events.APIGatewayProxyRequest{}
1622
body, err := ioutil.ReadAll(r.Body)
1723
if err != nil {
@@ -40,7 +46,7 @@ func Request(r *http.Request) {
4046
r.Body = ioutil.NopCloser(bytes.NewBuffer(js))
4147
}
4248

43-
func Response(w http.ResponseWriter, statusCode int, data []byte) (int, error) {
49+
func (a *APIGateway) Response(w http.ResponseWriter, statusCode int, data []byte) (int, error) {
4450
var js events.APIGatewayProxyResponse
4551
if err := json.Unmarshal(data, &js); err != nil {
4652
return 0, err

0 commit comments

Comments
 (0)