use std::{ convert::Infallible, net::{IpAddr, Ipv4Addr, SocketAddr}, path::PathBuf, }; use asset_db::FsAssets; use authdb::AuthError; use database::DbConn; use filters::{route_authenticate, route_healthcheck, route_image}; use warp::{http::StatusCode, reply::Reply, Filter}; mod asset_db; mod core; mod database; mod filters; mod handlers; mod types; /* fn with_session( auth_ctx: Arc, ) -> impl Filter + 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 + 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, ) -> impl Filter + 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, ) -> impl Filter + 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()]) }) } */ #[tokio::main] pub async fn main() { pretty_env_logger::init(); let conn = DbConn::new(Some("/home/savanni/game.db")); let core = core::Core::new(FsAssets::new(PathBuf::from("/home/savanni/Pictures")), conn); let unauthenticated_endpoints = route_healthcheck().or(route_authenticate(core.clone())); let authenticated_endpoints = route_image(core.clone()); let server = warp::serve( unauthenticated_endpoints .or(authenticated_endpoints) .with(warp::log("visions")) ); server .run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001)) .await; }