reorganize module

This commit is contained in:
Timur Gordon
2025-04-04 08:28:07 +02:00
parent 1ea37e2e7f
commit 939b6b4e57
375 changed files with 7580 additions and 191 deletions

80
server/src/main.rs Normal file
View File

@@ -0,0 +1,80 @@
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use hyper::StatusCode;
use std::convert::Infallible;
use std::net::SocketAddr;
extern crate calendar;
#[tokio::main]
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(|req| async {
// Remove the /calendar prefix and pass the modified request to the calendar handler
if req.uri().path().starts_with("/calendar/") {
calendar::handle_request(remove_path_prefix(req, "/calendar")).await
} else {
let mut resp = Response::new(Body::from("Not Found"));
*resp.status_mut() = StatusCode::NOT_FOUND;
Ok(resp)
}
}))
});
println!("Proxy server running at http://{}", addr);
if let Err(e) = Server::bind(&addr).serve(make_svc).await {
eprintln!("server error: {}", e);
}
}
/// Removes a specified prefix from the request's URI path
///
/// # Arguments
/// * `req` - The original request
/// * `prefix` - The prefix to remove from the path (e.g., "/calendar")
///
/// # Returns
/// A new request with the modified path
fn remove_path_prefix(req: Request<Body>, prefix: &str) -> Request<Body> {
let (parts, body) = req.into_parts();
let mut new_parts = parts;
// Get the original path for prefix calculation
let original_path = new_parts.uri.path();
// Calculate the prefix length (including the trailing slash if present)
let prefix_len = if original_path.starts_with(&format!("{}/", prefix)) {
prefix.len() + 1
} else {
prefix.len()
};
// Build a new URI with the prefix removed
let mut uri_builder = hyper::Uri::builder()
.scheme(new_parts.uri.scheme_str().unwrap_or("http"));
// Only set authority if it exists
if let Some(auth) = new_parts.uri.authority() {
uri_builder = uri_builder.authority(auth.clone());
}
// Create path and query string
let mut path_query = original_path[prefix_len..].to_string();
// Add query parameters if they exist
if let Some(q) = new_parts.uri.query() {
path_query.push('?');
path_query.push_str(q);
}
let new_uri = uri_builder
.path_and_query(path_query)
.build()
.unwrap_or(new_parts.uri.clone());
new_parts.uri = new_uri;
Request::from_parts(new_parts, body)
}