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 c2b4264f32 - Show all commits

View File

@ -2,40 +2,43 @@ use core::panic;
use std::sync::Arc; use std::sync::Arc;
use tauri::{path::BaseDirectory, Manager}; use tauri::{path::BaseDirectory, Manager};
use tokio::sync::Mutex; use tokio::sync::{Mutex, MutexGuard};
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
async fn process_tauri_request(request: tauri::http::Request<>, router: axum:Router ){ async fn process_tauri_request(
let (parts, body) = request.into_parts(); request: tauri::http::Request<Vec<u8>>,
let body = axum::body::Body::from(body); mut router: MutexGuard<'_, axum::Router>,
) -> tauri::http::Response<Vec<u8>> {
let (parts, body) = request.into_parts();
let body = axum::body::Body::from(body);
let request = axum::extract::Request::from_parts(parts, body); let request = axum::extract::Request::from_parts(parts, body);
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"),
}; };
let response = match response { let response = match response {
Ok(response) => response, Ok(response) => response,
Err(_error) => panic!("Problem getting response from request."), Err(_error) => panic!("Problem getting response from request."),
}; };
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 = match axum::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 = tauri::http::Response::from_parts(parts, body);
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| {
// Create a router and adds it the the app state
let resource_path_buf = app let resource_path_buf = app
.path() .path()
.resolve("assets", BaseDirectory::Resource) .resolve("assets", BaseDirectory::Resource)
@ -45,6 +48,8 @@ pub fn run() {
clego::get_router(resource_path_buf.as_path()).clone(), 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); app.manage(router);
Ok(()) Ok(())
@ -53,7 +58,10 @@ pub fn run() {
let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>()); let router = Arc::clone(&app.state::<Arc<Mutex<axum::Router>>>());
tauri::async_runtime::spawn(async move { 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); responder.respond(response);
}); });
}) })