monorepo/visions/server/src/main.rs

119 lines
3.6 KiB
Rust
Raw Normal View History

use std::{
convert::Infallible,
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
};
2024-11-21 23:46:05 +00:00
use asset_db::{AssetId, FsAssets};
use authdb::AuthError;
use database::DbConn;
2024-12-27 19:02:43 +00:00
use filters::{route_authenticate, route_available_images, route_get_charsheet, route_healthcheck, route_image, route_register_client, route_server_status, route_set_bg_image, route_unregister_client, route_websocket, routes_user_management};
use warp::{
// header,
2024-12-22 14:17:00 +00:00
filters::{method, path},
http::{Response, StatusCode},
reply::Reply,
Filter,
};
mod asset_db;
mod core;
mod database;
mod handlers;
mod types;
mod filters;
#[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(&param)
})
}
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() {
2024-12-22 14:17:00 +00:00
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);
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-27 19:02:43 +00:00
let server = warp::serve(unauthenticated_endpoints
.or(authenticated_endpoints)
.with(warp::log("visions"))
.recover(handle_rejection));
server
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8001))
.await;
}