2024-11-30 04:14:52 +00:00
|
|
|
use std::{
|
|
|
|
convert::Infallible,
|
|
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
|
|
path::PathBuf,
|
|
|
|
};
|
|
|
|
|
2024-12-27 19:29:07 +00:00
|
|
|
use asset_db::FsAssets;
|
2024-11-12 00:58:50 +00:00
|
|
|
use authdb::AuthError;
|
2024-11-30 16:48:35 +00:00
|
|
|
use database::DbConn;
|
2024-12-27 19:29:07 +00:00
|
|
|
use filters::{route_authenticate, route_healthcheck, route_image};
|
|
|
|
use warp::{http::StatusCode, reply::Reply, Filter};
|
2023-11-21 14:57:35 +00:00
|
|
|
|
2024-11-20 14:52:26 +00:00
|
|
|
mod asset_db;
|
2024-11-12 00:58:50 +00:00
|
|
|
mod core;
|
2024-11-30 04:14:52 +00:00
|
|
|
mod database;
|
2024-12-27 19:29:07 +00:00
|
|
|
mod filters;
|
2023-11-21 14:57:35 +00:00
|
|
|
mod handlers;
|
2024-11-19 00:08:49 +00:00
|
|
|
mod types;
|
|
|
|
|
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
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
pub async fn main() {
|
2024-12-22 14:17:00 +00:00
|
|
|
pretty_env_logger::init();
|
|
|
|
|
2024-11-30 16:48:35 +00:00
|
|
|
let conn = DbConn::new(Some("/home/savanni/game.db"));
|
|
|
|
|
|
|
|
let core = core::Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")), conn);
|
2024-11-12 05:16:54 +00:00
|
|
|
|
2024-12-27 19:02:43 +00:00
|
|
|
let unauthenticated_endpoints = route_healthcheck().or(route_authenticate(core.clone()));
|
|
|
|
let authenticated_endpoints = route_image(core.clone());
|
2024-12-16 05:27:55 +00:00
|
|
|
|
2024-12-27 19:29:07 +00:00
|
|
|
let server = warp::serve(
|
|
|
|
unauthenticated_endpoints
|
|
|
|
.or(authenticated_endpoints)
|
|
|
|
.with(warp::log("visions"))
|
|
|
|
);
|
2023-11-21 04:30:10 +00:00
|
|
|
server
|
|
|
|
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001))
|
|
|
|
.await;
|
2023-11-20 04:23:26 +00:00
|
|
|
}
|