lanflare_core/server/
router.rs1use std::net::IpAddr;
4use bytes::Bytes;
5use http_body_util::Full;
6use hyper::body::Incoming;
7use hyper::{Method, Request, Response, StatusCode};
8
9use crate::server::handlers::{
10 empty_response, handle_cancel, handle_download, handle_info, handle_prepare_download,
11 handle_prepare_upload, handle_register, handle_upload, ServerState,
12};
13
14#[derive(Clone, Debug)]
16pub struct ClientConnectionInfo {
17 pub remote_ip: IpAddr,
19 pub cert_fingerprint: Option<String>,
21}
22
23pub async fn route_request(
25 req: Request<Incoming>,
26 client: ClientConnectionInfo,
27 state: &ServerState,
28) -> Result<Response<Full<Bytes>>, hyper::Error> {
29 let method = req.method();
30 let path = req.uri().path();
31
32 let response = match (method, path) {
33 (&Method::GET, "/api/localsend/v2/info") | (&Method::GET, "/api/localsend/v1/info") => {
35 handle_info(state).await
36 }
37
38 (&Method::POST, "/api/localsend/v2/register") => {
40 handle_register(req, client.remote_ip, client.cert_fingerprint, state).await
41 }
42
43 (&Method::POST, "/api/localsend/v2/prepare-upload") => {
45 handle_prepare_upload(req, client.remote_ip, client.cert_fingerprint, state).await
46 }
47
48 (&Method::POST, "/api/localsend/v2/upload") => {
50 handle_upload(req, client.remote_ip, state).await
51 }
52
53 (&Method::POST, "/api/localsend/v2/prepare-download") => {
55 handle_prepare_download(req, client.remote_ip, state).await
56 }
57
58 (&Method::GET, "/api/localsend/v2/download") => {
60 handle_download(req, client.remote_ip, state).await
61 }
62
63 (&Method::POST, "/api/localsend/v2/cancel") => {
65 handle_cancel(req, client.remote_ip, state).await
66 }
67
68 _ => empty_response(StatusCode::NOT_FOUND),
70 };
71
72 Ok(response)
73}