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
3 changed files with 22 additions and 30 deletions
Showing only changes of commit 65059b87d4 - Show all commits

View File

@ -1,6 +1,6 @@
use ::app::get_router; use ::app::get_router;
use std::env;
use std::path::Path; use std::path::Path;
use std::env;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {

View File

@ -1,2 +1,2 @@
pub mod index;
pub mod hello; pub mod hello;
pub mod index;

View File

@ -1,15 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use tauri::{path::BaseDirectory, Manager}; use axum::body::{to_bytes, Body as AxumBody};
use tauri::http::{
Request as TauriRequest,
Response as TauriResponse,
};
use axum::body::{
Body as AxumBody,
to_bytes,
};
use axum::extract::Request as AxumRequest; use axum::extract::Request as AxumRequest;
use tauri::http::{Request as TauriRequest, Response as TauriResponse};
use tauri::{path::BaseDirectory, Manager};
use tokio::sync::{Mutex, MutexGuard}; use tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
@ -25,14 +19,17 @@ async fn process_tauri_request(
// Process the request with the router // Process the request with the router
let response = router let response = router
.as_service() .as_service()
.ready().await .ready()
.await
.expect("Failed to get ready service") .expect("Failed to get ready service")
.call(request).await .call(request)
.await
.expect("Failed to get response from router"); .expect("Failed to get response from router");
// Convert the Axum response to a Tauri response // Convert the Axum response to a Tauri response
let (parts, body) = response.into_parts(); let (parts, body) = response.into_parts();
let body = to_bytes(body, usize::MAX).await let body = to_bytes(body, usize::MAX)
.await
.expect("Failed to convert body to bytes") .expect("Failed to convert body to bytes")
.to_vec(); .to_vec();
let response = TauriResponse::from_parts(parts, body); let response = TauriResponse::from_parts(parts, body);
@ -51,27 +48,22 @@ pub fn run() {
// 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( let router = Arc::new(Mutex::new(app::get_router(assets_path.as_path()).clone()));
app::get_router(assets_path.as_path()).clone(),
));
app.manage(router); app.manage(router);
Ok(()) Ok(())
}) })
.register_asynchronous_uri_scheme_protocol( .register_asynchronous_uri_scheme_protocol("axum", move |app, request, responder| {
"axum", // Retrieve the router from the application state and clone it for the async block
move |app, request, responder| { let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
// Retrieve the router from the application state and clone it for the async block
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
// Spawn a new async task to process the request // Spawn a new async task to process the request
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
let router = router.lock().await; 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");
} }