2024-04-03 17:47:23 +00:00
|
|
|
#![allow(
|
|
|
|
non_upper_case_globals,
|
|
|
|
clippy::large_enum_variant,
|
2024-04-03 18:03:28 +00:00
|
|
|
clippy::upper_case_acronyms,
|
|
|
|
dead_code
|
2024-04-03 17:47:23 +00:00
|
|
|
)]
|
2024-03-09 18:59:15 +00:00
|
|
|
#![feature(let_chains)]
|
|
|
|
#![deny(clippy::unwrap_used, clippy::expect_used)]
|
2024-03-06 15:42:04 +00:00
|
|
|
mod app;
|
|
|
|
mod queries;
|
|
|
|
mod routes;
|
2024-07-03 14:07:04 +00:00
|
|
|
mod sitemap;
|
|
|
|
mod test;
|
2024-03-06 15:42:04 +00:00
|
|
|
|
|
|
|
use saleor_app_sdk::{
|
|
|
|
config::Config,
|
2024-04-03 17:47:23 +00:00
|
|
|
manifest::{cargo_info, AppManifestBuilder, AppPermission},
|
|
|
|
webhooks::{AsyncWebhookEventType, WebhookManifestBuilder},
|
2024-03-06 15:42:04 +00:00
|
|
|
SaleorApp,
|
|
|
|
};
|
2024-03-09 18:59:15 +00:00
|
|
|
use std::sync::Arc;
|
2024-07-03 14:07:04 +00:00
|
|
|
use tokio::{
|
|
|
|
spawn,
|
|
|
|
sync::{
|
|
|
|
mpsc::{channel, Receiver},
|
|
|
|
Mutex,
|
|
|
|
},
|
|
|
|
};
|
2024-05-17 13:43:59 +00:00
|
|
|
use tracing::{debug, error, info};
|
2024-03-06 15:42:04 +00:00
|
|
|
|
|
|
|
use crate::{
|
2024-07-03 14:07:04 +00:00
|
|
|
app::{trace_to_std, AppState, SitemapConfig},
|
2024-03-06 15:42:04 +00:00
|
|
|
queries::event_subjects_updated::EVENTS_QUERY,
|
2024-03-18 15:12:39 +00:00
|
|
|
routes::{create_routes, register::regenerate},
|
2024-03-06 15:42:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> anyhow::Result<()> {
|
|
|
|
let config = Config::load()?;
|
2024-04-03 17:47:23 +00:00
|
|
|
trace_to_std(&config)?;
|
2024-03-06 15:42:04 +00:00
|
|
|
let sitemap_config = SitemapConfig::load()?;
|
|
|
|
debug!("Creating configs...");
|
|
|
|
|
|
|
|
let saleor_app = SaleorApp::new(&config)?;
|
|
|
|
|
|
|
|
debug!("Creating saleor App...");
|
2024-04-03 17:47:23 +00:00
|
|
|
let app_manifest = AppManifestBuilder::new(&config, cargo_info!())
|
2024-03-06 15:42:04 +00:00
|
|
|
.add_permissions(vec![
|
|
|
|
AppPermission::ManageProducts,
|
|
|
|
AppPermission::ManagePages,
|
|
|
|
])
|
|
|
|
.add_webhook(
|
2024-04-03 17:47:23 +00:00
|
|
|
WebhookManifestBuilder::new(&config)
|
2024-03-06 15:42:04 +00:00
|
|
|
.set_query(EVENTS_QUERY)
|
|
|
|
.add_async_events(vec![
|
|
|
|
AsyncWebhookEventType::ProductCreated,
|
|
|
|
AsyncWebhookEventType::ProductUpdated,
|
|
|
|
AsyncWebhookEventType::ProductDeleted,
|
|
|
|
AsyncWebhookEventType::CategoryCreated,
|
|
|
|
AsyncWebhookEventType::CategoryUpdated,
|
|
|
|
AsyncWebhookEventType::CategoryDeleted,
|
|
|
|
AsyncWebhookEventType::PageCreated,
|
|
|
|
AsyncWebhookEventType::PageUpdated,
|
|
|
|
AsyncWebhookEventType::PageDeleted,
|
|
|
|
AsyncWebhookEventType::CollectionCreated,
|
|
|
|
AsyncWebhookEventType::CollectionUpdated,
|
|
|
|
AsyncWebhookEventType::CollectionDeleted,
|
|
|
|
])
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.build();
|
|
|
|
debug!("Created AppManifest...");
|
2024-07-03 14:07:04 +00:00
|
|
|
|
|
|
|
//Task queue
|
|
|
|
let (sender, receiver) = tokio::sync::mpsc::channel(100);
|
|
|
|
|
2024-03-06 15:42:04 +00:00
|
|
|
let app_state = AppState {
|
2024-07-03 14:07:04 +00:00
|
|
|
task_queue_sender: sender,
|
2024-03-06 15:42:04 +00:00
|
|
|
sitemap_config,
|
|
|
|
manifest: app_manifest,
|
|
|
|
config: config.clone(),
|
2024-05-17 13:43:59 +00:00
|
|
|
target_channel: match dotenvy::var("CHANNEL_SLUG") {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(e) => {
|
|
|
|
error!("Missing channel slug, Saleor will soon deprecate product queries without channel specified.");
|
|
|
|
anyhow::bail!(e);
|
|
|
|
}
|
|
|
|
},
|
2024-03-06 15:42:04 +00:00
|
|
|
saleor_app: Arc::new(Mutex::new(saleor_app)),
|
|
|
|
};
|
|
|
|
debug!("Created AppState...");
|
2024-03-18 15:12:39 +00:00
|
|
|
|
2024-03-06 15:42:04 +00:00
|
|
|
let app = create_routes(app_state);
|
|
|
|
let listener = tokio::net::TcpListener::bind(
|
2024-03-21 15:21:01 +00:00
|
|
|
"0.0.0.0:".to_owned()
|
|
|
|
+ config
|
|
|
|
.app_api_base_url
|
|
|
|
.split(':')
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.get(2)
|
|
|
|
.unwrap_or(&"3000"),
|
2024-03-06 15:42:04 +00:00
|
|
|
)
|
|
|
|
.await?;
|
2024-04-03 17:47:23 +00:00
|
|
|
info!("listening on {}", listener.local_addr()?);
|
2024-03-06 15:42:04 +00:00
|
|
|
match axum::serve(listener, app).await {
|
|
|
|
Ok(o) => Ok(o),
|
|
|
|
Err(e) => anyhow::bail!(e),
|
|
|
|
}
|
|
|
|
}
|