sneedstr/src/relay/routes.rs
2024-01-12 09:35:31 -06:00

32 lines
914 B
Rust

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 {
let client_addr = warp::addr::remote();
warp::path::end()
.and(warp::ws())
.and(with_context(context))
.and(client_addr)
.and_then(handler::ws_handler)
}
fn static_files() -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
let mut foo = std::env::current_exe().unwrap();
foo.pop();
let mut www = foo.clone();
www.pop();
www.push(std::path::Path::new("www/static"));
warp::get().and(warp::fs::dir(www))
}