Compare commits
No commits in common. "f8d7f82c50c46fd0d723ad94c39e0ddc96a43201" and "8c38f0e4ba52e8943e8b3f292f60f02421b929ca" have entirely different histories.
f8d7f82c50
...
8c38f0e4ba
@ -1,41 +1,36 @@
|
||||
use core::panic;
|
||||
use std::sync::Arc;
|
||||
|
||||
use tauri::{path::BaseDirectory, Manager};
|
||||
use tauri::http::{
|
||||
Request as TauriRequest,
|
||||
Response as TauriResponse,
|
||||
};
|
||||
use axum::body::{
|
||||
Body as AxumBody,
|
||||
to_bytes,
|
||||
};
|
||||
use axum::extract::Request as AxumRequest;
|
||||
use tokio::sync::{Mutex, MutexGuard};
|
||||
use tower::{Service, ServiceExt};
|
||||
|
||||
async fn process_tauri_request(
|
||||
request: TauriRequest<Vec<u8>>,
|
||||
request: tauri::http::Request<Vec<u8>>,
|
||||
mut router: MutexGuard<'_, axum::Router>,
|
||||
) -> TauriResponse<Vec<u8>> {
|
||||
// Convert the Tauri request to an Axum request
|
||||
) -> tauri::http::Response<Vec<u8>> {
|
||||
let (parts, body) = request.into_parts();
|
||||
let body = AxumBody::from(body);
|
||||
let request = AxumRequest::from_parts(parts, body);
|
||||
let body = axum::body::Body::from(body);
|
||||
|
||||
// Process the request with the router
|
||||
let response = router
|
||||
.as_service()
|
||||
.ready().await
|
||||
.expect("Failed to get ready service")
|
||||
.call(request).await
|
||||
.expect("Failed to get response from router");
|
||||
let request = axum::extract::Request::from_parts(parts, body);
|
||||
|
||||
let response = match router.as_service().ready().await {
|
||||
Ok(ready_service) => ready_service.call(request).await,
|
||||
Err(_error) => panic!("Failed to get ready service"),
|
||||
};
|
||||
|
||||
let response = match response {
|
||||
Ok(response) => response,
|
||||
Err(_error) => panic!("Problem getting response from request."),
|
||||
};
|
||||
|
||||
// Convert the Axum response to a Tauri response
|
||||
let (parts, body) = response.into_parts();
|
||||
let body = to_bytes(body, usize::MAX).await
|
||||
.expect("Failed to convert body to bytes")
|
||||
.to_vec();
|
||||
let response = TauriResponse::from_parts(parts, body);
|
||||
let body = match axum::body::to_bytes(body, usize::MAX).await {
|
||||
Ok(bytes) => bytes.to_vec(),
|
||||
Err(_error) => panic!("Problem converting response body to bytes."),
|
||||
};
|
||||
|
||||
let response = tauri::http::Response::from_parts(parts, body);
|
||||
|
||||
response
|
||||
}
|
||||
@ -44,34 +39,32 @@ async fn process_tauri_request(
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.setup(|app| {
|
||||
let assets_path = app
|
||||
let resource_path_buf = app
|
||||
.path()
|
||||
.resolve("assets", BaseDirectory::Resource)
|
||||
.expect("Path should be resolvable");
|
||||
|
||||
let router = Arc::new(Mutex::new(
|
||||
app::get_router(resource_path_buf.as_path()).clone(),
|
||||
));
|
||||
|
||||
// Adds the router to the application state
|
||||
// This makes it so we can retrieve it from any app instance (see bellow)
|
||||
let router = Arc::new(Mutex::new(
|
||||
app::get_router(assets_path.as_path()).clone(),
|
||||
));
|
||||
app.manage(router);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.register_asynchronous_uri_scheme_protocol(
|
||||
"axum",
|
||||
move |app, request, responder| {
|
||||
// Retrieve the router from the application state and clone it for the async block
|
||||
.register_asynchronous_uri_scheme_protocol("axum", move |app, request, responder| {
|
||||
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
)
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
@ -4,8 +4,8 @@
|
||||
"identifier": "org.p4pillon.pharma.desktop",
|
||||
"build": {
|
||||
"beforeDevCommand": {
|
||||
"cwd": "../app",
|
||||
"script": "cargo run"
|
||||
"script": "cargo run",
|
||||
"cwd": "../clego"
|
||||
},
|
||||
"devUrl": "http://localhost:3000",
|
||||
"frontendDist": "axum://place.holder/"
|
||||
|
Loading…
Reference in New Issue
Block a user