119 lines
3.4 KiB
Rust
119 lines
3.4 KiB
Rust
use authdb::AuthError;
|
|
use handlers::{handle_file, handle_playing_field};
|
|
use std::{
|
|
convert::Infallible,
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
};
|
|
use warp::{
|
|
// header,
|
|
http::{Response, StatusCode},
|
|
reply::Reply,
|
|
Filter,
|
|
};
|
|
|
|
mod core;
|
|
|
|
mod handlers;
|
|
// use handlers::handle_auth;
|
|
|
|
mod routes;
|
|
|
|
#[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> {
|
|
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 route_playing_field =
|
|
warp::path!("api" / "v1" / "field").map(move || warp::reply::json(&handle_playing_field()));
|
|
|
|
let route_static_file = warp::path!("api" / "v1" / "file" / String)
|
|
.and(warp::get())
|
|
.map(move |file_name| {
|
|
let bytes = handle_file(file_name);
|
|
Response::builder()
|
|
.header("application-type", "image/jpeg")
|
|
.body(bytes)
|
|
.unwrap()
|
|
});
|
|
|
|
let core = core::Core::new();
|
|
let filter = route_playing_field
|
|
.or(route_static_file)
|
|
.recover(handle_rejection);
|
|
|
|
let server = warp::serve(filter);
|
|
server
|
|
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001))
|
|
.await;
|
|
}
|