Commit ced93bee authored by TechnoElf's avatar TechnoElf
Browse files

Control over HTTP

parent 9e4ba00a
use std::net::{TcpListener};
use std::io::Read;
use std::io::{Read, Write};
use rppal::hal::Delay;
use rppal::spi::{Spi, Bus, SlaveSelect, Mode};
......@@ -30,13 +30,36 @@ fn main() {
display.clear(Rgb565::new(255, 255, 255)).unwrap();
Text::with_alignment("Release the Kraken!", Point::new(120, 120), style, Alignment::Center).draw(&mut display).unwrap();
let listener = TcpListener::bind("0.0.0.0:8080").unwrap();
let listener = TcpListener::bind("0.0.0.0:80").unwrap();
for stream in listener.incoming() {
if let Ok(mut stream) = stream {
let mut text = String::new();
stream.read_to_string(&mut text).unwrap();
display.clear(Rgb565::new(255, 255, 255)).unwrap();
Text::with_alignment(&text, Point::new(120, 20), style, Alignment::Center).draw(&mut display).unwrap();
let mut buf = [0; 1024];
let len = stream.read(&mut buf).unwrap();
let buf = String::from_utf8_lossy(&buf[0..len]);
let (head, body) = buf.split_once("\r\n\r\n").unwrap();
let (req, _headers) = head.split_once("\r\n").unwrap();
let (method, rem) = req.split_once(" ").unwrap();
let (uri, _ver) = rem.split_once(" ").unwrap();
let resp = match (method, uri) {
("PUT", "/id") => {
display.clear(Rgb565::new(255, 255, 255)).unwrap();
Text::with_alignment(&body, Point::new(120, 20), style, Alignment::Center).draw(&mut display).unwrap();
"200 OK"
},
("PUT", "/status") => {
display.clear(Rgb565::new(0, 255, 0)).unwrap();
"200 OK"
},
(m, u) => {
eprintln!("Invalid request: {} {}", m, u);
"404 NOT FOUND"
}
};
stream.write(format!("HTTP/1.1 {}\r\n\r\n", resp).as_bytes()).unwrap();
stream.flush().unwrap();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment