Skip to content

Commit

Permalink
fix separate wasm and unix deps
Browse files Browse the repository at this point in the history
  • Loading branch information
djkato committed Oct 4, 2024
1 parent b21d49a commit 3d916f1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
16 changes: 14 additions & 2 deletions app-template-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,23 @@ dotenvy = { workspace = true }
envy = { workspace = true }
cynic = { workspace = true, features = ["http-surf"], optional = true }
surf = { workspace = true, optional = true }
saleor-app-sdk = { workspace = true, optional = true }

[target.'cfg(target_family = "unix")'.dependencies]
saleor-app-sdk = { workspace = true, optional = true, features = [
"file_apl",
"redis_apl",
"tracing",
"middleware",
"webhook_utils",
] }

[target.'cfg(target_family = "wasm")'.dependencies]
saleor-app-sdk = { workspace = true, optional = true, features = ["bridge"] }

[build-dependencies]
cynic-codegen = { workspace = true, optional = true }


[features]
ssr = [
"dep:axum",
Expand Down Expand Up @@ -106,7 +118,7 @@ tailwind-input-file = "style/base.css"
assets-dir = "public"

# The IP and port (ex: 127.0.0.1:3000) where the server serves the content. Use it in your server setup.
site-addr = "127.0.0.1:3000"
site-addr = "0.0.0.0:3000"

# The port to use for automatic reload monitoring
reload-port = 3001
Expand Down
2 changes: 0 additions & 2 deletions app-template-ui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::routes::home::Home;
use leptos::*;
use leptos_meta::*;
use leptos_router::*;
use saleor_app_sdk::bridge::AppBridge;

#[derive(Params, PartialEq)]
pub struct UrlAppParams {
Expand All @@ -15,7 +14,6 @@ pub struct UrlAppParams {
pub fn App() -> impl IntoView {
// Provides context that manages stylesheets, titles, meta tags, etc.
provide_meta_context();
let app_bridge = AppBridge::new(Some(true)).unwrap();
view! {
<Stylesheet id="leptos" href="/pkg/saleor-app-template-ui.css" />

Expand Down
71 changes: 47 additions & 24 deletions app-template-ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,34 @@ mod queries;

mod app;
mod components;
mod routes;
mod error_template;
mod routes;

#[tokio::main]
#[cfg(feature = "ssr")]
async fn main() -> Result<(), std::io::Error> {

use std::sync::Arc;
use axum::{middleware, routing::{get, post}, Router};
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes };
use app::*;
use axum::{
middleware,
routing::{get, post},
Router,
};
use fileserv::file_and_error_handler;
use saleor_app_sdk::{manifest::{extension::AppExtensionBuilder, AppExtensionMount, AppExtensionTarget}, middleware::verify_webhook_signature::webhook_signature_verifier};
use tokio::sync::Mutex;
use leptos::*;
use leptos_axum::{generate_route_list, LeptosRoutes};
use saleor_app_sdk::{
cargo_info,
config::Config,
manifest::{AppManifestBuilder, AppPermission},
webhooks::{AsyncWebhookEventType, WebhookManifestBuilder},
SaleorApp,
};
use saleor_app_sdk::{
manifest::{extension::AppExtensionBuilder, AppExtensionMount, AppExtensionTarget},
middleware::verify_webhook_signature::webhook_signature_verifier,
};
use std::sync::Arc;
use tokio::sync::Mutex;

use crate::routes::api::{manifest::manifest, register::register, webhooks::webhooks};

Expand All @@ -54,27 +60,41 @@ async fn main() -> Result<(), std::io::Error> {
AppExtensionBuilder::new()
.set_url("/extensions/order_to_pdf")
.set_label("Order to PDF")
.add_permissions(vec![AppPermission::ManageOrders, AppPermission::ManageProducts, AppPermission::ManageProductTypesAndAttributes])
.add_permissions(vec![
AppPermission::ManageOrders,
AppPermission::ManageProducts,
AppPermission::ManageProductTypesAndAttributes,
])
.set_mount(AppExtensionMount::OrderDetailsMoreActions)
.set_target(AppExtensionTarget::Popup)
.build()
.build(),
)
.build();

let app_state = AppState{
let app_state = AppState {
manifest: app_manifest,
config: config.clone(),
saleor_app: Arc::new(Mutex::new(saleor_app)),
leptos_options,
};

let state_1 = app_state.clone();
let app =
Router::new()
.leptos_routes_with_context(&app_state, routes,move || provide_context(state_1.clone()) , App)
let app = Router::new()
.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/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());

Expand All @@ -90,30 +110,35 @@ async fn main() -> Result<(), std::io::Error> {
.await?;
tracing::debug!("listening on {}", listener.local_addr()?);

let _= axum::serve(listener, app.into_make_service())
.await;
let _ = axum::serve(listener, app.into_make_service()).await;
Ok(())
}

#[cfg(not(feature = "ssr"))]
pub fn main() {
use saleor_app_sdk::bridge::AppBridge;
let app_bridge = AppBridge::new(Some(true)).unwrap();
// no client-side main function
// unless we want this to work with e.g., Trunk for a purely client-side app
// see lib.rs for hydration function instead
}


#[cfg(feature = "ssr")]
use saleor_app_sdk::config::Config;
#[cfg(feature = "ssr")]
pub fn trace_to_std(config: &Config) -> Result<(), envy::Error> {
pub fn trace_to_std(config: &Config) -> Result<(), envy::Error> {
use tracing::level_filters::LevelFilter;
use tracing_subscriber::EnvFilter;

let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env().unwrap()
.add_directive(format!("{}={}", env!("CARGO_PKG_NAME"), config.log_level).parse().unwrap());
.from_env()
.unwrap()
.add_directive(
format!("{}={}", env!("CARGO_PKG_NAME"), config.log_level)
.parse()
.unwrap(),
);
tracing_subscriber::fmt()
.with_max_level(config.log_level)
.with_env_filter(filter)
Expand All @@ -122,5 +147,3 @@ pub fn trace_to_std(config: &Config) -> Result<(), envy::Error> {
.init();
Ok(())
}


0 comments on commit 3d916f1

Please sign in to comment.