Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Clean ups example codes (#445)
Browse files Browse the repository at this point in the history
While there, this also updates the formatter/linter dependencies as well
as the year in the license.

Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored May 28, 2024
1 parent 021aa9c commit 9456a1b
Show file tree
Hide file tree
Showing 59 changed files with 212 additions and 166 deletions.
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
goimports := golang.org/x/tools/cmd/goimports@v0.7.0
golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2
goimports := golang.org/x/tools/cmd/goimports@v0.21.0
golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.59.0


.PHONY: build.example
Expand Down Expand Up @@ -40,7 +40,10 @@ run:

.PHONY: lint
lint:
@go run $(golangci_lint) run
@find . -name "go.mod" \
| grep go.mod \
| xargs -I {} bash -c 'dirname {}' \
| xargs -I {} bash -c 'echo "=> {}"; cd {}; go run $(golangci_lint) run; '

.PHONY: format
format:
Expand Down
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
proxy-wasm-go-sdk
Copyright 2020-2021 Tetrate
Copyright 2020-2024 Tetrate
2 changes: 1 addition & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Tetrate
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion e2e/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Tetrate
// Copyright 2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
10 changes: 6 additions & 4 deletions examples/dispatch_call_on_tick/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Tetrate
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,17 +27,19 @@ func main() {
proxywasm.SetVMContext(&vmContext{})
}

// vmContext implements types.VMContext.
type vmContext struct {
// Embed the default VM context here,
// so that we don't need to reimplement all the methods.
types.DefaultVMContext
}

// Override types.DefaultVMContext.
// NewPluginContext implements types.VMContext.
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &pluginContext{contextID: contextID}
}

// pluginContext implements types.PluginContext.
type pluginContext struct {
// Embed the default plugin context here,
// so that we don't need to reimplement all the methods.
Expand All @@ -47,7 +49,7 @@ type pluginContext struct {
cnt int
}

// Override types.DefaultPluginContext.
// OnPluginStart implements types.PluginContext.
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
proxywasm.LogCriticalf("failed to set tick period: %v", err)
Expand Down Expand Up @@ -75,7 +77,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
return types.OnPluginStartStatusOK
}

// Override types.DefaultPluginContext.
// OnTick implements types.PluginContext.
func (ctx *pluginContext) OnTick() {
headers := [][2]string{
{":method", "GET"}, {":authority", "some_authority"}, {"accept", "*/*"},
Expand Down
13 changes: 7 additions & 6 deletions examples/foreign_call_on_tick/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Tetrate
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,26 +27,27 @@ func main() {
proxywasm.SetVMContext(&vmContext{})
}

// vmContext implements types.VMContext.
type vmContext struct {
// Embed the default VM context here,
// so that we don't need to reimplement all the methods.
types.DefaultVMContext
}

// Override types.DefaultVMContext.
// NewPluginContext implements types.VMContext.
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &pluginContext{}
}

// pluginContext implements types.PluginContext.
type pluginContext struct {
// Embed the default plugin context here,
// so that we don't need to reimplement all the methods.
types.DefaultPluginContext
contextID uint32
callNum uint32
callNum uint32
}

// Override types.DefaultPluginContext.
// OnPluginStart implements types.PluginContext.
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
if err := proxywasm.SetTickPeriodMilliSeconds(tickMilliseconds); err != nil {
proxywasm.LogCriticalf("failed to set tick period: %v", err)
Expand All @@ -56,7 +57,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
return types.OnPluginStartStatusOK
}

// Override types.DefaultPluginContext.
// OnTick implements types.PluginContext.
func (ctx *pluginContext) OnTick() {
ctx.callNum++
ret, err := proxywasm.CallForeignFunction("compress", []byte("hello world!"))
Expand Down
13 changes: 7 additions & 6 deletions examples/helloworld/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Tetrate
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,25 +28,26 @@ func main() {
proxywasm.SetVMContext(&vmContext{})
}

// vmContext implements types.VMContext.
type vmContext struct {
// Embed the default VM context here,
// so that we don't need to reimplement all the methods.
types.DefaultVMContext
}

// Override types.DefaultVMContext.
// NewPluginContext implements types.VMContext.
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &helloWorld{}
}

// helloWorld implements types.PluginContext.
type helloWorld struct {
// Embed the default plugin context here,
// so that we don't need to reimplement all the methods.
types.DefaultPluginContext
contextID uint32
}

// Override types.DefaultPluginContext.
// OnPluginStart implements types.PluginContext.
func (ctx *helloWorld) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
rand.Seed(time.Now().UnixNano())

Expand All @@ -58,12 +59,12 @@ func (ctx *helloWorld) OnPluginStart(pluginConfigurationSize int) types.OnPlugin
return types.OnPluginStartStatusOK
}

// Override types.DefaultPluginContext.
// OnTick implements types.PluginContext.
func (ctx *helloWorld) OnTick() {
t := time.Now().UnixNano()
proxywasm.LogInfof("It's %d: random value: %d", t, rand.Uint64())
proxywasm.LogInfof("OnTick called")
}

// Override types.DefaultPluginContext.
// NewHttpContext implements types.PluginContext.
func (*helloWorld) NewHttpContext(uint32) types.HttpContext { return &types.DefaultHttpContext{} }
20 changes: 12 additions & 8 deletions examples/http_auth_random/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Tetrate
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,36 +27,39 @@ func main() {
proxywasm.SetVMContext(&vmContext{})
}

// vmContext implements types.VMContext.
type vmContext struct {
// Embed the default VM context here,
// so that we don't need to reimplement all the methods.
types.DefaultVMContext
}

// Override types.DefaultVMContext.
// NewPluginContext implements types.VMContext.
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &pluginContext{}
}

// pluginContext implements types.PluginContext.
type pluginContext struct {
// Embed the default plugin context here,
// so that we don't need to reimplement all the methods.
types.DefaultPluginContext
}

// Override types.DefaultPluginContext.
// NewHttpContext implements types.PluginContext.
func (*pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
return &httpAuthRandom{contextID: contextID}
}

// httpAuthRandom implements types.HttpContext.
type httpAuthRandom struct {
// Embed the default http context here,
// so that we don't need to reimplement all the methods.
types.DefaultHttpContext
contextID uint32
}

// Override types.DefaultHttpContext.
// OnHttpRequestHeaders implements types.HttpContext.
func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
hs, err := proxywasm.GetHttpRequestHeaders()
if err != nil {
Expand All @@ -77,6 +80,7 @@ func (ctx *httpAuthRandom) OnHttpRequestHeaders(numHeaders int, endOfStream bool
return types.ActionPause
}

// httpCallResponseCallback is a callback function when the http call response is received after dispatching.
func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
hs, err := proxywasm.GetHttpCallResponseHeaders()
if err != nil {
Expand All @@ -91,20 +95,20 @@ func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
b, err := proxywasm.GetHttpCallResponseBody(0, bodySize)
if err != nil {
proxywasm.LogCriticalf("failed to get response body: %v", err)
proxywasm.ResumeHttpRequest()
_ = proxywasm.ResumeHttpRequest()
return
}

s := fnv.New32a()
if _, err := s.Write(b); err != nil {
proxywasm.LogCriticalf("failed to calculate hash: %v", err)
proxywasm.ResumeHttpRequest()
_ = proxywasm.ResumeHttpRequest()
return
}

if s.Sum32()%2 == 0 {
proxywasm.LogInfo("access granted")
proxywasm.ResumeHttpRequest()
_ = proxywasm.ResumeHttpRequest()
return
}

Expand All @@ -114,6 +118,6 @@ func httpCallResponseCallback(numHeaders, bodySize, numTrailers int) {
{"powered-by", "proxy-wasm-go-sdk!!"},
}, []byte(body), -1); err != nil {
proxywasm.LogErrorf("failed to send local response: %v", err)
proxywasm.ResumeHttpRequest()
_ = proxywasm.ResumeHttpRequest()
}
}
22 changes: 13 additions & 9 deletions examples/http_body/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020-2021 Tetrate
// Copyright 2020-2024 Tetrate
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,33 +29,35 @@ func main() {
proxywasm.SetVMContext(&vmContext{})
}

// vmContext implements types.VMContext.
type vmContext struct {
// Embed the default VM context here,
// so that we don't need to reimplement all the methods.
types.DefaultVMContext
}

// Override types.DefaultVMContext.
// NewPluginContext implements types.VMContext.
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
return &pluginContext{}
}

// pluginContext implements types.PluginContext.
type pluginContext struct {
// Embed the default plugin context here,
// so that we don't need to reimplement all the methods.
types.DefaultPluginContext
shouldEchoBody bool
}

// Override types.DefaultPluginContext.
// NewHttpContext implements types.PluginContext.
func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
if ctx.shouldEchoBody {
return &echoBodyContext{}
}
return &setBodyContext{}
}

// Override types.DefaultPluginContext.
// OnPluginStart implements types.PluginContext.
func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPluginStartStatus {
data, err := proxywasm.GetPluginConfiguration()
if err != nil {
Expand All @@ -65,6 +67,7 @@ func (ctx *pluginContext) OnPluginStart(pluginConfigurationSize int) types.OnPlu
return types.OnPluginStartStatusOK
}

// setBodyContext implements types.HttpContext.
type setBodyContext struct {
// Embed the default root http context here,
// so that we don't need to reimplement all the methods.
Expand All @@ -73,7 +76,7 @@ type setBodyContext struct {
bufferOperation string
}

// Override types.DefaultHttpContext.
// OnHttpRequestHeaders implements types.HttpContext.
func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
mode, err := proxywasm.GetHttpRequestHeader("buffer-replace-at")
if err == nil && mode == "response" {
Expand Down Expand Up @@ -104,7 +107,7 @@ func (ctx *setBodyContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool
return types.ActionContinue
}

// Override types.DefaultHttpContext.
// OnHttpRequestBody implements types.HttpContext.
func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
if ctx.modifyResponse {
return types.ActionContinue
Expand Down Expand Up @@ -137,7 +140,7 @@ func (ctx *setBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) typ
return types.ActionContinue
}

// Override types.DefaultHttpContext.
// OnHttpResponseHeaders implements types.HttpContext.
func (ctx *setBodyContext) OnHttpResponseHeaders(numHeaders int, endOfStream bool) types.Action {
if !ctx.modifyResponse {
return types.ActionContinue
Expand All @@ -151,7 +154,7 @@ func (ctx *setBodyContext) OnHttpResponseHeaders(numHeaders int, endOfStream boo
return types.ActionContinue
}

// Override types.DefaultHttpContext.
// OnHttpResponseBody implements types.HttpContext.
func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action {
if !ctx.modifyResponse {
return types.ActionContinue
Expand Down Expand Up @@ -184,14 +187,15 @@ func (ctx *setBodyContext) OnHttpResponseBody(bodySize int, endOfStream bool) ty
return types.ActionContinue
}

// echoBodyContext implements types.HttpContext.
type echoBodyContext struct {
// Embed the default plugin context
// so that you don't need to reimplement all the methods by yourself.
types.DefaultHttpContext
totalRequestBodySize int
}

// Override types.DefaultHttpContext.
// OnHttpRequestBody implements types.HttpContext.
func (ctx *echoBodyContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
ctx.totalRequestBodySize = bodySize
if !endOfStream {
Expand Down
Loading

0 comments on commit 9456a1b

Please sign in to comment.