http-wasm defines HTTP functions implemented in WebAssembly. This repository includes http_handler ABI middleware for various HTTP server libraries written in Go.
The current maturity phase is early draft. Once this is integrated with coraza and dapr, we can begin discussions about compatability.
handler.Middleware
作为包装器
type middleware struct {
host handler.Host // host实现
runtime wazero.Runtime // 运行时
guestModule wazero.CompiledModule //编译的模式
moduleConfig wazero.ModuleConfig //模块配置
guestConfig []byte // wasm二进制文件
logger api.Logger
pool sync.Pool
features handler.Features
instanceCounter uint64
}
handler.Host
接口增加函数
// GetTemplate supports the WebAssembly function export FuncGetTemplate.
GetTemplate(ctx context.Context) string
// SetTemplate supports the WebAssembly function export FuncSetTemplate.
SetTemplate(ctx context.Context, template string)
- 默认空实现
func (UnimplementedHost) GetTemplate(context.Context) string { return "" }
func (UnimplementedHost) SetTemplate(context.Context, string)
- 在
handler.middleware.go#692
文件中,声明导入函数,这样在guest/wasm中就能调用了,都是通过指针地址进行数据交换的.
//getTemplate
NewFunctionBuilder().
WithGoModuleFunction(wazeroapi.GoModuleFunc(m.getTemplate), []wazeroapi.ValueType{i32, i32}, []wazeroapi.ValueType{i32}).
WithParameterNames("buf", "buf_limit").Export(handler.FuncGetTemplate).
NewFunctionBuilder().
//setTemplate
WithGoModuleFunction(wazeroapi.GoModuleFunc(m.setTemplate), []wazeroapi.ValueType{i32, i32}, []wazeroapi.ValueType{}).
WithParameterNames("template", "template_len").Export(handler.FuncSetTemplate).