add server
This commit is contained in:
commit
fe616b76e0
3
server/.gitignore
vendored
Normal file
3
server/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
|
||||
target/
|
||||
|
2113
server/Cargo.lock
generated
Normal file
2113
server/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
12
server/Cargo.toml
Normal file
12
server/Cargo.toml
Normal 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
28
server/src/main.rs
Normal 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())
|
||||
}
|
Loading…
Reference in New Issue
Block a user