Create an image server and create the playing field

This commit is contained in:
Savanni D'Gerinel 2024-11-11 19:58:50 -05:00
parent 911bc97b69
commit addfd2072c
6 changed files with 89 additions and 318 deletions

304
Cargo.lock generated
View File

@ -350,22 +350,6 @@ version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b"
[[package]]
name = "bike"
version = "0.1.0"
dependencies = [
"az",
"cortex-m",
"cortex-m-rt",
"embedded-alloc",
"embedded-hal 0.2.7",
"fixed",
"fugit",
"lights-core",
"panic-halt 0.2.0",
"rp-pico 0.8.0",
]
[[package]]
name = "bindgen"
version = "0.69.5"
@ -408,18 +392,6 @@ version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]]
name = "bitfield"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719"
[[package]]
name = "bitfield"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac"
[[package]]
name = "bitflags"
version = "1.3.2"
@ -759,38 +731,6 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cortex-m"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9"
dependencies = [
"bare-metal",
"bitfield 0.13.2",
"embedded-hal 0.2.7",
"volatile-register",
]
[[package]]
name = "cortex-m-rt"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee84e813d593101b1723e13ec38b6ab6abbdbaaa4546553f5395ed274079ddb1"
dependencies = [
"cortex-m-rt-macros",
]
[[package]]
name = "cortex-m-rt-macros"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0f6f3e36f203cfedbc78b357fb28730aa2c6dc1ab060ee5c2405e843988d3c7"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "cpufeatures"
version = "0.2.14"
@ -1033,66 +973,6 @@ dependencies = [
"serde 1.0.210",
]
[[package]]
name = "embedded-alloc"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddae17915accbac2cfbc64ea0ae6e3b330e6ea124ba108dada63646fd3c6f815"
dependencies = [
"critical-section",
"linked_list_allocator",
]
[[package]]
name = "embedded-dma"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "994f7e5b5cb23521c22304927195f236813053eb9c065dd2226a32ba64695446"
dependencies = [
"stable_deref_trait",
]
[[package]]
name = "embedded-hal"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff"
dependencies = [
"nb 0.1.3",
"void",
]
[[package]]
name = "embedded-hal"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89"
[[package]]
name = "embedded-hal-async"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4c685bbef7fe13c3c6dd4da26841ed3980ef33e841cddfa15ce8a8fb3f1884"
dependencies = [
"embedded-hal 1.0.0",
]
[[package]]
name = "embedded-hal-nb"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605"
dependencies = [
"embedded-hal 1.0.0",
"nb 1.1.0",
]
[[package]]
name = "embedded-io"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d"
[[package]]
name = "emseries"
version = "0.6.0"
@ -1945,26 +1825,6 @@ dependencies = [
"crunchy",
]
[[package]]
name = "halloween-leds"
version = "0.1.0"
dependencies = [
"cortex-m-rt",
"embedded-hal 1.0.0",
"embedded-io",
"panic-halt 1.0.0",
"rp-pico 0.9.0",
]
[[package]]
name = "hash32"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
@ -2014,16 +1874,6 @@ dependencies = [
"http 0.2.12",
]
[[package]]
name = "heapless"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
dependencies = [
"hash32",
"stable_deref_trait",
]
[[package]]
name = "heck"
version = "0.4.1"
@ -2507,7 +2357,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
dependencies = [
"cfg-if",
"windows-targets 0.52.6",
"windows-targets 0.48.5",
]
[[package]]
@ -3035,18 +2885,6 @@ dependencies = [
"system-deps",
]
[[package]]
name = "panic-halt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
[[package]]
name = "panic-halt"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11"
[[package]]
name = "parking"
version = "2.2.1"
@ -3749,130 +3587,6 @@ dependencies = [
"thiserror",
]
[[package]]
name = "rp-pico"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6341771e6f8e5d130b2b3cbc23435b7847761adf198af09f4b2a60407d43bd56"
dependencies = [
"cortex-m-rt",
"fugit",
"rp2040-boot2",
"rp2040-hal 0.9.2",
"usb-device 0.2.9",
]
[[package]]
name = "rp-pico"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9342d3ac7011ac688300979e9b52a81f0add1d05feb02868cf94bfee0705b28"
dependencies = [
"cortex-m-rt",
"fugit",
"rp2040-boot2",
"rp2040-hal 0.10.2",
"usb-device 0.3.2",
]
[[package]]
name = "rp2040-boot2"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c92f344f63f950ee36cf4080050e4dce850839b9175da38f9d2ffb69b4dbb21"
dependencies = [
"crc-any",
]
[[package]]
name = "rp2040-hal"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ff2b9ae7e6dd6720fd9f64250c9087260e50fe98b6b032ccca65be3581167ca"
dependencies = [
"cortex-m",
"critical-section",
"embedded-dma",
"embedded-hal 0.2.7",
"frunk",
"fugit",
"itertools 0.10.5",
"nb 1.1.0",
"paste",
"pio",
"rand_core 0.6.4",
"rp2040-hal-macros",
"rp2040-pac 0.5.0",
"usb-device 0.2.9",
"vcell",
"void",
]
[[package]]
name = "rp2040-hal"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d11e711940087f2cdff8aeae9f4b902e2014c06a00b39a1092686b81ec973d6f"
dependencies = [
"bitfield 0.14.0",
"cortex-m",
"critical-section",
"embedded-dma",
"embedded-hal 0.2.7",
"embedded-hal 1.0.0",
"embedded-hal-async",
"embedded-hal-nb",
"embedded-io",
"frunk",
"fugit",
"itertools 0.10.5",
"nb 1.1.0",
"paste",
"pio",
"rand_core 0.6.4",
"rp2040-hal-macros",
"rp2040-pac 0.6.0",
"usb-device 0.3.2",
"vcell",
"void",
]
[[package]]
name = "rp2040-hal-macros"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86479063e497efe1ae81995ef9071f54fd1c7427e04d6c5b84cde545ff672a5e"
dependencies = [
"cortex-m-rt",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "rp2040-pac"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12d9d8375815f543f54835d01160d4e47f9e2cae75f17ff8f1ec19ce1da96e4c"
dependencies = [
"cortex-m",
"cortex-m-rt",
"critical-section",
"vcell",
]
[[package]]
name = "rp2040-pac"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83cbcd3f7a0ca7bbe61dc4eb7e202842bee4e27b769a7bf3a4a72fa399d6e404"
dependencies = [
"cortex-m",
"cortex-m-rt",
"critical-section",
"vcell",
]
[[package]]
name = "rsa"
version = "0.9.6"
@ -5067,22 +4781,6 @@ version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]]
name = "usb-device"
version = "0.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f6cc3adc849b5292b4075fc0d5fdcf2f24866e88e336dd27a8943090a520508"
[[package]]
name = "usb-device"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98816b1accafbb09085168b90f27e93d790b4bfa19d883466b5e53315b5f06a6"
dependencies = [
"heapless",
"portable-atomic",
]
[[package]]
name = "utf-8"
version = "0.7.6"

View File

@ -32,5 +32,6 @@ members = [
"sgf",
"timezone-testing",
"tree",
"visions/server", "gm-dash/server", "halloween-leds"
"visions/server",
"gm-dash/server"
]

View File

@ -0,0 +1,21 @@
use std::sync::{Arc, RwLock};
#[derive(Debug)]
pub enum AppError {
JsonError(serde_json::Error)
}
#[derive(Clone, Debug)]
pub struct AppState {}
#[derive(Clone, Debug)]
pub struct Core(Arc<RwLock<AppState>>);
impl Core {
pub fn new() -> Self {
Self(Arc::new(RwLock::new(AppState {})))
}
}

View File

@ -1,5 +1,8 @@
use std::{io::Read, path::PathBuf};
use authdb::{AuthDB, AuthToken};
use http::{response::Response, status::StatusCode, Error};
use serde::{Deserialize, Serialize};
pub async fn handle_auth(
auth_ctx: &AuthDB,
@ -22,3 +25,29 @@ pub async fn handle_auth(
.body("".to_owned()),
}
}
#[derive(Deserialize, Serialize)]
pub struct PlayArea {
pub background_image: PathBuf,
}
pub fn handle_playing_field() -> PlayArea {
PlayArea {
background_image: PathBuf::from("tower-in-mist.jpg"),
}
}
pub fn handle_file(file_name: String) -> Vec<u8> {
let mut full_path = PathBuf::new();
full_path.push("/home");
full_path.push("savanni");
full_path.push("Pictures");
full_path.push(&file_name);
println!("path: {:?}", full_path);
let mut content: Vec<u8> = Vec::new();
let mut file = std::fs::File::open(&full_path).unwrap();
file.read_to_end(&mut content).unwrap();
content
}

View File

@ -1,19 +1,22 @@
use authdb::{AuthDB, AuthError, AuthToken, SessionToken, Username};
use authdb::AuthError;
use handlers::{handle_file, handle_playing_field};
use std::{
convert::Infallible,
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
sync::Arc,
};
use warp::{
header,
http::StatusCode,
reply::{Json, Reply},
// header,
http::{Response, StatusCode},
reply::Reply,
Filter,
};
mod core;
mod handlers;
use handlers::handle_auth;
// use handlers::handle_auth;
mod routes;
#[derive(Debug)]
struct Unauthorized;
@ -23,6 +26,7 @@ impl warp::reject::Reject for Unauthorized {}
struct AuthDBError(AuthError);
impl warp::reject::Reject for AuthDBError {}
/*
fn with_session(
auth_ctx: Arc<AuthDB>,
) -> impl Filter<Extract = (Username,), Error = warp::Rejection> + Clone {
@ -71,6 +75,7 @@ fn route_echo_authenticated(
warp::reply::json(&vec!["authenticated", username.as_str(), param.as_str()])
})
}
*/
async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible> {
if let Some(Unauthorized) = err.find() {
@ -88,14 +93,22 @@ async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible
#[tokio::main]
pub async fn main() {
let auth_db = AuthDB::new(PathBuf::from("./auth_db.sqlite"))
.await
.expect("AuthDB should initialize");
let auth_ctx: Arc<AuthDB> = Arc::new(auth_db);
let route_playing_field =
warp::path!("api" / "v1" / "field").map(move || warp::reply::json(&handle_playing_field()));
let filter = route_echo_authenticated(auth_ctx.clone())
.or(route_authenticate(auth_ctx.clone()))
.or(route_echo_unauthenticated())
let route_static_file = warp::path!("api" / "v1" / "file" / String)
.and(warp::get())
.map(move |file_name| {
let bytes = handle_file(file_name);
Response::builder()
.header("application-type", "image/jpeg")
.body(bytes)
.unwrap()
});
let core = core::Core::new();
let filter = route_playing_field
.or(route_static_file)
.recover(handle_rejection);
let server = warp::serve(filter);

View File

@ -0,0 +1,9 @@
use crate::{core::Core, handlers::handle_playing_field};
use warp::{reply::Json, Filter};
pub fn route_playing_field(
_app: Core,
) -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
warp::path!("api" / "v1" / "field").map(move || warp::reply::json(&handle_playing_field()))
}