Skip to content

Commit

Permalink
Basically whole app in one commit lol
Browse files Browse the repository at this point in the history
  • Loading branch information
djkato committed Jul 14, 2024
1 parent 9ef2313 commit 5fa0a3f
Show file tree
Hide file tree
Showing 20 changed files with 1,543 additions and 640 deletions.
6 changes: 6 additions & 0 deletions sitemap-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ envy.workspace = true
tracing.workspace = true
tracing-serde.workspace = true
tracing-subscriber.workspace = true
tracing-test = "0.2.5"
dotenvy.workspace = true
axum.workspace = true
saleor-app-sdk = { workspace = true, features = ["file_apl"] }
Expand All @@ -44,5 +45,10 @@ serde_cbor = "0.11.2"
# rayon = "1.10.0"
# itertools = "0.13.0"

[dev-dependencies]
random_word = { version = "0.4.3", features = ["en"] }
rand = "0.8.5"
serial_test = "3.1.1"

[build-dependencies]
cynic-codegen.workspace = true
6 changes: 2 additions & 4 deletions sitemap-generator/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Using sitemap-generator

To clear the cache, you can run the program with `./sitemap-generator --for-url https://my-saleor-api.com/graphql --cache-clear` or `docker compose --rm app-sitemap-generator sitemap-generator --for-url https://my-saleor-api.com/graphql --cache-clear`
To regenerate the cache, you can run the program with `./sitemap-generator --for-url https://my-saleor-api.com/graphql --cache-regenerate` or `docker compose --rm app-sitemap-generator sitemap-generator --for-url https://my-saleor-api.com/graphql --cache-regenerate`

You can also add both flags (do --cache-regenerate first), which will clear and then regenerate.
Only works for a single website. No locale support and no sitemap-index. Outputs Only pure sitemap.txt file. Downside is limit of 50 000 links. Upside: Easy to write c:
Partially supports relations of objects (Category-product), where the sitemap template can use info from both.

# Unofficial Saleor App Template

Expand Down
6 changes: 4 additions & 2 deletions sitemap-generator/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use saleor_app_sdk::{config::Config, manifest::AppManifest, SaleorApp};
use serde::{Deserialize, Serialize};
use tracing::level_filters::LevelFilter;

use crate::queries::event_subjects_updated::Event;
use crate::sitemap::event_handler::Event;

// Make our own error that wraps `anyhow::Error`.
pub struct AppError(anyhow::Error);
Expand Down Expand Up @@ -39,7 +39,7 @@ where

pub fn trace_to_std(config: &Config) -> anyhow::Result<()> {
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.with_default_directive(LevelFilter::INFO.into())
.from_env()?
.add_directive(format!("{}={}", env!("CARGO_PKG_NAME"), config.log_level).parse()?);
tracing_subscriber::fmt()
Expand Down Expand Up @@ -79,6 +79,8 @@ pub struct SitemapConfig {
pub collection_template: String,
#[serde(rename = "sitemap_index_hostname")]
pub index_hostname: String,
#[serde(rename = "sitemap_allowed_host")]
pub allowed_host: String,
}

impl SitemapConfig {
Expand Down
24 changes: 10 additions & 14 deletions sitemap-generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
dead_code
)]
#![feature(let_chains)]
#![deny(clippy::unwrap_used, clippy::expect_used)]
// #![deny(clippy::unwrap_used, clippy::expect_used)]
mod app;
mod queries;
mod routes;
mod sitemap;

#[cfg(debug_assertions)]
#[cfg(test)]
mod tests;

use axum::Router;
Expand All @@ -21,14 +21,9 @@ use saleor_app_sdk::{
webhooks::{AsyncWebhookEventType, WebhookManifestBuilder},
SaleorApp,
};
use sitemap::event_handler::EventHandler;
use std::sync::Arc;
use tokio::{
spawn,
sync::{
mpsc::{channel, Receiver},
Mutex,
},
};
use tokio::sync::Mutex;
use tracing::{debug, error, info};

use crate::{
Expand Down Expand Up @@ -62,16 +57,16 @@ async fn main() {
}

async fn create_app(config: &Config, sitemap_config: SitemapConfig) -> Router {
let saleor_app = SaleorApp::new(&config).unwrap();
let saleor_app = SaleorApp::new(config).unwrap();

debug!("Creating saleor App...");
let app_manifest = AppManifestBuilder::new(&config, cargo_info!())
let app_manifest = AppManifestBuilder::new(config, cargo_info!())
.add_permissions(vec![
AppPermission::ManageProducts,
AppPermission::ManagePages,
])
.add_webhook(
WebhookManifestBuilder::new(&config)
WebhookManifestBuilder::new(config)
.set_query(EVENTS_QUERY)
.add_async_events(vec![
AsyncWebhookEventType::ProductCreated,
Expand All @@ -92,17 +87,18 @@ async fn create_app(config: &Config, sitemap_config: SitemapConfig) -> Router {
.build();
debug!("Created AppManifest...");

//Task queue
let (sender, receiver) = tokio::sync::mpsc::channel(100);

EventHandler::start(sitemap_config.clone(), receiver);

let app_state = AppState {
task_queue_sender: sender,
sitemap_config,
manifest: app_manifest,
config: config.clone(),
target_channel: match dotenvy::var("CHANNEL_SLUG") {
Ok(v) => v,
Err(e) => {
Err(_) => {
error!("Missing channel slug, Saleor will soon deprecate product queries without channel specified.");
"".to_string()
}
Expand Down
16 changes: 8 additions & 8 deletions sitemap-generator/src/queries/event_subjects_updated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ pub struct ProductUpdated {
pub product: Option<Product>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct ProductDeleted {
pub product: Option<Product>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct ProductCreated {
pub product: Option<Product>,
}
Expand All @@ -123,12 +123,12 @@ pub struct PageUpdated {
pub page: Option<Page>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct PageDeleted {
pub page: Option<Page>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct PageCreated {
pub page: Option<Page>,
}
Expand All @@ -144,12 +144,12 @@ pub struct CollectionUpdated {
pub collection: Option<Collection>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct CollectionDeleted {
pub collection: Option<Collection>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct CollectionCreated {
pub collection: Option<Collection>,
}
Expand All @@ -165,12 +165,12 @@ pub struct CategoryUpdated {
pub category: Option<Category2>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct CategoryDeleted {
pub category: Option<Category2>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone)]
pub struct CategoryCreated {
pub category: Option<Category2>,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#[cynic::schema("saleor")]
mod schema {}
pub struct CategorisedProduct {
pub product: Product,
pub category_id: cynic::Id,
}

/*
query getCategoriesInitial {
categories(first: 50) {
Expand Down Expand Up @@ -38,46 +33,6 @@ query getCategoriesNext($after: String) {
}
}
}
query getCategoryProductsInitial($id: ID!, $channel: String!) {
category(id: $id) {
slug
id
updatedAt
products(first: 50, channel: $channel) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
slug
updatedAt
}
}
totalCount
}
}
}
query getCategoryProductsNext($id: ID!, $after: String!, $channel: String!) {
category(id: $id) {
products(first: 50, after: $after, channel: $channel) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
slug
updatedAt
}
}
}
}
}
*/

#[derive(cynic::QueryVariables, Debug, Clone)]
Expand Down
108 changes: 108 additions & 0 deletions sitemap-generator/src/queries/get_all_products.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#[cynic::schema("saleor")]
mod schema {}
/*
query getProductsInitial($id: ID!, $channel: String!) {
category(id: $id) {
slug
id
updatedAt
products(first: 50, channel: $channel) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
slug
updatedAt
category {
id
slug
}
}
}
totalCount
}
}
}
query getProductsNext($after: String!, $channel: String!) {
products(first: 50, after: $after, channel: $channel) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
id
slug
updatedAt
category {
id
slug
}
}
}
}
}
*/

#[derive(cynic::QueryVariables, Debug)]
pub struct GetProductsInitialVariables<'a> {
pub channel: &'a str,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct GetProductsNextVariables<'a> {
pub after: &'a str,
pub channel: &'a str,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetProductsInitialVariables")]
pub struct GetProductsInitial {
#[arguments(first: 50, channel: $channel)]
pub products: Option<ProductCountableConnection>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "GetProductsNextVariables")]
pub struct GetProductsNext {
#[arguments(first: 50, after: $after, channel: $channel)]
pub products: Option<ProductCountableConnection>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct ProductCountableConnection {
pub page_info: PageInfo,
pub edges: Vec<ProductCountableEdge>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
pub struct ProductCountableEdge {
pub node: Product,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
pub struct Product {
pub id: cynic::Id,
pub slug: String,
pub updated_at: DateTime,
pub category: Option<Category>,
}

#[derive(cynic::QueryFragment, Debug)]
pub struct PageInfo {
pub has_next_page: bool,
pub end_cursor: Option<String>,
}

#[derive(cynic::QueryFragment, Debug, Clone)]
pub struct Category {
pub id: cynic::Id,
pub slug: String,
}

#[derive(cynic::Scalar, Debug, Clone)]
pub struct DateTime(pub String);
3 changes: 2 additions & 1 deletion sitemap-generator/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod event_subjects_updated;
pub mod get_all_categories_n_products;
pub mod get_all_categories;
pub mod get_all_collections;
pub mod get_all_pages;
pub mod get_all_products;
2 changes: 2 additions & 0 deletions sitemap-generator/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub fn create_routes(state: AppState) -> Router {
#[cfg(not(debug_assertions))]
let serve_dir = ServeDir::new("./public").not_found_service(service);

// When working in workspace, cargo works relative to workspace dir, not app dir. This is
// dev-only workaround
#[cfg(debug_assertions)]
let serve_dir = ServeDir::new("./sitemap-generator/public").not_found_service(service);
//TODO: Query for everything using the app auth token
Expand Down
Loading

0 comments on commit 5fa0a3f

Please sign in to comment.