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;
|
|
|
|
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-11-15 00:37:52 +00:00
|
|
|
#[cfg(feature = "settings_manager")]
|
|
|
|
pub mod settings_manager;
|
2024-02-27 16:50:48 +00:00
|
|
|
pub mod webhooks;
|
|
|
|
|
2024-11-15 00:37:52 +00:00
|
|
|
#[cfg(feature = "redis_apl")]
|
|
|
|
use apl::redis_apl::RedisAplError;
|
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)]
|
2024-11-15 00:37:52 +00:00
|
|
|
pub struct SaleorApp<E> {
|
|
|
|
pub apl: Box<dyn APL<E>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum CreateSaleorAppError {
|
|
|
|
#[error("Feature needed to use this APL is not enabled in cargo.toml")]
|
|
|
|
MissingFeature(String),
|
|
|
|
#[cfg(feature = "redis_apl")]
|
|
|
|
#[error("failed creating redis_apl, {0}")]
|
|
|
|
#[cfg(feature = "redis_apl")]
|
|
|
|
RedisAplError(#[from] RedisAplError),
|
2024-03-06 15:42:04 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 00:37:52 +00:00
|
|
|
impl<E: std::error::Error> SaleorApp<E> {
|
|
|
|
pub fn new(config: &Config) -> Result<SaleorApp<E>, CreateSaleorAppError> {
|
2024-07-10 13:08:20 +00:00
|
|
|
use AplType::{File, Redis};
|
2024-11-15 00:37:52 +00:00
|
|
|
fn decide_apl<E>(config: &Config) -> Box<dyn APL<E>> {
|
2024-05-22 23:07:04 +00:00
|
|
|
match config.apl {
|
2024-05-22 22:21:13 +00:00
|
|
|
Redis => {
|
2024-05-22 23:07:04 +00:00
|
|
|
#[cfg(feature = "redis_apl")]
|
2024-11-15 00:37:52 +00:00
|
|
|
return Box::new(
|
|
|
|
RedisApl::new(&config.apl_url, &config.app_api_base_url)
|
|
|
|
.expect("failed creating redisapl"),
|
|
|
|
);
|
2024-05-22 23:07:04 +00:00
|
|
|
|
|
|
|
#[cfg(not(feature = "redis_apl"))]
|
|
|
|
{
|
2024-11-15 00:37:52 +00:00
|
|
|
return CreateSaleorAppError ::MissingFeature("Tried starting app with redis 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")]
|
2024-11-15 00:37:52 +00:00
|
|
|
return Box::new(FileApl {
|
2024-07-10 21:52:16 +00:00
|
|
|
path: config.apl_url.to_owned(),
|
2024-11-15 00:37:52 +00:00
|
|
|
});
|
2024-05-22 23:07:04 +00:00
|
|
|
#[cfg(not(feature = "file_apl"))]
|
|
|
|
{
|
2024-11-15 00:37:52 +00:00
|
|
|
return CreateSaleorAppError ::MissingFeature("Tried starting app with file 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
|
|
|
}
|
|
|
|
}
|
2024-11-15 00:37:52 +00:00
|
|
|
let apl = decide_apl(config);
|
2024-05-22 23:07:04 +00:00
|
|
|
Ok(SaleorApp { apl })
|
2024-03-06 15:42:04 +00:00
|
|
|
}
|
2024-02-27 16:50:48 +00:00
|
|
|
}
|