|
| 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