tide-rhai 0.0.1

A scritping component for tide.
Documentation

Installation

$ cargo add tide-rhai

Overview

This component provides the ability to run rhai scripts to process http requests in tide. Currently it only supprts modifying the messages but additional are being considered.

Install

$ cargo add tide-rhai

Example

Create a tide server that points to a directory containing tide scripts.

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 so map to the at parameter. The second is the folder with the rhai scripts in

Creat a rhai script called headers.rhai that selects a header and returns it in a JSON Message

let obj = #{};
obj.message = "Is this acceptable?" + ctx.headers["accept"];
obj

When you now run to https://localhost:8080/headers.rhai you should see the following:

{"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

$ cargo run --example helloworld