feat: restructure project, implement askama templating #26

Merged
theo merged 21 commits from restructure-project into main 2024-07-26 14:42:03 +02:00
Showing only changes of commit d33140ebaf - Show all commits

View File

@ -2,18 +2,28 @@ use core::panic;
use std::sync::Arc; use std::sync::Arc;
use tauri::{path::BaseDirectory, Manager}; 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 tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
async fn process_tauri_request( async fn process_tauri_request(
request: tauri::http::Request<Vec<u8>>, request: TauriRequest<Vec<u8>>,
mut router: MutexGuard<'_, axum::Router>, mut router: MutexGuard<'_, axum::Router>,
) -> tauri::http::Response<Vec<u8>> { ) -> TauriResponse<Vec<u8>> {
// Convert the Tauri request to an Axum request
let (parts, body) = request.into_parts(); let (parts, body) = request.into_parts();
let body = axum::body::Body::from(body); let body = AxumBody::from(body);
let request = AxumRequest::from_parts(parts, body);
let request = axum::extract::Request::from_parts(parts, body);
// Process the request with the router
let response = match router.as_service().ready().await { let response = match router.as_service().ready().await {
Ok(ready_service) => ready_service.call(request).await, Ok(ready_service) => ready_service.call(request).await,
Err(_error) => panic!("Failed to get ready service"), Err(_error) => panic!("Failed to get ready service"),
@ -24,13 +34,14 @@ async fn process_tauri_request(
Err(_error) => panic!("Problem getting response from request."), Err(_error) => panic!("Problem getting response from request."),
}; };
// Convert the Axum response to a Tauri response
let (parts, body) = response.into_parts(); let (parts, body) = response.into_parts();
let body = match axum::body::to_bytes(body, usize::MAX).await { let body = to_bytes(body, usize::MAX).await {
Ok(bytes) => bytes.to_vec(), Ok(bytes) => bytes.to_vec(),
Err(_error) => panic!("Problem converting response body to bytes."), Err(_error) => panic!("Problem converting response body to bytes."),
}; };
let response = tauri::http::Response::from_parts(parts, body); let response = TauriResponse::from_parts(parts, body);
response response
} }
@ -39,32 +50,34 @@ async fn process_tauri_request(
pub fn run() { pub fn run() {
tauri::Builder::default() tauri::Builder::default()
.setup(|app| { .setup(|app| {
let resource_path_buf = app let assets_path = app
.path() .path()
.resolve("assets", BaseDirectory::Resource) .resolve("assets", BaseDirectory::Resource)
.expect("Path should be resolvable"); .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 // Adds the router to the 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(),
));
app.manage(router); app.manage(router);
Ok(()) Ok(())
}) })
.register_asynchronous_uri_scheme_protocol("axum", move |app, request, responder| { .register_asynchronous_uri_scheme_protocol(
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>()); "axum",
move |app, request, responder| {
// Retrieve the router from the application state and clone it for the async block
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
tauri::async_runtime::spawn(async move { // Spawn a new async task to process the request
let router = router.lock().await; tauri::async_runtime::spawn(async move {
let router = router.lock().await;
let response = process_tauri_request(request, router).await; let response = process_tauri_request(request, router).await;
responder.respond(response);
responder.respond(response); });
}); }
}) )
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }