diff --git a/plugins/samples/log_query/BUILD b/plugins/samples/log_query/BUILD index 4f5d5c27..2e8fabcf 100644 --- a/plugins/samples/log_query/BUILD +++ b/plugins/samples/log_query/BUILD @@ -1,4 +1,4 @@ -load("//:plugins.bzl", "proxy_wasm_plugin_cpp", "proxy_wasm_plugin_rust", "proxy_wasm_tests") +load("//:plugins.bzl", "proxy_wasm_plugin_cpp", "proxy_wasm_plugin_go", "proxy_wasm_plugin_rust", "proxy_wasm_tests") licenses(["notice"]) # Apache 2 @@ -21,10 +21,16 @@ proxy_wasm_plugin_cpp( ], ) +proxy_wasm_plugin_go( + name = "plugin_go.wasm", + srcs = ["plugin.go"], +) + proxy_wasm_tests( name = "tests", plugins = [ ":plugin_cpp.wasm", + ":plugin_go.wasm", ":plugin_rust.wasm", ], tests = ":tests.textpb", diff --git a/plugins/samples/log_query/plugin.go b/plugins/samples/log_query/plugin.go new file mode 100644 index 00000000..ac056090 --- /dev/null +++ b/plugins/samples/log_query/plugin.go @@ -0,0 +1,77 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// [START serviceextensions_plugin_log_query] +package main + +import ( + "fmt" + "net/url" + + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm" + "github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types" +) + +func main() {} +func init() { + proxywasm.SetVMContext(&vmContext{}) +} + +type vmContext struct { + types.DefaultVMContext +} + +type pluginContext struct { + types.DefaultPluginContext +} + +type httpContext struct { + types.DefaultHttpContext +} + +func (*vmContext) NewPluginContext(contextID uint32) types.PluginContext { + return &pluginContext{} +} + +func (*pluginContext) NewHttpContext(uint32) types.HttpContext { + return &httpContext{} +} + +func (ctx *httpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action { + defer func() { + err := recover() + if err != nil { + proxywasm.SendHttpResponse(500, [][2]string{}, []byte(fmt.Sprintf("%v", err)), 0) + } + }() + path, err := proxywasm.GetHttpRequestHeader(":path") + if err != types.ErrorStatusNotFound { + if err != nil { + panic(err) + } + u, err := url.Parse(path) + if err != nil { + panic(err) + } + token := u.Query().Get("token") + if token == "" { + token = "" + } + proxywasm.LogInfof("token: %s", token) + } + + return types.ActionContinue +} + +// [END serviceextensions_plugin_log_query]