Skip to content

Commit 7d4d115

Browse files
walves-citleonm1
authored andcommitted
feat: create a Go sample for the docs plugin config
1 parent a914f94 commit 7d4d115

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

plugins/samples/docs_plugin_config/BUILD

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
load("//:plugins.bzl", "proxy_wasm_plugin_cpp", "proxy_wasm_plugin_rust", "proxy_wasm_tests")
1+
load(
2+
"//:plugins.bzl",
3+
"proxy_wasm_plugin_cpp",
4+
"proxy_wasm_plugin_go",
5+
"proxy_wasm_plugin_rust",
6+
"proxy_wasm_tests"
7+
)
28

39
licenses(["notice"]) # Apache 2
410

@@ -16,11 +22,17 @@ proxy_wasm_plugin_cpp(
1622
srcs = ["plugin.cc"],
1723
)
1824

25+
proxy_wasm_plugin_go(
26+
name = "plugin_go.wasm",
27+
srcs = ["plugin.go"],
28+
)
29+
1930
proxy_wasm_tests(
2031
name = "tests",
2132
config = ":tests.config",
2233
plugins = [
2334
":plugin_cpp.wasm",
35+
":plugin_go.wasm",
2436
":plugin_rust.wasm",
2537
],
2638
tests = ":tests.textpb",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START serviceextensions_plugin_docs_plugin_config]
16+
package main
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm"
22+
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
23+
)
24+
25+
func main() {}
26+
func init() {
27+
proxywasm.SetVMContext(&vmContext{})
28+
}
29+
30+
type vmContext struct {
31+
types.DefaultVMContext
32+
}
33+
34+
type pluginContext struct {
35+
types.DefaultPluginContext
36+
secret string
37+
}
38+
39+
type httpContext struct {
40+
types.DefaultHttpContext
41+
pluginContext *pluginContext
42+
}
43+
44+
func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext {
45+
return &pluginContext{}
46+
}
47+
48+
func (ctx *pluginContext) OnPluginStart(int) types.OnPluginStartStatus {
49+
config, err := proxywasm.GetPluginConfiguration()
50+
if err != nil {
51+
proxywasm.LogErrorf("Error reading the configuration: %v", err)
52+
return types.OnPluginStartStatusFailed
53+
}
54+
ctx.secret = string(config)
55+
return types.OnPluginStartStatusOK
56+
}
57+
58+
func (ctx *pluginContext) NewHttpContext(uint32) types.HttpContext {
59+
return &httpContext{pluginContext: ctx}
60+
}
61+
62+
func (ctx *pluginContext) Secret() string {
63+
return ctx.secret
64+
}
65+
66+
func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
67+
defer func() {
68+
err := recover()
69+
if err != nil {
70+
proxywasm.SendHttpResponse(500, [][2]string{}, []byte(fmt.Sprintf("%v", err)), 0)
71+
}
72+
}()
73+
// Use secret here...
74+
proxywasm.LogInfof("secret: %v", ctx.pluginContext.Secret())
75+
return types.ActionContinue
76+
}
77+
78+
// [END serviceextensions_plugin_docs_plugin_config]

0 commit comments

Comments
 (0)