saleor-apps-rs/sitemap-generator/src/main.rs

111 lines
3.4 KiB
Rust
Raw Normal View History

#![allow(
non_upper_case_globals,
clippy::large_enum_variant,
2024-04-03 18:03:28 +00:00
clippy::upper_case_acronyms,
dead_code
)]
#![feature(let_chains)]
2024-07-14 20:28:21 +00:00
// #![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;
2024-03-06 15:42:04 +00:00
2024-07-14 20:28:21 +00:00
#[cfg(test)]
2024-07-10 21:51:59 +00:00
mod tests;
use axum::Router;
2024-03-06 15:42:04 +00:00
use saleor_app_sdk::{
config::Config,
manifest::{cargo_info, AppManifestBuilder, AppPermission},
webhooks::{AsyncWebhookEventType, WebhookManifestBuilder},
2024-03-06 15:42:04 +00:00
SaleorApp,
};
2024-07-14 20:28:21 +00:00
use sitemap::event_handler::EventHandler;
use std::sync::Arc;
2024-07-14 20:28:21 +00:00
use tokio::sync::Mutex;
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,
routes::create_routes,
2024-03-06 15:42:04 +00:00
};
#[tokio::main]
2024-07-10 21:51:59 +00:00
async fn main() {
2024-03-06 15:42:04 +00:00
debug!("Creating configs...");
2024-07-10 21:51:59 +00:00
let config = Config::load().unwrap();
trace_to_std(&config).unwrap();
let sitemap_config = SitemapConfig::load().unwrap();
let app = create_app(&config, sitemap_config).await;
2024-03-06 15:42:04 +00:00
2024-07-10 21:51:59 +00:00
let listener = tokio::net::TcpListener::bind(
"0.0.0.0:".to_owned()
+ config
.app_api_base_url
.split(':')
.collect::<Vec<_>>()
.get(2)
.unwrap_or(&"3000"),
)
.await
.unwrap();
info!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
async fn create_app(config: &Config, sitemap_config: SitemapConfig) -> Router {
2024-07-14 20:28:21 +00:00
let saleor_app = SaleorApp::new(config).unwrap();
2024-03-06 15:42:04 +00:00
debug!("Creating saleor App...");
2024-07-14 20:28:21 +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-07-14 20:28:21 +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
let (sender, receiver) = tokio::sync::mpsc::channel(100);
2024-07-14 20:28:21 +00:00
EventHandler::start(sitemap_config.clone(), receiver);
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(),
target_channel: match dotenvy::var("CHANNEL_SLUG") {
Ok(v) => v,
2024-07-14 20:28:21 +00:00
Err(_) => {
error!("Missing channel slug, Saleor will soon deprecate product queries without channel specified.");
2024-07-10 21:51:59 +00:00
"".to_string()
}
},
2024-03-06 15:42:04 +00:00
saleor_app: Arc::new(Mutex::new(saleor_app)),
};
debug!("Created AppState...");
2024-07-10 21:51:59 +00:00
create_routes(app_state)
2024-03-06 15:42:04 +00:00
}