-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6024ea
commit d7bfcb3
Showing
8 changed files
with
123 additions
and
23 deletions.
There are no files selected for viewing
38 changes: 18 additions & 20 deletions
38
server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
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({ | ||
...context, | ||
status: 200, | ||
headers: { | ||
...context.otoroshi_response.headers, | ||
OTOROSHI_WASM_PLUGIN_ID: "OTOROSHI_WASM_RESPONSE_TRANSFORMER", | ||
"Content-Type": "application/json" | ||
}, | ||
body_json: { | ||
foo: "bar" | ||
}, | ||
// cookies | ||
})) | ||
|
||
return 0 | ||
} |
23 changes: 23 additions & 0 deletions
23
server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { WasmRequestTransformerContext, WasmTransformerResponse } from 'otoroshi-ts-types'; | ||
|
||
export declare var Host: any; | ||
|
||
export function execute() { | ||
let context = JSON.parse(Host.inputString()) as WasmRequestTransformerContext; | ||
|
||
const error: WasmTransformerResponse = { | ||
...context, | ||
status: 200, | ||
headers: { | ||
...context.otoroshi_request.headers, | ||
OTOROSHI_WASM_PLUGIN_ID: "OTOROSHI_WASM_RESPONSE_TRANSFORMER", | ||
"Content-Type": "application/json" | ||
}, | ||
body_json: { | ||
foo: "bar" | ||
} | ||
} | ||
Host.outputString(JSON.stringify(error)); | ||
|
||
return 0 | ||
} |
27 changes: 27 additions & 0 deletions
27
server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use extism_pdk::*; | ||
use otoroshi_rust_types::types; | ||
|
||
#[plugin_fn] | ||
pub fn execute( | ||
Json(context): Json<types::WasmResponseTransformerContext>, | ||
) -> FnResult<Json<types::WasmResponse>> { | ||
let mut out_headers = context.request.headers; | ||
out_headers.insert( | ||
"OTOROSHI_WASM_PLUGIN_ID".to_string(), | ||
"OTOROSHI_WASM_RESPONSE_TRANSFORMER".to_string(), | ||
); | ||
out_headers.insert("Content-Type".to_string(), "text/plain".to_string()); | ||
|
||
let out = types::WasmResponse { | ||
status: Some(200), | ||
error: None, | ||
body_str: Some("{ \"foo\": \"bar\" }".to_string()), | ||
headers: out_headers, | ||
cookies: serde_json::Value::Null, | ||
body_base64: None, | ||
body_bytes: None, | ||
body_json: None, | ||
}; | ||
|
||
Ok(Json(out)) | ||
} |
51 changes: 51 additions & 0 deletions
51
server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/buger/jsonparser" | ||
"github.com/extism/go-pdk" | ||
// "github.com/MAIF/otoroshi-go-types" | ||
) | ||
|
||
//export execute | ||
func execute() int32 { | ||
input := pdk.Input() | ||
|
||
var headers, dataType, offset, err = jsonparser.Get(input, "request", "headers") | ||
|
||
_ = dataType | ||
_ = offset | ||
|
||
var outHeaders map[string]interface{} | ||
|
||
// Unmarshal the JSON data into the map | ||
err = json.Unmarshal(headers, &outHeaders) | ||
if err != nil { | ||
|
||
} | ||
|
||
outHeaders["Content-Type"] = "application/json" | ||
outHeaders["OTOROSHI_WASM_PLUGIN_ID"] = "OTOROSHI_WASM_RESPONSE_TRANSFORMER" | ||
|
||
if err != nil { | ||
} | ||
|
||
jsonHeaders, marshallingError := json.Marshal(outHeaders) | ||
if marshallingError != nil { | ||
|
||
} | ||
|
||
output := `{ | ||
"status": 200, | ||
"headers": ` + string(jsonHeaders) + `, | ||
"body_json": { "foo": "bar" } | ||
}` | ||
|
||
mem := pdk.AllocateString(output) | ||
pdk.OutputMemory(mem) | ||
|
||
return 0 | ||
} | ||
|
||
func main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters