Refactoring (#15)
Reviewed-on: #15
This commit was merged in pull request #15.
This commit is contained in:
+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 envs = vec![
|
||||
api_url_env,
|
||||
auth_url_env,
|
||||
service_username_env,
|
||||
service_passphrase_env,
|
||||
&api_url_env,
|
||||
&auth_url_env,
|
||||
&service_username_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 {
|
||||
if env.value.is_empty() {
|
||||
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);
|
||||
|
||||
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() {
|
||||
Ok(twilio_config) => Ok(App {
|
||||
api_url: envs[0].value.clone(),
|
||||
auth_url: envs[1].value.clone(),
|
||||
service_username: envs[2].value.clone(),
|
||||
service_passphrase: envs[3].value.clone(),
|
||||
api_url,
|
||||
auth_url,
|
||||
service_username,
|
||||
service_passphrase,
|
||||
twilio_config,
|
||||
}),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
} 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() {
|
||||
Ok(app) => app,
|
||||
Ok(app) => std::sync::Arc::new(app),
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
std::process::exit(-1);
|
||||
@@ -16,9 +16,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
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 {
|
||||
Ok(token) => token,
|
||||
Ok(token) => std::sync::Arc::new(token),
|
||||
Err(err) => {
|
||||
eprintln!("Error getting token");
|
||||
eprintln!("Error: {err:?}");
|
||||
@@ -27,8 +29,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
};
|
||||
|
||||
let mut svc = catapult::service::core::Service {
|
||||
app: app.clone(),
|
||||
token,
|
||||
app: std::sync::Arc::clone(&app),
|
||||
token: std::sync::Arc::clone(&token),
|
||||
};
|
||||
|
||||
loop {
|
||||
@@ -36,7 +38,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
match auth.get_refresh_token(&svc.token).await {
|
||||
Ok(refresh) => {
|
||||
println!("Refresh token: {refresh:?}");
|
||||
svc.token = refresh;
|
||||
let refreshed = std::sync::Arc::new(refresh);
|
||||
svc.token = refreshed;
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
pub struct Auth {
|
||||
pub app: crate::app::App,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
}
|
||||
|
||||
impl Auth {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub struct Contact {
|
||||
pub app: crate::app::App,
|
||||
pub token: textsender_models::token::LoginResult,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||
}
|
||||
|
||||
impl Contact {
|
||||
|
||||
+14
-14
@@ -1,6 +1,6 @@
|
||||
pub struct Service {
|
||||
pub app: crate::app::App,
|
||||
pub token: textsender_models::token::Login,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
pub token: std::sync::Arc<textsender_models::token::Login>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
@@ -10,8 +10,8 @@ impl Service {
|
||||
use textsender_models::message::scheduling::ScheduledMessage;
|
||||
|
||||
let queue = super::queue::Queue {
|
||||
app: self.app.clone(),
|
||||
token: self.token.clone(),
|
||||
app: std::sync::Arc::clone(&self.app),
|
||||
token: std::sync::Arc::clone(&self.token),
|
||||
};
|
||||
let queue_item: Option<ScheduledMessage> = match queue.get_queue().await {
|
||||
Ok(item) => Some(item),
|
||||
@@ -37,8 +37,8 @@ impl Service {
|
||||
println!("Can schedule");
|
||||
|
||||
let events_req = super::event::Event {
|
||||
app: self.app.clone(),
|
||||
token: self.token.clone(),
|
||||
app: std::sync::Arc::clone(&self.app),
|
||||
token: std::sync::Arc::clone(&self.token),
|
||||
};
|
||||
|
||||
let scheduled_message_id = queue_item.id;
|
||||
@@ -51,20 +51,20 @@ impl Service {
|
||||
};
|
||||
|
||||
let contacts = super::contact::Contact {
|
||||
app: self.app.clone(),
|
||||
token: self.token.clone(),
|
||||
app: std::sync::Arc::clone(&self.app),
|
||||
token: std::sync::Arc::clone(&self.token),
|
||||
};
|
||||
let messages = super::message::Message {
|
||||
app: self.app.clone(),
|
||||
token: self.token.clone(),
|
||||
app: std::sync::Arc::clone(&self.app),
|
||||
token: std::sync::Arc::clone(&self.token),
|
||||
};
|
||||
let sch_msg = super::scheduler::ScheduledMessage {
|
||||
app: self.app.clone(),
|
||||
token: self.token.clone(),
|
||||
app: std::sync::Arc::clone(&self.app),
|
||||
token: std::sync::Arc::clone(&self.token),
|
||||
};
|
||||
let mer_req = super::mer::MessageEventResponse {
|
||||
app: self.app.clone(),
|
||||
token: self.token.clone(),
|
||||
app: std::sync::Arc::clone(&self.app),
|
||||
token: std::sync::Arc::clone(&self.token),
|
||||
};
|
||||
|
||||
let events = events.unwrap_or_default();
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub struct Event {
|
||||
pub app: crate::app::App,
|
||||
pub token: textsender_models::token::LoginResult,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||
}
|
||||
|
||||
impl Event {
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
pub struct MessageEventResponse {
|
||||
pub app: crate::app::App,
|
||||
pub token: textsender_models::token::LoginResult,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||
}
|
||||
|
||||
impl MessageEventResponse {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub struct Message {
|
||||
pub app: crate::app::App,
|
||||
pub token: textsender_models::token::LoginResult,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub struct Queue {
|
||||
pub app: crate::app::App,
|
||||
pub token: textsender_models::token::LoginResult,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||
}
|
||||
|
||||
impl Queue {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
pub struct ScheduledMessage {
|
||||
pub app: crate::app::App,
|
||||
pub token: textsender_models::token::LoginResult,
|
||||
pub app: std::sync::Arc<crate::app::App>,
|
||||
pub token: std::sync::Arc<textsender_models::token::LoginResult>,
|
||||
}
|
||||
|
||||
impl ScheduledMessage {
|
||||
|
||||
Reference in New Issue
Block a user