60 lines
2.1 KiB
Rust
60 lines
2.1 KiB
Rust
|
use core::panic;
|
||
|
use std::sync::Arc;
|
||
|
|
||
|
use tauri::{path::BaseDirectory, Manager};
|
||
|
use tokio::sync::Mutex;
|
||
|
use tower::{Service, ServiceExt};
|
||
|
|
||
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||
|
pub fn run() {
|
||
|
tauri::Builder::default()
|
||
|
.setup(|app| {
|
||
|
let resource_path_buf = app
|
||
|
.path()
|
||
|
.resolve("assets", BaseDirectory::Resource)
|
||
|
.expect("Path should be resolvable");
|
||
|
|
||
|
let router = Arc::new(Mutex::new(
|
||
|
clego::get_router(resource_path_buf.as_path()).clone(),
|
||
|
));
|
||
|
|
||
|
app.manage(router);
|
||
|
|
||
|
Ok(())
|
||
|
})
|
||
|
.register_asynchronous_uri_scheme_protocol("axum", move |app, request, responder| {
|
||
|
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
|
||
|
|
||
|
tauri::async_runtime::spawn(async move {
|
||
|
let mut router = router.lock().await;
|
||
|
|
||
|
let (parts, body) = request.into_parts();
|
||
|
let body = axum::body::Body::from(body);
|
||
|
|
||
|
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."),
|
||
|
};
|
||
|
|
||
|
let (parts, body) = response.into_parts();
|
||
|
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);
|
||
|
|
||
|
responder.respond(response);
|
||
|
});
|
||
|
})
|
||
|
.run(tauri::generate_context!())
|
||
|
.expect("error while running tauri application");
|
||
|
}
|