-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
105 lines (85 loc) · 2.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"github.com/kong/proxy-wasm-go-filter-template/config"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
func main() {
proxywasm.SetVMContext(&myPluginVMContext{})
}
type (
myPluginVMContext struct {
types.DefaultVMContext
}
myPluginContext struct {
conf config.Config
types.DefaultPluginContext
}
myPluginHTTPContext struct {
conf *config.Config
types.DefaultHttpContext
}
)
// -----------------------------------------------------------------------------
// VM Context
// -----------------------------------------------------------------------------
//
// func (*myPluginVMContext) OnVMStart(vmConfigurationSize int) types.OnVMStartStatus {
// return types.OnVMStartStatusOK
// }
func (*myPluginVMContext) NewPluginContext(vmID uint32) types.PluginContext {
return &myPluginContext{}
}
// -----------------------------------------------------------------------------
// Plugin Context
// -----------------------------------------------------------------------------
func (ctx *myPluginContext) OnPluginStart(confSize int) types.OnPluginStartStatus {
data, err := proxywasm.GetPluginConfiguration()
if err != nil && err != types.ErrorStatusNotFound {
proxywasm.LogCriticalf("error reading plugin configuration: %v", err)
return types.OnPluginStartStatusFailed
}
err = config.Load(data, &ctx.conf)
if err != nil {
proxywasm.LogCriticalf("error parsing plugin configuration: %v", err)
return types.OnPluginStartStatusFailed
}
return types.OnPluginStartStatusOK
}
func (ctx *myPluginContext) NewHttpContext(pluginID uint32) types.HttpContext {
return &myPluginHTTPContext{
conf: &ctx.conf,
}
}
// func (ctx *myPluginContext) OnTick() {
// proxywasm.LogInfo("[plugin] OnTick")
// }
//
// func (*myPluginContext) OnPluginDone() bool {
// return true
// }
//
// -----------------------------------------------------------------------------
// HTTP Context
// -----------------------------------------------------------------------------
// func (*myPluginHTTPContext) OnHttpRequestHeaders(int, bool) types.Action {
// return types.ActionContinue
// }
//
// func (*myPluginHTTPContext) OnHttpRequestBody(int, bool) types.Action {
// return types.ActionContinue
// }
func (ctx *myPluginHTTPContext) OnHttpResponseHeaders(int, bool) types.Action {
if ctx.conf.MyGreeting != "" {
greeting := fmt.Sprintf("%v", ctx.conf.MyGreeting)
proxywasm.AddHttpResponseHeader("X-Greeting", greeting)
}
return types.ActionContinue
}
// func (*myPluginHTTPContext) OnHttpResponseBody(int, bool) types.Action {
// return types.ActionContinue
// }
//
// func (*myPluginHTTPContext) OnHttpStreamDone() {
// }