From 932aeb6e834b40813e79d3b03224cc3e7a303f9a Mon Sep 17 00:00:00 2001 From: zwiterrion Date: Wed, 19 Jun 2024 15:36:15 +0200 Subject: [PATCH] otoroshi wasm route matcher --- .../index.ts | 4 +-- .../OTOROSHI_WASM_ROUTE_MATCHER/index.js | 34 ++++++++----------- .../OTOROSHI_WASM_ROUTE_MATCHER/index.ts | 16 +++++++++ .../OTOROSHI_WASM_ROUTE_MATCHER/lib.rs | 10 ++++++ .../OTOROSHI_WASM_ROUTE_MATCHER/main.go | 27 +++++++++++++++ 5 files changed, 69 insertions(+), 22 deletions(-) create mode 100755 server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.ts create mode 100755 server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/lib.rs create mode 100755 server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/main.go diff --git a/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts index e43e47d..e924d80 100755 --- a/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts +++ b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts @@ -1,9 +1,9 @@ -import { WasmRequestTransformerContext, WasmTransformerResponse } from 'otoroshi-ts-types'; +import { WasmResponseTransformerContext, WasmTransformerResponse } from 'otoroshi-ts-types'; export declare var Host: any; export function execute() { - let context = JSON.parse(Host.inputString()) as WasmRequestTransformerContext; + let context = JSON.parse(Host.inputString()) as WasmResponseTransformerContext; const error: WasmTransformerResponse = { ...context, diff --git a/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.js b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.js index c54cb54..52902e3 100755 --- a/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.js +++ b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.js @@ -1,21 +1,15 @@ +/* + The "matcher" is a tool that allows filtering a route during the routing phase. + In practice, you can create two routes that are identical from a frontend perspective + but have different "route matchers." + These matchers will select one route or the other based on a specific criterion. +*/ export function execute() { - let context = JSON.parse(Host.inputString()); - - if (context.request.headers["foo"] === "bar") { - const out = { - result: true - }; - Host.outputString(JSON.stringify(out)); - } else { - const error = { - result: false, - error: { - message: "you're not authorized", - status: 401 - } - }; - Host.outputString(JSON.stringify(error)); - } - - return 0; - } \ No newline at end of file + let context = JSON.parse(Host.inputString()) + + Host.outputString(JSON.stringify({ + result: context.request.headers.foo === "bar" + })) + + return 0 +} \ No newline at end of file diff --git a/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.ts b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.ts new file mode 100755 index 0000000..dcf6d22 --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.ts @@ -0,0 +1,16 @@ +import { WasmMatchRouteContext, WasmMatchRouteResponse } from 'otoroshi-ts-types'; + +export declare var Host: any; + +export function execute() { + const context = JSON.parse(Host.inputString()) as WasmMatchRouteContext; + + const out: WasmMatchRouteResponse = { + result: context.request.headers.foo === "bar" + } + + Host.outputString(JSON.stringify(out)); + + return 0; +} + diff --git a/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/lib.rs b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/lib.rs new file mode 100755 index 0000000..c6b8c55 --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/lib.rs @@ -0,0 +1,10 @@ +use extism_pdk::*; +use otoroshi_rust_types::types; + +#[plugin_fn] +pub fn execute(Json(context): Json) -> +FnResult> { + Ok(Json(WasmMatchRouteResponse { + result: context.request.headers.foo === "bar" + })) +} \ No newline at end of file diff --git a/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/main.go b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/main.go new file mode 100755 index 0000000..943f38f --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "strconv" + + "github.com/buger/jsonparser" + "github.com/extism/go-pdk" + // "github.com/MAIF/otoroshi-go-types" +) + +//export execute +func execute() int32 { + input := pdk.Input() + + var foo, err = jsonparser.GetString(input, "request", "headers", "foo") + + if err != nil { + } + + output := `{ "result": ` + strconv.FormatBool(foo == "bar") + `}` + mem := pdk.AllocateString(output) + pdk.OutputMemory(mem) + + return 0 +} + +func main() {}