Feature: setup de Tauri v2 avec le routing fait par Axum #20

Merged
florian_briand merged 3 commits from feature-setup-tauri-v2-with-axum into main 2024-07-22 13:25:31 +02:00
4 changed files with 4561 additions and 0 deletions
Showing only changes of commit 83b2f7358d - Show all commits

4479
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -15,4 +15,7 @@ tauri = { version = "2.0.0-beta", features = [] }
tauri-plugin-shell = "2.0.0-beta"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
futures = "0.3.30"
axum = "0.7.5"
tower = "0.4.13"

View File

@ -1,6 +1,68 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use futures::executor::block_on;
use tauri::http::{Response, Uri};
// Enable the services methods on Axum Router
use tower::{Service, ServiceExt};
/*** ROUTEUR ***/
async fn route_test() -> &'static str {
"Hello, `/test/` !"
}
async fn route_root() -> &'static str {
"Hello, `/` !"
}
async fn route_fallback(uri: axum::http::Uri) -> (axum::http::StatusCode, String) {
(axum::http::StatusCode::NOT_FOUND, format!("No route for {uri}"))
}
/*** TAURI ***/
fn build_axum_request(uri: Uri) -> axum::http::Request<axum::body::Body> {
axum::http::Request::builder()
// .method("GET")
florian_briand marked this conversation as resolved
Review

commentaire oublié

commentaire oublié
.uri(uri.to_string())
.body(axum::body::Body::empty())
.unwrap()
}
async fn call_axum(uri: Uri) -> axum::http::Response<axum::body::Body> {
let mut routeur: axum::Router = axum::Router::new()
.route("/test", axum::routing::get(route_test))
.route("/", axum::routing::get(route_root))
.fallback(route_fallback);
let request = build_axum_request(uri);
routeur
.as_service()
.ready()
.await
.unwrap()
.call(request)
.await
.unwrap()
}
async fn build_tauri_response(axum_response: axum::http::Response<axum::body::Body>) -> Response<Vec<u8>> {
let (parts, body) = axum_response.into_parts();
let body_bytes = axum::body::to_bytes(body, usize::MAX).await.unwrap().to_vec();
Response::builder()
.status(parts.status.as_u16())
.header("Content-Type", "text/html")
.body(body_bytes)
.unwrap()
}
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
@ -9,8 +71,21 @@ fn greet(name: &str) -> String {
fn main() {
tauri::Builder::default()
// Tauri example using the tauri-plugin-shell
Outdated
Review

Je n'ai pas l'impression qu'on ai besoin du plugin shell, si?

Je n'ai pas l'impression qu'on ai besoin du plugin shell, si?

Il est là d'office avec le template autogénéré, je l'ai juste pas supprimé ; on le fait soit maintenant, soit ça sera fais plus tard, j'ai pas d'avis :)

Il est là d'office avec le template autogénéré, je l'ai juste pas supprimé ; on le fait soit maintenant, soit ça sera fais plus tard, j'ai pas d'avis :)
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![greet])
// Handling of the clego:// protocol through Axum
.register_asynchronous_uri_scheme_protocol("clego", |_app, request, responder| {
let uri = request.uri().clone();
std::thread::spawn(move || {
// Open this thread to execute asynchronous code avoid blocking the main thread while waiting for the response
let axum_response = block_on(call_axum(uri));
let response = block_on(build_tauri_response(axum_response));
responder.respond(response);
});
})
// Tauri execution
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@ -36,6 +36,10 @@
</form>
<p id="greet-msg"></p>
<a href="clego://test" id="clego-link">Open `clego://test`</a>
<a href="clego://0.0.0.0/test" id="clego-link">Open `clego://0.0.0.0/test`</a>
<a href="clego://0.0.0.0/foo" id="clego-link">Open `clego://0.0.0.0/foo`</a>
</div>
</body>
</html>