Skip to content

Commit b014305

Browse files
committed
refine tinygo/httpfilter example
Signed-off-by: mathetake <[email protected]>
1 parent 7f618cc commit b014305

File tree

4 files changed

+50
-38
lines changed

4 files changed

+50
-38
lines changed

data/extension/init/templates/tinygo/envoy.filters.http/default/main.go

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,51 @@ import (
55
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
66
)
77

8+
var (
9+
requestCounterName = "proxy_wasm_go.request_counter"
10+
counter proxywasm.MetricCounter
11+
)
12+
813
func main() {
9-
proxywasm.SetNewHttpContext(newContext)
14+
proxywasm.SetNewRootContext(func(uint32) proxywasm.RootContext { return &context{} })
15+
proxywasm.SetNewHttpContext(func(uint32) proxywasm.HttpContext { return &context{} })
1016
}
1117

12-
type httpFilterContext struct {
13-
id uint32
14-
proxywasm.DefaultContext
15-
}
18+
type context struct{ proxywasm.DefaultContext }
1619

17-
func newContext(contextID uint32) proxywasm.HttpContext {
18-
return &httpFilterContext{id: contextID}
20+
func (ctx *context) OnVMStart(int) bool {
21+
var err error
22+
counter, err = proxywasm.DefineCounterMetric(requestCounterName)
23+
if err != nil {
24+
proxywasm.LogCritical("failed to initialize request counter: ", err.Error())
25+
}
26+
return true
1927
}
2028

21-
func (ctx *httpFilterContext) OnHttpRequestHeaders(int, bool) types.Action {
22-
// TODO:
29+
func (ctx *context) OnHttpRequestHeaders(int, bool) types.Action {
30+
hs, err := proxywasm.HostCallGetHttpRequestHeaders()
31+
if err != nil {
32+
proxywasm.LogCritical("failed to get request headers: ", err.Error())
33+
}
34+
35+
proxywasm.LogInfo("observing request headers")
36+
for _, h := range hs {
37+
proxywasm.LogInfo(h[0], ": ", h[1])
38+
}
39+
2340
return types.ActionContinue
2441
}
2542

26-
func (ctx *httpFilterContext) OnHttpResponseHeaders(int, bool) types.Action {
27-
// TODO:
43+
func (ctx *context) OnHttpResponseHeaders(int, bool) types.Action {
44+
if err := proxywasm.HostCallSetHttpResponseHeader("additional", "header"); err != nil {
45+
proxywasm.LogCritical(err.Error())
46+
}
2847
return types.ActionContinue
2948
}
49+
50+
func (ctx *context) OnDone() bool {
51+
if err := counter.Increment(1); err != nil {
52+
proxywasm.LogCritical("failed to increment request counter: ", err.Error())
53+
}
54+
return true
55+
}

data/extension/init/templates/tinygo/envoy.filters.http/default/main_test.go

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ import (
77
"github.com/stretchr/testify/require"
88

99
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
10+
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
1011
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
1112
)
1213

1314
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
14-
host, done := proxytest.NewHttpFilterHost(newContext)
15+
host, done := proxytest.NewHttpFilterHost(func(contextID uint32) proxywasm.HttpContext {
16+
return &context{}
17+
})
1518
defer done()
1619
id := host.InitContext()
1720

@@ -22,26 +25,9 @@ func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
2225
host.PutRequestHeaders(id, hs) // call OnHttpRequestHeaders
2326

2427
logs := host.GetLogs(types.LogLevelInfo)
25-
require.Greater(t, len(logs), 1)
28+
require.Greater(t, len(logs), 2)
2629

27-
assert.Equal(t, "request header: key2: value2", logs[len(logs)-1])
28-
assert.Equal(t, "request header: key1: value1", logs[len(logs)-2])
29-
}
30-
31-
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
32-
host, done := proxytest.NewHttpFilterHost(newContext)
33-
defer done()
34-
id := host.InitContext()
35-
36-
hs := [][2]string{
37-
{"key1", "value1"},
38-
{"key2", "value2"},
39-
}
40-
host.PutResponseHeaders(id, hs) // call OnHttpResponseHeaders
41-
42-
logs := host.GetLogs(types.LogLevelInfo)
43-
require.Greater(t, len(logs), 1)
44-
45-
assert.Equal(t, "response header: key2: value2", logs[len(logs)-1])
46-
assert.Equal(t, "response header: key1: value1", logs[len(logs)-2])
30+
assert.Equal(t, "key2: value2", logs[len(logs)-1])
31+
assert.Equal(t, "key1: value1", logs[len(logs)-2])
32+
assert.Equal(t, "observing request headers", logs[len(logs)-3])
4733
}

images/extension-builders/tinygo/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ ENV TINYGO_SDK_PATH=${GOMODCACHE}/${TINYGO_SDK_NAME}@${TINYGO_SDK_VERSION}
3333

3434
RUN mkdir -p ${TINYGO_SDK_PATH} && git clone https://${TINYGO_SDK_NAME} -b ${TINYGO_SDK_VERSION} ${TINYGO_SDK_PATH}
3535

36-
3736
COPY ./entrypoint.sh /usr/local/getenvoy/extension/builder/entrypoint.sh
3837
COPY ./tinygo/commands.sh /usr/local/getenvoy/extension/builder/commands.sh
3938
ENTRYPOINT ["/usr/local/getenvoy/extension/builder/entrypoint.sh"]

images/extension-builders/tinygo/commands.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@
2323
# Path relative to the workspace root to copy *.wasm file to.
2424
#########################################################################
2525
extension_build() {
26-
tinygo build -o "$1" -wasm-abi=generic -target wasm main.go
26+
exec tinygo build -o "$1" -wasm-abi=generic -target wasm main.go
2727
}
2828

2929
extension_test() {
30-
go test -tags=proxytest -v ./...
30+
exec go test -tags=proxytest -v ./...
3131
}
3232

3333
extension_clean() {
34-
rm main.wasm || true
35-
rm -rf "${GOCACHE}" "${XDG_CACHE_HOME}" "${GOMODCACHE}" || true
34+
rm /source/main.wasm || true
35+
go clean -modcache
36+
rm -rf "${GOCACHE}" "${XDG_CACHE_HOME}" || true
3637
}

0 commit comments

Comments
 (0)