2024-07-23 20:08:45 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use tauri::{path::BaseDirectory, Manager};
|
2024-07-24 23:39:02 +02:00
|
|
|
use tauri::http::{
|
|
|
|
Request as TauriRequest,
|
|
|
|
Response as TauriResponse,
|
|
|
|
};
|
|
|
|
use axum::body::{
|
|
|
|
Body as AxumBody,
|
|
|
|
to_bytes,
|
|
|
|
};
|
|
|
|
use axum::extract::Request as AxumRequest;
|
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-07-24 14:21:37 +02:00
|
|
|
async fn process_tauri_request(
|
2024-07-24 23:39:02 +02:00
|
|
|
request: TauriRequest<Vec<u8>>,
|
2024-07-24 14:21:37 +02:00
|
|
|
mut router: MutexGuard<'_, axum::Router>,
|
2024-07-24 23:39:02 +02:00
|
|
|
) -> TauriResponse<Vec<u8>> {
|
|
|
|
// Convert the Tauri request to an Axum request
|
2024-07-24 14:21:37 +02:00
|
|
|
let (parts, body) = request.into_parts();
|
2024-07-24 23:39:02 +02:00
|
|
|
let body = AxumBody::from(body);
|
|
|
|
let request = AxumRequest::from_parts(parts, body);
|
2024-07-24 11:58:03 +02:00
|
|
|
|
2024-07-24 23:39:02 +02:00
|
|
|
// Process the request with the router
|
2024-07-24 23:39:27 +02:00
|
|
|
let response = router
|
|
|
|
.as_service()
|
|
|
|
.ready().await
|
|
|
|
.expect("Failed to get ready service")
|
|
|
|
.call(request).await
|
|
|
|
.expect("Failed to get response from router");
|
2024-07-24 11:58:03 +02:00
|
|
|
|
2024-07-24 23:39:02 +02:00
|
|
|
// Convert the Axum response to a Tauri response
|
2024-07-24 14:21:37 +02:00
|
|
|
let (parts, body) = response.into_parts();
|
2024-07-24 23:39:27 +02:00
|
|
|
let body = to_bytes(body, usize::MAX).await
|
|
|
|
.expect("Failed to convert body to bytes")
|
|
|
|
.to_vec();
|
2024-07-24 23:39:02 +02:00
|
|
|
let response = TauriResponse::from_parts(parts, body);
|
2024-07-24 14:21:37 +02:00
|
|
|
|
|
|
|
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-24 23:39:02 +02:00
|
|
|
let assets_path = app
|
2024-07-23 20:08:45 +02:00
|
|
|
.path()
|
|
|
|
.resolve("assets", BaseDirectory::Resource)
|
|
|
|
.expect("Path should be resolvable");
|
|
|
|
|
2024-07-24 14:21:37 +02:00
|
|
|
// Adds the router to the application state
|
|
|
|
// This makes it so we can retrieve it from any app instance (see bellow)
|
2024-07-24 23:39:02 +02:00
|
|
|
let router = Arc::new(Mutex::new(
|
|
|
|
app::get_router(assets_path.as_path()).clone(),
|
|
|
|
));
|
2024-07-23 20:08:45 +02:00
|
|
|
app.manage(router);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
2024-07-24 23:39:02 +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-24 23:39:02 +02:00
|
|
|
// Spawn a new async task to process the request
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
let router = router.lock().await;
|
|
|
|
let response = process_tauri_request(request, router).await;
|
|
|
|
responder.respond(response);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
)
|
2024-07-23 20:08:45 +02:00
|
|
|
.run(tauri::generate_context!())
|
|
|
|
.expect("error while running tauri application");
|
|
|
|
}
|