Skip to content

Commit

Permalink
otoroshi wasm route matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jun 19, 2024
1 parent d7bfcb3 commit 932aeb6
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
34 changes: 14 additions & 20 deletions server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
let context = JSON.parse(Host.inputString())

Host.outputString(JSON.stringify({
result: context.request.headers.foo === "bar"
}))

return 0
}
16 changes: 16 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/index.ts
Original file line number Diff line number Diff line change
@@ -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;
}

10 changes: 10 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use extism_pdk::*;
use otoroshi_rust_types::types;

#[plugin_fn]
pub fn execute(Json(context): Json<types::WasmMatchRouteContext>) ->
FnResult<Json<types::WasmMatchRouteResponse>> {
Ok(Json(WasmMatchRouteResponse {
result: context.request.headers.foo === "bar"
}))
}
27 changes: 27 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_ROUTE_MATCHER/main.go
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit 932aeb6

Please sign in to comment.