Move the handlers out of main.rs

This commit is contained in:
Savanni D'Gerinel 2024-11-12 09:45:34 -05:00
parent 69ef3c3892
commit 962ea66506
3 changed files with 35 additions and 34 deletions

View File

@ -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<u8> {
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()
}

View File

@ -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 Reply, Infallible
pub async fn main() {
let core = core::Core::new();
let route_playing_field =
warp::path!("api" / "v1" / "field").map(move || warp::reply::json(&handle_playing_field()));
let route_playing_field = warp::path!("api" / "v1" / "field").then(|| handle_playing_field());
let route_image = warp::path!("api" / "v1" / "image" / String)
.and(warp::get())
.map({
.then({
let core = core.clone();
move |file_name| {
let core = core.clone();
let mimetype = mime_guess::from_path(&file_name).first().unwrap();
let bytes = handle_file(core, file_name);
Response::builder()
.header("application-type", mimetype.to_string())
.body(bytes)
.unwrap()
handle_file(core.clone(), file_name)
}
});
let route_available_images = warp::path!("api" / "v1" / "image").and(warp::get()).map({
let route_available_images = warp::path!("api" / "v1" / "image").and(warp::get()).then({
let core = core.clone();
move || {
let core = core.clone();
Response::builder()
.header("Access-Control-Allow-Origin", "*")
.body(serde_json::to_string(&core.available_images()).unwrap())
handle_available_images(core.clone())
}
});

View File

@ -1,9 +0,0 @@
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()))
}