diff --git a/server/templates/otoroshi/OTOROSHI_WASM_ROUTER/index.js b/server/templates/otoroshi/OTOROSHI_WASM_ROUTER/index.js deleted file mode 100755 index c54cb54..0000000 --- a/server/templates/otoroshi/OTOROSHI_WASM_ROUTER/index.js +++ /dev/null @@ -1,21 +0,0 @@ -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 diff --git a/server/templates/otoroshi/OTOROSHI_WASM_SINK/index.js b/server/templates/otoroshi/OTOROSHI_WASM_SINK/index.js index c54cb54..7e63900 100755 --- a/server/templates/otoroshi/OTOROSHI_WASM_SINK/index.js +++ b/server/templates/otoroshi/OTOROSHI_WASM_SINK/index.js @@ -1,21 +1,25 @@ -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 +export function sink_matches() { + // const context = JSON.parse(Host.inputString()) + + Host.outputString(JSON.stringify({ + result: true + })) + + return 0 +} + +export function sink_handle() { + const context = JSON.parse(Host.inputString()) + + Host.outputString(JSON.stringify({ + status: 200, + headers: { + 'Content-Type': 'application/json' + }, + body_json: { + "WASM_SINK_RESPONSE": `Unknown path and domain for ${context.request.path}` + } + })) + + return 0 +} \ No newline at end of file diff --git a/server/templates/otoroshi/OTOROSHI_WASM_SINK/index.ts b/server/templates/otoroshi/OTOROSHI_WASM_SINK/index.ts new file mode 100755 index 0000000..252f510 --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_SINK/index.ts @@ -0,0 +1,33 @@ +import { WasmSinkContext, WasmSinkMatchesResponse, WasmSinkHandleResponse } from 'otoroshi-ts-types'; + +export declare var Host: any; + +export function sink_matches() { + // const context = JSON.parse(Host.inputString()) as WasmSinkContext; + + const out: WasmSinkMatchesResponse = { + result: true + } + + Host.outputString(JSON.stringify(out)); + + return 0; +} + +export function sink_handle() { + const context = JSON.parse(Host.inputString()) as WasmSinkContext; + + const out: WasmSinkHandleResponse = { + status: 200, + headers: { + 'Content-Type': 'application/json' + }, + body_json: { + "WASM_SINK_RESPONSE": `Unknown path and domain for ${context.request.path}` + } + } + + Host.outputString(JSON.stringify(out)) + + return 0 +} \ No newline at end of file diff --git a/server/templates/otoroshi/OTOROSHI_WASM_SINK/lib.rs b/server/templates/otoroshi/OTOROSHI_WASM_SINK/lib.rs new file mode 100755 index 0000000..6e9b358 --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_SINK/lib.rs @@ -0,0 +1,28 @@ +use extism_pdk::*; +use otoroshi_rust_types::types; + +#[plugin_fn] +pub fn sink_matches( + Json(context): Json, +) -> FnResult> { + Ok(Json(types::WasmSinkMatchesResponse { result: true })) +} + +#[plugin_fn] +pub fn sink_handle( + Json(context): Json, +) -> FnResult> { + let path: String = context.request.path; + + Ok(Json(types::WasmSinkHandleResponse { + status: 404, + body_str: Some(format!( + r#"{{ "WASM_SINK_RESPONSE": "Unknown path and domain for {}" }}"#, + path + )), + headers: context.request.headers, + body_base64: None, + body_bytes: None, + body_json: None, + })) +} diff --git a/server/templates/otoroshi/OTOROSHI_WASM_SINK/main.go b/server/templates/otoroshi/OTOROSHI_WASM_SINK/main.go new file mode 100755 index 0000000..0dcd089 --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_SINK/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "github.com/buger/jsonparser" + "github.com/extism/go-pdk" + // "github.com/MAIF/otoroshi-go-types" +) + +//export sink_matches +func sink_matches() int32 { + output := `{ "result": true }` + mem := pdk.AllocateString(output) + pdk.OutputMemory(mem) + + return 0 +} + +//export sink_handle +func sink_handle() int32 { + input := pdk.Input() + + var path, err = jsonparser.GetString(input, "request", "path") + + if err != nil { + } + + output := `{ + "status": 200, + "headers": { + "Content-Type": "application/json" + }, + "body_json": { + "WASM_SINK_RESPONSE": "Unknown path and domain for ` + path + `" + } + }` + mem := pdk.AllocateString(output) + pdk.OutputMemory(mem) + + return 0 +} + +func main() {} diff --git a/ui/src/templates/otoroshi.js b/ui/src/templates/otoroshi.js index 995e61f..39b898a 100644 --- a/ui/src/templates/otoroshi.js +++ b/ui/src/templates/otoroshi.js @@ -23,10 +23,10 @@ export const OTOROSHI_TEMPLATES = { key: "OTOROSHI_WASM_SINK", description: "Handle unmatched requests with a wasm plugin" }, - "Router": { - key: "OTOROSHI_WASM_ROUTER", - description: "Can decide for routing with a wasm plugin" - }, + // "Router": { + // key: "OTOROSHI_WASM_ROUTER", + // description: "Can decide for routing with a wasm plugin" + // }, "Pre route": { key: "OTOROSHI_WASM_PRE_ROUTE", description: "This plugin can be used to use a wasm plugin as in pre-route phase"