Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/no9/tide-rhai
Rhai scripts in tide servers
https://github.com/no9/tide-rhai
Last synced: about 2 months ago
JSON representation
Rhai scripts in tide servers
- Host: GitHub
- URL: https://github.com/no9/tide-rhai
- Owner: No9
- License: apache-2.0
- Created: 2021-03-06T17:41:58.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-04-05T23:24:18.000Z (over 3 years ago)
- Last Synced: 2024-10-14T08:50:11.798Z (2 months ago)
- Language: Rust
- Size: 686 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
tide-rhai
A scripting engine for tide.
## Overview
This component provides the ability to run [rhai scripts](https://github.com/rhaiscript/rhai) to process http requests in [tide](https://github.com/http-rs/tide).
Currently it only supprts modifying the messages but additional features such as a http client are being considered.## Install
```
$ cargo add tide-rhai
```## Example
Create a tide server that points to a directory containing rhai scripts.
```rust
use tide_rhai::RhaiDir;
#[async_std::main]
async fn main() -> Result<(), std::io::Error> {
tide::log::start();
let mut app = tide::new();
app.at("/*")
.get(RhaiDir::new("/*", "./").unwrap());
app.listen("127.0.0.1:8080").await?;
Ok(())
}
```
The first parameter for new is the prefix and should be mapped to the `at` parameter.
The second is the folder with the rhai scripts inCreat a rhai script called headers.rhai that selects a header and returns it in a JSON Message.
Note it doesn't have to be called .rhai but [VS Code](https://marketplace.visualstudio.com/items?itemName=Aster.vscode-rhai) has suppport for that file extention.```rust
let obj = #{};
obj.message = "Is this acceptable?" + ctx.headers["accept"];
obj
```
Here we are using the `headers` property of the context object. If this was a POST then the `ctx` object would also contain a `data` property with the JSON that has been sent to the server.When you now run to https://localhost:8080/headers.rhai you should see the following:
```json
{"message":"Is this acceptable?text/html,application/xhtml+xml,application/xml;q=0.9,
image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"}
```
This example can also be ran by cloning this repository and running```bash
$ cargo run --example samples
```Then browse to:
http://localhost:8080/helloworld.rhai
http://localhost:8080/headers.rhai
http://localhost:8080/fetch.rhai## Todo List
- [x] Logging - Integrate with tide logging system
- [x] Http Client - A fetch-like API (alpha implementation known to lock in self referencing scenarios)
- [ ] Benchmarks - Set of scripts to manage regressions
- [ ] Observability - Support for BPF/DTrace probes. Maybe flamegraphs or statemaps
- [ ] Module System - Rhai supports modules but doesn't have a modeue system.