add server

This commit is contained in:
Simon 2023-12-18 11:06:04 +01:00
commit fe616b76e0
4 changed files with 2156 additions and 0 deletions

3
server/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
target/

2113
server/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

12
server/Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
local-ip-address = "0.5.6"
serde = { version = "1.0.193", features = ["derive"] }
tide = "0.16.0"

28
server/src/main.rs Normal file
View File

@ -0,0 +1,28 @@
use local_ip_address::local_ip;
use tide::Request;
use tide::prelude::*;
#[derive(Debug, Deserialize)]
struct Sample {
message: String,
value: i32,
}
#[async_std::main]
async fn main() -> tide::Result<()> {
let mut app = tide::new();
app.at("/sample").post(send_sample);
if let Ok(ip) = local_ip() {
println!("Starting server at {ip}:8000!");
} else {
println!("Starting server on port 8000. Could not get IP");
}
app.listen("127.0.0.1:8000").await?;
Ok(())
}
async fn send_sample(mut req: Request<()>) -> tide::Result {
let Sample { message, value } = req.body_json().await?;
println!("Ok, message: {}, value: {}", message, value);
Ok(format!("Ok, message: {}, value: {}", message, value).into())
}