chore: reduce verbosity and propose alternative to named types

This commit is contained in:
theo 2024-07-26 14:06:53 +02:00
parent 65059b87d4
commit d6d487a727
No known key found for this signature in database
3 changed files with 31 additions and 27 deletions

2
Cargo.lock generated
View File

@ -701,6 +701,8 @@ version = "0.1.0"
dependencies = [
"app",
"axum",
"bytes",
"http",
"tauri",
"tauri-build",
"tokio",

View File

@ -19,4 +19,6 @@ tower = "0.4.13"
tokio = "1.39.1"
app = { path = "../app" }
http = "1.1.0"
bytes = "1.6.1"

View File

@ -1,54 +1,54 @@
use bytes::Bytes;
use http::{request, response, Request, Response};
use std::path::PathBuf;
use std::sync::Arc;
use axum::body::{to_bytes, Body as AxumBody};
use axum::extract::Request as AxumRequest;
use tauri::http::{Request as TauriRequest, Response as TauriResponse};
use tauri::{path::BaseDirectory, Manager};
use axum::body::{to_bytes, Body};
use axum::Router;
use tauri::path::BaseDirectory;
use tauri::Manager;
use tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt};
async fn process_tauri_request(
request: TauriRequest<Vec<u8>>,
mut router: MutexGuard<'_, axum::Router>,
) -> TauriResponse<Vec<u8>> {
// Convert the Tauri request to an Axum request
let (parts, body) = request.into_parts();
let body = AxumBody::from(body);
let request = AxumRequest::from_parts(parts, body);
tauri_request: Request<Vec<u8>>,
mut router: MutexGuard<'_, Router>,
) -> Response<Vec<u8>> {
let (parts, body): (request::Parts, Vec<u8>) = tauri_request.into_parts();
let axum_request: Request<Body> = Request::from_parts(parts, body.into());
// Process the request with the router
let response = router
let axum_response: Response<Body> = router
.as_service()
.ready()
.await
.expect("Failed to get ready service")
.call(request)
.expect("Failed to get ready service from router")
.call(axum_request)
.await
.expect("Failed to get response from router");
.expect("Could not get response from router");
// 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 (parts, body): (response::Parts, Body) = axum_response.into_parts();
let body: Bytes = to_bytes(body, usize::MAX).await.unwrap_or_default();
response
let tauri_response: Response<Vec<u8>> = Response::from_parts(parts, body.into());
tauri_response
}
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.setup(|app| {
let assets_path = app
let assets_path: PathBuf = app
.path()
.resolve("assets", BaseDirectory::Resource)
.expect("Path should be resolvable");
// Adds the router to the application state
// Adds Axum router to 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()));
let router = Arc::new(Mutex::new(app::get_router(&assets_path)));
app.manage(router);
Ok(())