add engine and rpc client, archive old code
This commit is contained in:
BIN
_archive/listen/.DS_Store
vendored
Normal file
BIN
_archive/listen/.DS_Store
vendored
Normal file
Binary file not shown.
2620
_archive/listen/Cargo.lock
generated
Normal file
2620
_archive/listen/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
26
_archive/listen/Cargo.toml
Normal file
26
_archive/listen/Cargo.toml
Normal file
@@ -0,0 +1,26 @@
|
||||
[package]
|
||||
name = "server"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
hyper = { version = "0.14", features = ["full"] }
|
||||
tokio = { version = "1", features = ["full"] }
|
||||
rhai = { version = "1.15.0", features = ["sync"] }
|
||||
rhai_system = { path = "../rhai_system" }
|
||||
bytes = "1"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tera = "1.0"
|
||||
once_cell = "1"
|
||||
rhai_tera = { path = "../rhai_tera" }
|
||||
calendar = { path = "../components/calendar" }
|
||||
|
||||
[[example]]
|
||||
name = "send_rhai_script"
|
||||
path = "examples/send_rhai_script.rs"
|
||||
required-features = []
|
||||
|
||||
[dev-dependencies] # Examples often use dev-dependencies, but reqwest is more like a direct dep for the example's purpose.
|
||||
reqwest = { version = "0.11", features = ["json", "rustls-tls"] } # Updated for async example client
|
||||
# tokio is already a main dependency, so the example can use it.
|
51
_archive/listen/README.md
Normal file
51
_archive/listen/README.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Calendar Server Example
|
||||
|
||||
A simple Rust web server using Hyper that exposes an `/all_calendars` endpoint.
|
||||
|
||||
## Running the server
|
||||
|
||||
```bash
|
||||
# Navigate to the examples directory
|
||||
cd /path/to/examples
|
||||
|
||||
# Build and run the server
|
||||
cargo run
|
||||
```
|
||||
|
||||
Once the server is running, you can access the endpoint at:
|
||||
- http://127.0.0.1:8080/all_calendars
|
||||
|
||||
## Features
|
||||
- Simple HTTP server using Hyper
|
||||
- Single endpoint that returns "Hello World"
|
||||
|
||||
|
||||
Sure thing! Here’s the Markdown version you can copy-paste directly into your README.md:
|
||||
|
||||
## 🔁 Live Reload (Hot Reload for Development)
|
||||
|
||||
To automatically recompile and restart your example server on file changes (e.g. Rust code, templates, Rhai scripts), you can use [`cargo-watch`](https://github.com/watchexec/cargo-watch):
|
||||
|
||||
### ✅ Step 1: Install `cargo-watch`
|
||||
|
||||
```bash
|
||||
cargo install cargo-watch
|
||||
```
|
||||
|
||||
### ✅ Step 2: Run the server with live reload
|
||||
|
||||
cargo watch -x 'run --example server'
|
||||
|
||||
This will:
|
||||
• Watch for file changes in your project
|
||||
• Rebuild and re-run examples/server.rs whenever you make a change
|
||||
|
||||
### 🧠 Bonus: Watch additional folders
|
||||
|
||||
To also reload when .tera templates or .rhai scripts change:
|
||||
|
||||
cargo watch -w src -w examples -w src/templates -w src/scripts -x 'run --example server'
|
||||
|
||||
### 💡 Optional: Clear terminal on each reload
|
||||
|
||||
cargo watch -c -x 'run --example server'
|
11
_archive/listen/develop.sh
Executable file
11
_archive/listen/develop.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
# 🚀 Start dev server with file watching and browser reload
|
||||
reloadd \
|
||||
--watch src \
|
||||
--watch src/templates \
|
||||
--watch examples \
|
||||
--port 8080 \
|
||||
--run "cargo build" \
|
||||
--run "cargo run --example server"
|
38
_archive/listen/examples/send_rhai_script.rs
Normal file
38
_archive/listen/examples/send_rhai_script.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use reqwest::Client;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let client = Client::new();
|
||||
let server_url = "http://127.0.0.1:8000";
|
||||
|
||||
// Simple Rhai script that creates a map
|
||||
let rhai_script = r#"
|
||||
let message = "Hello from Rhai script!";
|
||||
let number = 40 + 2;
|
||||
#{
|
||||
greeting: message,
|
||||
calculation_result: number
|
||||
}
|
||||
"#;
|
||||
|
||||
println!("Sending Rhai script to server:\n{}", rhai_script);
|
||||
|
||||
let response = client
|
||||
.post(server_url)
|
||||
.header("Content-Type", "text/plain") // Or application/rhai, but plain text is fine
|
||||
.body(rhai_script.to_string())
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let status = response.status();
|
||||
let response_text = response.text().await?;
|
||||
|
||||
println!("\nServer responded with status: {}", status);
|
||||
println!("Response body:\n{}", response_text);
|
||||
|
||||
if !status.is_success() {
|
||||
return Err(format!("Server returned error: {} - {}", status, response_text).into());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
3
_archive/listen/scripts/init.rhai
Normal file
3
_archive/listen/scripts/init.rhai
Normal file
@@ -0,0 +1,3 @@
|
||||
// This script is used to initialize the Rhai system.
|
||||
// It can be left empty or used to define globally available functions/variables.
|
||||
// print("init.rhai loaded!");
|
111
_archive/listen/src/main.rs
Normal file
111
_archive/listen/src/main.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use hyper::{
|
||||
service::{make_service_fn, service_fn},
|
||||
Body,
|
||||
Request,
|
||||
Response,
|
||||
Server,
|
||||
StatusCode,
|
||||
Method,
|
||||
};
|
||||
use std::convert::Infallible;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::path::Path;
|
||||
|
||||
use rhai_system::{create_hot_reloadable_system, System};
|
||||
use rhai::Dynamic;
|
||||
use hyper::body::to_bytes;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 8000));
|
||||
|
||||
let init_script_path = Path::new("scripts/init.rhai");
|
||||
|
||||
let rhai_sys = match create_hot_reloadable_system(&[init_script_path], Some(0)) {
|
||||
Ok(system) => Arc::new(system),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to create Rhai system: {}", e);
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
|
||||
let rhai_sys_clone = Arc::clone(&rhai_sys);
|
||||
|
||||
let make_svc = make_service_fn(move |_conn| {
|
||||
let rhai_system_for_service = Arc::clone(&rhai_sys_clone);
|
||||
async {
|
||||
Ok::<_, Infallible>(service_fn(move |req: Request<Body>| {
|
||||
let rhai_system_for_request = Arc::clone(&rhai_system_for_service);
|
||||
handle_request(rhai_system_for_request, req)
|
||||
}))
|
||||
}
|
||||
});
|
||||
|
||||
println!("Rhai script server running at http://{}", addr);
|
||||
println!("Send POST requests with Rhai script in the body to execute.");
|
||||
|
||||
Server::bind(&addr).serve(make_svc).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_request(rhai_sys: Arc<System>, req: Request<Body>) -> Result<Response<Body>, Infallible> {
|
||||
match *req.method() {
|
||||
Method::POST => {
|
||||
let body_bytes = match to_bytes(req.into_body()).await {
|
||||
Ok(bytes) => bytes,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading request body: {}", e);
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::from(format!("Error reading request body: {}", e)))
|
||||
.unwrap());
|
||||
}
|
||||
};
|
||||
|
||||
let script_string = match String::from_utf8(body_bytes.to_vec()) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
eprintln!("Request body is not valid UTF-8: {}", e);
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::BAD_REQUEST)
|
||||
.body(Body::from(format!("Request body is not valid UTF-8: {}", e)))
|
||||
.unwrap());
|
||||
}
|
||||
};
|
||||
|
||||
if script_string.trim().is_empty() {
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::BAD_REQUEST)
|
||||
.body(Body::from("Rhai script body cannot be empty."))
|
||||
.unwrap());
|
||||
}
|
||||
|
||||
println!("Executing Rhai script: \n{}", script_string);
|
||||
|
||||
match rhai_sys.engine.eval::<Dynamic>(&script_string) {
|
||||
Ok(result) => {
|
||||
let response_body = format!("{}", result);
|
||||
println!("Script result: {}", response_body);
|
||||
Ok(Response::new(Body::from(response_body)))
|
||||
}
|
||||
Err(e) => {
|
||||
let error_msg = format!("Rhai script execution error: {}", e);
|
||||
eprintln!("{}", error_msg);
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(Body::from(error_msg))
|
||||
.unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::METHOD_NOT_ALLOWED)
|
||||
.header("Allow", "POST")
|
||||
.body(Body::from("Method Not Allowed. Please use POST with Rhai script in the body."))
|
||||
.unwrap())
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user