From b8ae6fcb67bbc4bcc213b59b587cf2c10c13584a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Djk=C3=A1=C5=A5o?= Date: Mon, 17 Jun 2024 23:51:41 +0200 Subject: [PATCH] it builds! axum and leptos separate routes --- app-template-ui/Cargo.toml | 2 +- app-template-ui/src/app.rs | 2 +- app-template-ui/src/main.rs | 29 ++++++++++------------ app-template-ui/src/routes/api/manifest.rs | 2 +- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app-template-ui/Cargo.toml b/app-template-ui/Cargo.toml index 992d272..3b055bd 100644 --- a/app-template-ui/Cargo.toml +++ b/app-template-ui/Cargo.toml @@ -15,7 +15,7 @@ license = "MIT OR Apache-2.0" crate-type = ["cdylib", "rlib"] [dependencies] -axum = { workspace = true, optional = true } +axum = { workspace = true, optional = true, features = ["macros"] } console_error_panic_hook = { workspace = true } leptos = { workspace = true, features = ["nightly"] } anyhow = { workspace = true, optional = true } diff --git a/app-template-ui/src/app.rs b/app-template-ui/src/app.rs index b3afb65..00e5c1c 100644 --- a/app-template-ui/src/app.rs +++ b/app-template-ui/src/app.rs @@ -36,7 +36,7 @@ pub fn App() -> impl IntoView { } #[cfg(feature = "ssr")] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, axum::extract::FromRef)] pub struct AppState { pub saleor_app: std::sync::Arc>, pub config: saleor_app_sdk::config::Config, diff --git a/app-template-ui/src/main.rs b/app-template-ui/src/main.rs index 876e6cf..28af48c 100644 --- a/app-template-ui/src/main.rs +++ b/app-template-ui/src/main.rs @@ -21,20 +21,20 @@ mod error_template; async fn main() -> Result<(), std::io::Error> { use std::sync::Arc; - use axum::{middleware, routing::{post,get}, Router}; + use axum::{middleware, routing::{get, post}, Router}; use leptos::*; - use leptos_axum::{generate_route_list, LeptosRoutes}; + use leptos_axum::{generate_route_list, LeptosRoutes }; use app::*; use fileserv::file_and_error_handler; use saleor_app_sdk::middleware::verify_webhook_signature::webhook_signature_verifier; use tokio::sync::Mutex; -use saleor_app_sdk::{ - cargo_info, - config::Config, - manifest::{AppManifestBuilder, AppPermission}, - webhooks::{AsyncWebhookEventType, WebhookManifestBuilder}, - SaleorApp, -}; + use saleor_app_sdk::{ + cargo_info, + config::Config, + manifest::{AppManifestBuilder, AppPermission}, + webhooks::{AsyncWebhookEventType, WebhookManifestBuilder}, + SaleorApp, + }; use crate::routes::api::{manifest::manifest, register::register, webhooks::webhooks}; @@ -103,16 +103,13 @@ use saleor_app_sdk::{ let state_1 = app_state.clone(); let app = Router::new() - .layer(middleware::from_fn(webhook_signature_verifier)) - .route("/api/webhooks", post(webhooks)) - .route("/api/register", post(register)) - .route("/api/manifest", get(manifest)) - // .leptos_routes_with_context(&leptos_options, routes,move || provide_context(state_1.clone()) , App) - .leptos_routes(&leptos_options, routes, App) + .leptos_routes_with_context(&app_state, routes,move || provide_context(state_1.clone()) , App) .fallback(file_and_error_handler) + .route("/api/webhooks", post(webhooks).route_layer(middleware::from_fn(webhook_signature_verifier))) + .route("/api/register", post(register).route_layer(middleware::from_fn(webhook_signature_verifier))) + .route("/api/manifest", get(manifest)) .with_state(app_state.clone()); - let listener = tokio::net::TcpListener::bind( "0.0.0.0:".to_owned() + config diff --git a/app-template-ui/src/routes/api/manifest.rs b/app-template-ui/src/routes/api/manifest.rs index 787b366..b58dc18 100644 --- a/app-template-ui/src/routes/api/manifest.rs +++ b/app-template-ui/src/routes/api/manifest.rs @@ -3,6 +3,6 @@ use saleor_app_sdk::manifest::AppManifest; use crate::{app::AppState, error_template::AxumError}; -pub fn manifest(State(state): State) -> Result, AxumError> { +pub async fn manifest(State(state): State) -> Result, AxumError> { Ok(Json(state.manifest)) }