2024-08-14 10:58:09 +02:00
|
|
|
use axum::body::{to_bytes, Body};
|
|
|
|
use axum::Router;
|
2024-07-26 14:06:53 +02:00
|
|
|
use bytes::Bytes;
|
|
|
|
use http::{request, response, Request, Response};
|
|
|
|
use std::path::PathBuf;
|
2024-07-23 20:08:45 +02:00
|
|
|
use std::sync::Arc;
|
2024-07-26 14:06:53 +02:00
|
|
|
use tauri::path::BaseDirectory;
|
|
|
|
use tauri::Manager;
|
2024-08-14 10:58:09 +02:00
|
|
|
use thiserror::Error;
|
2024-07-24 14:21:37 +02:00
|
|
|
use tokio::sync::{Mutex, MutexGuard};
|
2024-07-23 20:08:45 +02:00
|
|
|
use tower::{Service, ServiceExt};
|
|
|
|
|
2024-08-14 10:58:09 +02:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum DesktopError {
|
|
|
|
#[error("Axum error:\n{0}")]
|
|
|
|
Axum(#[from] axum::Error),
|
|
|
|
#[error("Infallible error")]
|
|
|
|
Infallible(#[from] std::convert::Infallible),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process requests sent to Tauri (with the `axum://` protocol) and handle them with Axum
|
|
|
|
/// When an error occurs, this function is expected to panic, which should result in a 500 error
|
|
|
|
/// being sent to the client, so we let the client handle the error recovering
|
2024-07-24 14:21:37 +02:00
|
|
|
async fn process_tauri_request(
|
2024-07-26 14:06:53 +02:00
|
|
|
tauri_request: Request<Vec<u8>>,
|
|
|
|
mut router: MutexGuard<'_, Router>,
|
2024-08-14 10:58:09 +02:00
|
|
|
) -> Result<Response<Vec<u8>>, DesktopError> {
|
2024-07-26 14:06:53 +02:00
|
|
|
let (parts, body): (request::Parts, Vec<u8>) = tauri_request.into_parts();
|
|
|
|
let axum_request: Request<Body> = Request::from_parts(parts, body.into());
|
2024-07-24 11:58:03 +02:00
|
|
|
|
2024-07-26 14:06:53 +02:00
|
|
|
let axum_response: Response<Body> = router
|
2024-07-24 23:39:27 +02:00
|
|
|
.as_service()
|
2024-07-25 09:21:17 +02:00
|
|
|
.ready()
|
|
|
|
.await
|
2024-08-14 10:58:09 +02:00
|
|
|
.map_err(DesktopError::Infallible)?
|
2024-07-26 14:06:53 +02:00
|
|
|
.call(axum_request)
|
2024-07-25 09:21:17 +02:00
|
|
|
.await
|
2024-08-14 10:58:09 +02:00
|
|
|
.map_err(DesktopError::Infallible)?;
|
2024-07-24 11:58:03 +02:00
|
|
|
|
2024-07-26 14:06:53 +02:00
|
|
|
let (parts, body): (response::Parts, Body) = axum_response.into_parts();
|
2024-08-14 10:58:09 +02:00
|
|
|
let body: Bytes = to_bytes(body, usize::MAX).await?;
|
2024-07-24 14:21:37 +02:00
|
|
|
|
2024-07-26 14:06:53 +02:00
|
|
|
let tauri_response: Response<Vec<u8>> = Response::from_parts(parts, body.into());
|
2024-08-14 10:58:09 +02:00
|
|
|
Ok(tauri_response)
|
2024-07-24 11:58:03 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 20:08:45 +02:00
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
|
|
pub fn run() {
|
|
|
|
tauri::Builder::default()
|
|
|
|
.setup(|app| {
|
2024-07-26 14:06:53 +02:00
|
|
|
let assets_path: PathBuf = app
|
2024-07-23 20:08:45 +02:00
|
|
|
.path()
|
|
|
|
.resolve("assets", BaseDirectory::Resource)
|
2024-08-14 10:58:09 +02:00
|
|
|
.expect("Assets path should be resolvable");
|
2024-07-23 20:08:45 +02:00
|
|
|
|
2024-07-26 14:06:53 +02:00
|
|
|
// Adds Axum router to application state
|
2024-07-24 14:21:37 +02:00
|
|
|
// This makes it so we can retrieve it from any app instance (see bellow)
|
2024-07-26 14:06:53 +02:00
|
|
|
let router = Arc::new(Mutex::new(app::get_router(&assets_path)));
|
|
|
|
|
2024-07-23 20:08:45 +02:00
|
|
|
app.manage(router);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
2024-07-25 09:21:17 +02:00
|
|
|
.register_asynchronous_uri_scheme_protocol("axum", move |app, request, responder| {
|
|
|
|
// Retrieve the router from the application state and clone it for the async block
|
|
|
|
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
|
2024-07-24 14:21:37 +02:00
|
|
|
|
2024-07-25 09:21:17 +02:00
|
|
|
// Spawn a new async task to process the request
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
let router = router.lock().await;
|
2024-08-14 10:58:09 +02:00
|
|
|
match process_tauri_request(request, router).await {
|
|
|
|
Ok(response) => responder.respond(response),
|
|
|
|
Err(err) => {
|
|
|
|
let body = format!("Failed to process an axum:// request:\n{}", err);
|
|
|
|
responder.respond(
|
|
|
|
http::Response::builder()
|
|
|
|
.status(http::StatusCode::BAD_REQUEST)
|
|
|
|
.header(http::header::CONTENT_TYPE, "text/plain")
|
|
|
|
.body::<Vec<u8>>(body.into())
|
|
|
|
.expect("BAD_REQUEST response should be valid"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 09:21:17 +02:00
|
|
|
});
|
|
|
|
})
|
2024-07-23 20:08:45 +02:00
|
|
|
.run(tauri::generate_context!())
|
|
|
|
.expect("error while running tauri application");
|
|
|
|
}
|