Skip to content

Commit

Permalink
Wasm sink example
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jun 20, 2024
1 parent 932aeb6 commit e85d154
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 46 deletions.
21 changes: 0 additions & 21 deletions server/templates/otoroshi/OTOROSHI_WASM_ROUTER/index.js

This file was deleted.

46 changes: 25 additions & 21 deletions server/templates/otoroshi/OTOROSHI_WASM_SINK/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
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
}
33 changes: 33 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_SINK/index.ts
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_SINK/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use extism_pdk::*;
use otoroshi_rust_types::types;

#[plugin_fn]
pub fn sink_matches(
Json(context): Json<types::WasmSinkContext>,
) -> FnResult<Json<types::WasmSinkMatchesResponse>> {
Ok(Json(types::WasmSinkMatchesResponse { result: true }))
}

#[plugin_fn]
pub fn sink_handle(
Json(context): Json<types::WasmSinkContext>,
) -> FnResult<Json<types::WasmSinkHandleResponse>> {
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,
}))
}
42 changes: 42 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_SINK/main.go
Original file line number Diff line number Diff line change
@@ -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() {}
8 changes: 4 additions & 4 deletions ui/src/templates/otoroshi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e85d154

Please sign in to comment.