sneedstr/src/relay/routes.rs

37 lines
1.2 KiB
Rust
Raw Normal View History

2024-01-12 09:35:31 -06:00
use super::handler;
use crate::utils::filter::with_context;
use crate::utils::structs::Context;
use warp::{Filter, Rejection, Reply};
pub fn routes(context: Context) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let cors = warp::cors().allow_any_origin();
static_files().or(index(context)).with(cors)
}
fn index(context: Context) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
2024-01-15 16:39:55 -06:00
// let real_client_ip = warp::header::optional::<std::net::SocketAddr>("X-Real-IP");
let real_client_ip = warp::addr::remote();
2024-01-15 17:58:27 -06:00
let accept_application_json_header = warp::header::header("Accept");
2024-01-12 09:35:31 -06:00
2024-01-15 17:58:27 -06:00
warp::path::end().and(
accept_application_json_header
.and_then(handler::relay_config)
.or(warp::ws()
.and(with_context(context))
.and(real_client_ip)
.and_then(handler::ws_handler)),
)
2024-01-12 09:35:31 -06:00
}
fn static_files() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let mut exe_dir = std::env::current_exe().unwrap();
exe_dir.pop();
2024-01-12 09:35:31 -06:00
let mut www = exe_dir.clone();
2024-01-12 09:35:31 -06:00
www.pop();
www.push(std::path::Path::new("www/static"));
warp::get().and(warp::fs::dir(www))
}