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 = [ dependencies = [
"app", "app",
"axum", "axum",
"bytes",
"http",
"tauri", "tauri",
"tauri-build", "tauri-build",
"tokio", "tokio",

View File

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