add web stuff

This commit is contained in:
Simon 2023-12-20 12:05:01 +01:00
parent ca9c23b82b
commit cca7996110
2 changed files with 30 additions and 11 deletions

9
public/index.html Normal file
View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h1>linking er slow 😭</h1>
</body>
</html>

View File

@ -1,20 +1,21 @@
use local_ip_address::local_ip;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use tide::prelude::*;
use tide::Request;
use tide::{Body, Request};
use lcd_writer::lcd::Lcd;
#[derive(Clone)]
struct App {
values: Vec<i32>,
values: Arc<Mutex<Vec<i32>>>,
lcd: Arc<Mutex<Lcd>>,
}
impl App {
fn new(lcd: Lcd) -> Self {
App {
values: Vec::new(),
values: Arc::new(Mutex::new(Vec::new())),
lcd: Arc::new(Mutex::new(lcd)),
}
}
@ -29,7 +30,11 @@ struct Sample {
async fn main() -> tide::Result<()> {
let lcd = Lcd::new()?;
let mut app = tide::with_state(App::new(lcd));
app.at("/sample").post(send_sample);
app.with(tide::log::LogMiddleware::new());
app.at("/samples").get(get_samples);
app.at("/sample").post(post_sample);
app.at("/").serve_dir("public/")?;
app.at("/").serve_file("public/index.html")?;
if let Ok(ip) = local_ip() {
println!("Starting server at {ip}:8000!");
} else {
@ -39,16 +44,21 @@ async fn main() -> tide::Result<()> {
Ok(())
}
async fn send_sample(mut req: Request<App>) -> tide::Result {
async fn get_samples(req: Request<App>) -> tide::Result<Body> {
let values = req.state().values.lock().unwrap();
Body::from_json(values.deref())
}
async fn post_sample(mut req: Request<App>) -> tide::Result<Body> {
let Sample { value } = req.body_json().await?;
let lcd = req.state().lcd.lock()?;
lcd.clear();
lcd.set_cursor(0, 0);
lcd.write(format!("Value: {}", value).as_str());
let mut lcd = req.state().lcd.lock().unwrap();
lcd.clear()?;
lcd.set_cursor(0, 0)?;
lcd.write(format!("Value: {}", value).as_str())?;
req.state().values.push(value);
req.state().values.lock().unwrap().push(value);
println!("Ok, value: {}", value);
Ok(format!("Ok, value: {}", value).into())
Body::from_json(&json!({"ok": true}))
}