171 lines
5.3 KiB
Rust
171 lines
5.3 KiB
Rust
use asset_db::{AssetId, FsAssets};
|
|
use authdb::AuthError;
|
|
use handlers::{
|
|
handle_available_images, handle_connect_websocket, handle_file, handle_register_client, handle_set_background_image, handle_unregister_client, RegisterRequest
|
|
};
|
|
use std::{
|
|
convert::Infallible,
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
};
|
|
use warp::{
|
|
// header,
|
|
http::{Response, StatusCode},
|
|
reply::Reply,
|
|
Filter,
|
|
};
|
|
|
|
mod asset_db;
|
|
mod core;
|
|
mod handlers;
|
|
|
|
mod types;
|
|
|
|
#[derive(Debug)]
|
|
struct Unauthorized;
|
|
impl warp::reject::Reject for Unauthorized {}
|
|
|
|
#[derive(Debug)]
|
|
struct AuthDBError(AuthError);
|
|
impl warp::reject::Reject for AuthDBError {}
|
|
|
|
/*
|
|
fn with_session(
|
|
auth_ctx: Arc<AuthDB>,
|
|
) -> impl Filter<Extract = (Username,), Error = warp::Rejection> + Clone {
|
|
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))),
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
fn route_echo_unauthenticated() -> impl Filter<Extract = (Json,), Error = warp::Rejection> + Clone {
|
|
warp::path!("api" / "v1" / "echo" / String).map(|param: String| {
|
|
println!("param: {}", param);
|
|
warp::reply::json(&vec!["unauthenticated", param.as_str()])
|
|
})
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
|
|
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| {
|
|
println!("param: {:?}", username);
|
|
println!("param: {}", param);
|
|
warp::reply::json(&vec!["authenticated", username.as_str(), param.as_str()])
|
|
})
|
|
}
|
|
*/
|
|
|
|
async fn handle_rejection(err: warp::Rejection) -> Result<impl Reply, Infallible> {
|
|
println!("handle_rejection: {:?}", err);
|
|
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() {
|
|
let core = core::Core::new(FsAssets::new());
|
|
let log = warp::log("visions::api");
|
|
|
|
let route_image = warp::path!("api" / "v1" / "image" / String)
|
|
.and(warp::get())
|
|
.then({
|
|
let core = core.clone();
|
|
move |file_name| handle_file(core.clone(), AssetId::from(file_name))
|
|
});
|
|
|
|
let route_available_images = warp::path!("api" / "v1" / "image").and(warp::get()).then({
|
|
let core = core.clone();
|
|
move || handle_available_images(core.clone())
|
|
});
|
|
|
|
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)
|
|
});
|
|
|
|
let route_set_bg_image_options = warp::path!("api" / "v1" / "tabletop" / "bg_image")
|
|
.and(warp::options())
|
|
.map({
|
|
move || {
|
|
Response::builder()
|
|
.header("Access-Control-Allow-Origin", "*")
|
|
.header("Access-Control-Allow-Methods", "PUT")
|
|
.header("Access-Control-Allow-Headers", "content-type")
|
|
.header("Content-Type", "application/json")
|
|
.body("")
|
|
.unwrap()
|
|
}
|
|
});
|
|
let route_set_bg_image = warp::path!("api" / "v1" / "tabletop" / "bg_image")
|
|
.and(warp::put())
|
|
.and(warp::body::json())
|
|
.then({
|
|
let core = core.clone();
|
|
move |body| handle_set_background_image(core.clone(), body)
|
|
}).with(log);
|
|
|
|
let filter = route_register_client
|
|
.or(route_unregister_client)
|
|
.or(route_websocket)
|
|
.or(route_image)
|
|
.or(route_available_images)
|
|
.or(route_set_bg_image_options)
|
|
.or(route_set_bg_image)
|
|
.recover(handle_rejection);
|
|
|
|
let server = warp::serve(filter);
|
|
server
|
|
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001))
|
|
.await;
|
|
}
|