saleor-apps-rs/sdk/src/lib.rs

89 lines
2.5 KiB
Rust
Raw Normal View History

2024-02-27 16:50:48 +00:00
pub mod apl;
2024-06-17 20:18:13 +00:00
#[cfg(feature = "bridge")]
pub mod bridge;
2024-03-06 15:42:04 +00:00
pub mod config;
2024-02-27 16:50:48 +00:00
pub mod headers;
2024-03-12 19:48:20 +00:00
pub mod locales;
2024-02-27 16:50:48 +00:00
pub mod manifest;
2024-05-22 22:21:13 +00:00
#[cfg(feature = "middleware")]
2024-03-06 15:42:04 +00:00
pub mod middleware;
2024-02-27 16:50:48 +00:00
pub mod webhooks;
use anyhow::bail;
2024-03-06 15:42:04 +00:00
use apl::{AplType, APL};
use config::Config;
2024-02-27 16:50:48 +00:00
use serde::{Deserialize, Serialize};
2024-05-22 23:07:04 +00:00
#[cfg(feature = "file_apl")]
use crate::apl::file_apl::FileApl;
2024-05-22 22:21:13 +00:00
#[cfg(feature = "redis_apl")]
2024-05-22 23:07:04 +00:00
use crate::apl::redis_apl::RedisApl;
2024-03-06 15:42:04 +00:00
2024-02-27 16:50:48 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthToken {
pub auth_token: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthData {
pub domain: Option<String>,
pub token: String,
pub saleor_api_url: String,
pub app_id: String,
pub jwks: Option<String>,
}
2024-03-12 19:48:20 +00:00
2024-02-27 16:50:48 +00:00
impl std::fmt::Display for AuthData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"(domain:{}\ntoken:{}\nsaleor_api_url:{}\napp_id:{}\njwks:{})",
self.domain.clone().unwrap_or_default(),
self.token,
self.saleor_api_url,
self.app_id,
self.jwks.clone().unwrap_or_default()
)
}
}
2024-03-06 15:42:04 +00:00
#[derive(Debug)]
pub struct SaleorApp {
pub apl: Box<dyn APL>,
}
impl SaleorApp {
pub fn new(config: &Config) -> anyhow::Result<SaleorApp> {
use AplType::{File, Redis};
2024-05-22 23:07:04 +00:00
fn decide_apl(config: &Config) -> anyhow::Result<Box<dyn APL>> {
match config.apl {
2024-05-22 22:21:13 +00:00
Redis => {
2024-05-22 23:07:04 +00:00
#[cfg(feature = "redis_apl")]
return Ok(Box::new(RedisApl::new(
&config.apl_url,
&config.app_api_base_url,
)?));
#[cfg(not(feature = "redis_apl"))]
{
bail!("Tried starting app with apl that wasn't present at compile time (cargo feature missing)")
2024-05-22 22:21:13 +00:00
}
}
File => {
2024-05-22 23:07:04 +00:00
#[cfg(feature = "file_apl")]
return Ok(Box::new(FileApl {
path: "apl.txt".to_owned(),
}));
#[cfg(not(feature = "file_apl"))]
{
bail!("Tried starting app with apl that wasn't present at compile time (cargo feature missing)")
2024-05-22 22:21:13 +00:00
}
}
2024-05-22 23:07:04 +00:00
}
}
let apl = decide_apl(config)?;
Ok(SaleorApp { apl })
2024-03-06 15:42:04 +00:00
}
2024-02-27 16:50:48 +00:00
}