diff --git a/visions/server/src/handlers.rs b/visions/server/src/handlers.rs index b7f8641..9c178b7 100644 --- a/visions/server/src/handlers.rs +++ b/visions/server/src/handlers.rs @@ -1,11 +1,13 @@ use std::{io::Read, path::PathBuf}; use authdb::{AuthDB, AuthToken}; -use http::{response::Response, status::StatusCode, Error}; +use http::{Error, StatusCode}; use serde::{Deserialize, Serialize}; +use warp::{http::Response, reply::Reply}; use crate::core::Core; +/* pub async fn handle_auth( auth_ctx: &AuthDB, auth_token: AuthToken, @@ -27,18 +29,38 @@ 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 async fn handle_playing_field() -> impl Reply { + Response::builder() + .header("application-type", "application/json") + .body( + serde_json::to_string(&PlayArea { + background_image: PathBuf::from("tower-in-mist.jpg"), + }) + .unwrap(), + ) + .unwrap() } -pub fn handle_file(core: Core, file_name: String) -> Vec { - core.get_file(file_name) +pub async fn handle_file(core: Core, file_name: String) -> impl Reply { + let mimetype = mime_guess::from_path(&file_name).first().unwrap(); + let bytes = core.get_file(file_name); + Response::builder() + .header("application-type", mimetype.to_string()) + .body(bytes) + .unwrap() +} + +pub async fn handle_available_images(core: Core) -> impl Reply { + Response::builder() + .header("Access-Control-Allow-Origin", "*") + .header("Content-Type", "application/json") + .body(serde_json::to_string(&core.available_images()).unwrap()) + .unwrap() } diff --git a/visions/server/src/main.rs b/visions/server/src/main.rs index f31abd2..4ba8fa0 100644 --- a/visions/server/src/main.rs +++ b/visions/server/src/main.rs @@ -1,5 +1,5 @@ use authdb::AuthError; -use handlers::{handle_file, handle_playing_field}; +use handlers::{handle_available_images, handle_file, handle_playing_field}; use std::{ convert::Infallible, net::{IpAddr, Ipv4Addr, SocketAddr}, @@ -16,8 +16,6 @@ mod core; mod handlers; // use handlers::handle_auth; -mod routes; - #[derive(Debug)] struct Unauthorized; impl warp::reject::Reject for Unauthorized {} @@ -95,31 +93,21 @@ async fn handle_rejection(err: warp::Rejection) -> Result impl Filter + Clone { - warp::path!("api" / "v1" / "field").map(move || warp::reply::json(&handle_playing_field())) -}