extract protocol logic to external function

This commit is contained in:
theo 2024-07-24 14:21:37 +02:00
parent 0e8514d906
commit c2b4264f32
No known key found for this signature in database

View File

@ -2,10 +2,13 @@ use core::panic;
use std::sync::Arc;
use tauri::{path::BaseDirectory, Manager};
use tokio::sync::Mutex;
use tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt};
async fn process_tauri_request(request: tauri::http::Request<>, router: axum:Router ){
async fn process_tauri_request(
request: tauri::http::Request<Vec<u8>>,
mut router: MutexGuard<'_, axum::Router>,
) -> tauri::http::Response<Vec<u8>> {
let (parts, body) = request.into_parts();
let body = axum::body::Body::from(body);
@ -28,14 +31,14 @@ let body = match axum::body::to_bytes(body, usize::MAX).await {
};
let response = tauri::http::Response::from_parts(parts, body);
response
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.setup(|app| {
// Create a router and adds it the the app state
let resource_path_buf = app
.path()
.resolve("assets", BaseDirectory::Resource)
@ -45,6 +48,8 @@ pub fn run() {
clego::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)
app.manage(router);
Ok(())
@ -53,7 +58,10 @@ pub fn run() {
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
tauri::async_runtime::spawn(async move {
let mut router = router.lock().await;
let router = router.lock().await;
let response = process_tauri_request(request, router).await;
responder.respond(response);
});
})