Move the handlers out of main.rs
This commit is contained in:
parent
69ef3c3892
commit
962ea66506
|
@ -1,11 +1,13 @@
|
||||||
use std::{io::Read, path::PathBuf};
|
use std::{io::Read, path::PathBuf};
|
||||||
|
|
||||||
use authdb::{AuthDB, AuthToken};
|
use authdb::{AuthDB, AuthToken};
|
||||||
use http::{response::Response, status::StatusCode, Error};
|
use http::{Error, StatusCode};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use warp::{http::Response, reply::Reply};
|
||||||
|
|
||||||
use crate::core::Core;
|
use crate::core::Core;
|
||||||
|
|
||||||
|
/*
|
||||||
pub async fn handle_auth(
|
pub async fn handle_auth(
|
||||||
auth_ctx: &AuthDB,
|
auth_ctx: &AuthDB,
|
||||||
auth_token: AuthToken,
|
auth_token: AuthToken,
|
||||||
|
@ -27,18 +29,38 @@ pub async fn handle_auth(
|
||||||
.body("".to_owned()),
|
.body("".to_owned()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct PlayArea {
|
pub struct PlayArea {
|
||||||
pub background_image: PathBuf,
|
pub background_image: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_playing_field() -> PlayArea {
|
pub async fn handle_playing_field() -> impl Reply {
|
||||||
PlayArea {
|
Response::builder()
|
||||||
background_image: PathBuf::from("tower-in-mist.jpg"),
|
.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> {
|
pub async fn handle_file(core: Core, file_name: String) -> impl Reply {
|
||||||
core.get_file(file_name)
|
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()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use authdb::AuthError;
|
use authdb::AuthError;
|
||||||
use handlers::{handle_file, handle_playing_field};
|
use handlers::{handle_available_images, handle_file, handle_playing_field};
|
||||||
use std::{
|
use std::{
|
||||||
convert::Infallible,
|
convert::Infallible,
|
||||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||||
|
@ -16,8 +16,6 @@ mod core;
|
||||||
mod handlers;
|
mod handlers;
|
||||||
// use handlers::handle_auth;
|
// use handlers::handle_auth;
|
||||||
|
|
||||||
mod routes;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Unauthorized;
|
struct Unauthorized;
|
||||||
impl warp::reject::Reject for 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() {
|
pub async fn main() {
|
||||||
let core = core::Core::new();
|
let core = core::Core::new();
|
||||||
|
|
||||||
let route_playing_field =
|
let route_playing_field = warp::path!("api" / "v1" / "field").then(|| handle_playing_field());
|
||||||
warp::path!("api" / "v1" / "field").map(move || warp::reply::json(&handle_playing_field()));
|
|
||||||
|
|
||||||
let route_image = warp::path!("api" / "v1" / "image" / String)
|
let route_image = warp::path!("api" / "v1" / "image" / String)
|
||||||
.and(warp::get())
|
.and(warp::get())
|
||||||
.map({
|
.then({
|
||||||
let core = core.clone();
|
let core = core.clone();
|
||||||
move |file_name| {
|
move |file_name| {
|
||||||
let core = core.clone();
|
handle_file(core.clone(), file_name)
|
||||||
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()
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
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();
|
let core = core.clone();
|
||||||
move || {
|
move || {
|
||||||
let core = core.clone();
|
handle_available_images(core.clone())
|
||||||
Response::builder()
|
|
||||||
.header("Access-Control-Allow-Origin", "*")
|
|
||||||
.body(serde_json::to_string(&core.available_images()).unwrap())
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -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()))
|
|
||||||
}
|
|
Loading…
Reference in New Issue