Refactoring #15
Generated
+1
-1
@@ -132,7 +132,7 @@ checksum = "8ae3f5d315924270530207e2a68396c3cc547f6dca3fbdca317cfb1a51edb593"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "catapult"
|
name = "catapult"
|
||||||
version = "0.3.2"
|
version = "0.3.3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "catapult"
|
name = "catapult"
|
||||||
version = "0.3.2"
|
version = "0.3.3"
|
||||||
rust-version = "1.96.1"
|
rust-version = "1.96.1"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
|
|||||||
+17
-11
@@ -20,13 +20,13 @@ pub fn load_app() -> Result<App, std::io::Error> {
|
|||||||
let service_passphrase_env = envy::environment::get_env("SERVICE_PASSPHRASE");
|
let service_passphrase_env = envy::environment::get_env("SERVICE_PASSPHRASE");
|
||||||
|
|
||||||
let envs = vec![
|
let envs = vec![
|
||||||
api_url_env,
|
&api_url_env,
|
||||||
auth_url_env,
|
&auth_url_env,
|
||||||
service_username_env,
|
&service_username_env,
|
||||||
service_passphrase_env,
|
&service_passphrase_env,
|
||||||
];
|
];
|
||||||
|
|
||||||
let check_envs = |vs: &Vec<envy::EnvVar>| -> (bool, Option<String>) {
|
let check_envs = |vs: &Vec<&envy::EnvVar>| -> (bool, Option<String>) {
|
||||||
for env in vs {
|
for env in vs {
|
||||||
if env.value.is_empty() {
|
if env.value.is_empty() {
|
||||||
return (false, Some(format!("Key {} is not provided", env.key)));
|
return (false, Some(format!("Key {} is not provided", env.key)));
|
||||||
@@ -36,19 +36,25 @@ pub fn load_app() -> Result<App, std::io::Error> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let (envs_valid, reason) = check_envs(&envs);
|
let (envs_valid, reason) = check_envs(&envs);
|
||||||
|
|
||||||
if envs_valid {
|
if envs_valid {
|
||||||
|
let auth_url: String = auth_url_env.value;
|
||||||
|
let api_url: String = api_url_env.value;
|
||||||
|
let service_username: String = service_username_env.value;
|
||||||
|
let service_passphrase: String = service_passphrase_env.value;
|
||||||
match textsender_models::config::auxiliary::load_config() {
|
match textsender_models::config::auxiliary::load_config() {
|
||||||
Ok(twilio_config) => Ok(App {
|
Ok(twilio_config) => Ok(App {
|
||||||
api_url: envs[0].value.clone(),
|
api_url,
|
||||||
auth_url: envs[1].value.clone(),
|
auth_url,
|
||||||
service_username: envs[2].value.clone(),
|
service_username,
|
||||||
service_passphrase: envs[3].value.clone(),
|
service_passphrase,
|
||||||
twilio_config,
|
twilio_config,
|
||||||
}),
|
}),
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Err(std::io::Error::other(reason.unwrap()))
|
match reason {
|
||||||
|
Some(reason) => Err(std::io::Error::other(reason)),
|
||||||
|
None => Err(std::io::Error::other("No reason found")),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-6
@@ -7,7 +7,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let app = match catapult::app::load_app() {
|
let app = match catapult::app::load_app() {
|
||||||
Ok(app) => app,
|
Ok(app) => std::sync::Arc::new(app),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
std::process::exit(-1);
|
std::process::exit(-1);
|
||||||
@@ -16,9 +16,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
println!("App: {app:?}");
|
println!("App: {app:?}");
|
||||||
|
|
||||||
let auth = catapult::service::auth::Auth { app: app.clone() };
|
let auth = catapult::service::auth::Auth {
|
||||||
|
app: std::sync::Arc::clone(&app),
|
||||||
|
};
|
||||||
let token = match auth.get_token().await {
|
let token = match auth.get_token().await {
|
||||||
Ok(token) => token,
|
Ok(token) => std::sync::Arc::new(token),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error getting token");
|
eprintln!("Error getting token");
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
@@ -27,8 +29,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut svc = catapult::service::core::Service {
|
let mut svc = catapult::service::core::Service {
|
||||||
app: app.clone(),
|
app: std::sync::Arc::clone(&app),
|
||||||
token,
|
token: std::sync::Arc::clone(&token),
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
@@ -36,7 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
match auth.get_refresh_token(&svc.token).await {
|
match auth.get_refresh_token(&svc.token).await {
|
||||||
Ok(refresh) => {
|
Ok(refresh) => {
|
||||||
println!("Refresh token: {refresh:?}");
|
println!("Refresh token: {refresh:?}");
|
||||||
svc.token = refresh;
|
let refreshed = std::sync::Arc::new(refresh);
|
||||||
|
svc.token = refreshed;
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!("Error: {err:?}");
|
eprintln!("Error: {err:?}");
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
pub struct Auth {
|
pub struct Auth {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Auth {
|
impl Auth {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
pub struct Contact {
|
pub struct Contact {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Contact {
|
impl Contact {
|
||||||
|
|||||||
+14
-14
@@ -1,6 +1,6 @@
|
|||||||
pub struct Service {
|
pub struct Service {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::Login,
|
pub token: std::sync::Arc<textsender_models::token::Login>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Service {
|
impl Service {
|
||||||
@@ -10,8 +10,8 @@ impl Service {
|
|||||||
use textsender_models::message::scheduling::ScheduledMessage;
|
use textsender_models::message::scheduling::ScheduledMessage;
|
||||||
|
|
||||||
let queue = super::queue::Queue {
|
let queue = super::queue::Queue {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let queue_item: Option<ScheduledMessage> = match queue.get_queue().await {
|
let queue_item: Option<ScheduledMessage> = match queue.get_queue().await {
|
||||||
Ok(item) => Some(item),
|
Ok(item) => Some(item),
|
||||||
@@ -37,8 +37,8 @@ impl Service {
|
|||||||
println!("Can schedule");
|
println!("Can schedule");
|
||||||
|
|
||||||
let events_req = super::event::Event {
|
let events_req = super::event::Event {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
|
|
||||||
let scheduled_message_id = queue_item.id;
|
let scheduled_message_id = queue_item.id;
|
||||||
@@ -51,20 +51,20 @@ impl Service {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let contacts = super::contact::Contact {
|
let contacts = super::contact::Contact {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let messages = super::message::Message {
|
let messages = super::message::Message {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let sch_msg = super::scheduler::ScheduledMessage {
|
let sch_msg = super::scheduler::ScheduledMessage {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
let mer_req = super::mer::MessageEventResponse {
|
let mer_req = super::mer::MessageEventResponse {
|
||||||
app: self.app.clone(),
|
app: std::sync::Arc::clone(&self.app),
|
||||||
token: self.token.clone(),
|
token: std::sync::Arc::clone(&self.token),
|
||||||
};
|
};
|
||||||
|
|
||||||
let events = events.unwrap_or_default();
|
let events = events.unwrap_or_default();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
pub struct Event {
|
pub struct Event {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event {
|
impl Event {
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
pub struct MessageEventResponse {
|
pub struct MessageEventResponse {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MessageEventResponse {
|
impl MessageEventResponse {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
pub struct Message {
|
pub struct Message {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
pub struct Queue {
|
pub struct Queue {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Queue {
|
impl Queue {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
pub struct ScheduledMessage {
|
pub struct ScheduledMessage {
|
||||||
pub app: crate::app::App,
|
pub app: std::sync::Arc<crate::app::App>,
|
||||||
pub token: textsender_models::token::LoginResult,
|
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ScheduledMessage {
|
impl ScheduledMessage {
|
||||||
|
|||||||
Reference in New Issue
Block a user