2024-11-12 00:58:50 +00:00
|
|
|
use authdb::AuthError;
|
2024-11-19 00:08:49 +00:00
|
|
|
use handlers::{
|
|
|
|
handle_available_images, handle_connect_websocket, handle_file, handle_playing_field,
|
|
|
|
handle_register_client, handle_unregister_client, RegisterRequest,
|
|
|
|
};
|
2023-11-21 04:30:10 +00:00
|
|
|
use std::{
|
2023-11-21 14:57:35 +00:00
|
|
|
convert::Infallible,
|
2023-11-21 04:30:10 +00:00
|
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
|
|
};
|
2023-11-21 14:57:35 +00:00
|
|
|
use warp::{
|
2024-11-12 00:58:50 +00:00
|
|
|
// header,
|
|
|
|
http::{Response, StatusCode},
|
|
|
|
reply::Reply,
|
2023-11-21 14:57:35 +00:00
|
|
|
Filter,
|
|
|
|
};
|
|
|
|
|
2024-11-12 00:58:50 +00:00
|
|
|
mod core;
|
|
|
|
|
2023-11-21 14:57:35 +00:00
|
|
|
mod handlers;
|
2024-11-12 00:58:50 +00:00
|
|
|
// use handlers::handle_auth;
|
|
|
|
|
2024-11-19 00:08:49 +00:00
|
|
|
mod types;
|
|
|
|
|
2023-11-21 14:57:35 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Unauthorized;
|
|
|
|
impl warp::reject::Reject for Unauthorized {}
|
2023-11-21 04:30:10 +00:00
|
|
|
|
2023-11-21 14:57:35 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct AuthDBError(AuthError);
|
|
|
|
impl warp::reject::Reject for AuthDBError {}
|
|
|
|
|
2024-11-12 00:58:50 +00:00
|
|
|
/*
|
2023-11-21 14:57:35 +00:00
|
|
|
fn with_session(
|
|
|
|
auth_ctx: Arc<AuthDB>,
|
2023-11-21 04:30:10 +00:00
|
|
|
) -> impl Filter<Extract = (Username,), Error = warp::Rejection> + Clone {
|
2023-11-21 14:57:35 +00:00
|
|
|
header("authentication").and_then({
|
|
|
|
move |value: String| {
|
|
|
|
let auth_ctx = auth_ctx.clone();
|
|
|
|
async move {
|
|
|
|
match auth_ctx.validate_session(SessionToken::from(value)).await {
|
|
|
|
Ok(Some(username)) => Ok(username),
|
|
|
|
Ok(None) => Err(warp::reject::custom(Unauthorized)),
|
|
|
|
Err(err) => Err(warp::reject::custom(AuthDBError(err))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 04:30:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-21 14:57:35 +00:00
|
|
|
fn route_echo_unauthenticated() -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
|
|
|
|
warp::path!("api" / "v1" / "echo" / String).map(|param: String| {
|
2023-11-21 04:30:10 +00:00
|
|
|
println!("param: {}", param);
|
|
|
|
warp::reply::json(&vec!["unauthenticated", param.as_str()])
|
2023-11-21 14:57:35 +00:00
|
|
|
})
|
|
|
|
}
|
2023-11-21 04:30:10 +00:00
|
|
|
|
2023-11-21 14:57:35 +00:00
|
|
|
fn route_authenticate(
|
|
|
|
auth_ctx: Arc<AuthDB>,
|
|
|
|
) -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
|
|
|
|
let auth_ctx = auth_ctx.clone();
|
|
|
|
warp::path!("api" / "v1" / "auth")
|
|
|
|
.and(warp::post())
|
|
|
|
.and(warp::body::json())
|
|
|
|
.map(move |param: AuthToken| {
|
|
|
|
let res = handle_auth(&auth_ctx, param.clone());
|
|
|
|
warp::reply::json(¶m)
|
|
|
|
})
|
|
|
|
}
|
2023-11-21 04:30:10 +00:00
|
|
|
|
2023-11-21 14:57:35 +00:00
|
|
|
fn route_echo_authenticated(
|
|
|
|
auth_ctx: Arc<AuthDB>,
|
|
|
|
) -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
|
|
|
|
warp::path!("api" / "v1" / "echo" / String)
|
|
|
|
.and(with_session(auth_ctx.clone()))
|
|
|
|
.map(move |param: String, username: Username| {
|
2023-11-21 04:30:10 +00:00
|
|
|
println!("param: {:?}", username);
|
|
|
|
println!("param: {}", param);
|
|
|
|
warp::reply::json(&vec!["authenticated", username.as_str(), param.as_str()])
|
2023-11-21 14:57:35 +00:00
|
|
|
})
|
|
|
|
}
|
2024-11-12 00:58:50 +00:00
|
|
|
*/
|
2023-11-21 14:57:35 +00:00
|
|
|
|
|
|
|
async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible> {
|
|
|
|
if let Some(Unauthorized) = err.find() {
|
|
|
|
Ok(warp::reply::with_status(
|
|
|
|
"".to_owned(),
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
Ok(warp::reply::with_status(
|
|
|
|
"".to_owned(),
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
pub async fn main() {
|
2024-11-12 05:16:54 +00:00
|
|
|
let core = core::Core::new();
|
|
|
|
|
2024-11-12 14:45:34 +00:00
|
|
|
let route_playing_field = warp::path!("api" / "v1" / "field").then(|| handle_playing_field());
|
2024-11-12 00:58:50 +00:00
|
|
|
|
2024-11-12 05:16:54 +00:00
|
|
|
let route_image = warp::path!("api" / "v1" / "image" / String)
|
2024-11-12 00:58:50 +00:00
|
|
|
.and(warp::get())
|
2024-11-12 14:45:34 +00:00
|
|
|
.then({
|
2024-11-12 05:16:54 +00:00
|
|
|
let core = core.clone();
|
2024-11-19 00:08:49 +00:00
|
|
|
move |file_name| handle_file(core.clone(), file_name)
|
2024-11-12 00:58:50 +00:00
|
|
|
});
|
2023-11-21 04:30:10 +00:00
|
|
|
|
2024-11-12 14:45:34 +00:00
|
|
|
let route_available_images = warp::path!("api" / "v1" / "image").and(warp::get()).then({
|
2024-11-12 05:16:54 +00:00
|
|
|
let core = core.clone();
|
2024-11-19 00:08:49 +00:00
|
|
|
move || handle_available_images(core.clone())
|
2024-11-12 05:16:54 +00:00
|
|
|
});
|
|
|
|
|
2024-11-19 00:08:49 +00:00
|
|
|
let route_register_client = warp::path!("api" / "v1" / "client")
|
|
|
|
.and(warp::post())
|
|
|
|
.then({
|
|
|
|
let core = core.clone();
|
|
|
|
move || handle_register_client(core.clone(), RegisterRequest {})
|
|
|
|
});
|
|
|
|
|
|
|
|
let route_unregister_client = warp::path!("api" / "v1" / "client" / String)
|
|
|
|
.and(warp::delete())
|
|
|
|
.then({
|
|
|
|
let core = core.clone();
|
|
|
|
move |client_id| handle_unregister_client(core.clone(), client_id)
|
|
|
|
});
|
|
|
|
|
|
|
|
let route_websocket = warp::path("ws")
|
|
|
|
.and(warp::ws())
|
|
|
|
.and(warp::path::param())
|
|
|
|
.then({
|
|
|
|
let core = core.clone();
|
|
|
|
move |ws, client_id| handle_connect_websocket(core.clone(), ws, client_id)
|
|
|
|
});
|
|
|
|
|
2024-11-19 04:32:54 +00:00
|
|
|
let route_set_playfield_bg = warp::path!("api" / "v1" / "playfield" / "bg")
|
|
|
|
.and(warp::put())
|
2024-11-19 00:08:49 +00:00
|
|
|
.and(warp::body::json())
|
|
|
|
.map({
|
|
|
|
let core = core.clone();
|
|
|
|
move |body| {
|
2024-11-19 04:32:54 +00:00
|
|
|
core.set_playfield_background(body);
|
2024-11-19 00:08:49 +00:00
|
|
|
warp::reply()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let filter = route_register_client
|
|
|
|
.or(route_unregister_client)
|
|
|
|
.or(route_websocket)
|
|
|
|
.or(route_playing_field)
|
2024-11-12 05:16:54 +00:00
|
|
|
.or(route_image)
|
|
|
|
.or(route_available_images)
|
2024-11-19 04:32:54 +00:00
|
|
|
.or(route_set_playfield_bg)
|
2023-11-21 14:57:35 +00:00
|
|
|
.recover(handle_rejection);
|
2023-11-21 04:30:10 +00:00
|
|
|
|
|
|
|
let server = warp::serve(filter);
|
|
|
|
server
|
|
|
|
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001))
|
|
|
|
.await;
|
2023-11-20 04:23:26 +00:00
|
|
|
}
|