Huh wuh (#7)
Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
/// Seconds to sleep after finising a task
|
||||
pub const SECONDS_TO_SLEEP: u64 = 5;
|
||||
|
||||
pub const APP_NAME: &str = "catapult";
|
||||
|
||||
pub struct App {
|
||||
pub api_url: String,
|
||||
pub auth_url: String,
|
||||
pub service_username: String,
|
||||
pub service_passphrase: String,
|
||||
pub twilio_config: textsender_models::config::auxiliary::TwilioConfig,
|
||||
}
|
||||
|
||||
// TODO: Change this to non-async at some point.
|
||||
// Will require changes in other libraries
|
||||
pub async fn load_app() -> Result<App, std::io::Error> {
|
||||
use textsender_models::envy;
|
||||
let auth_url_env = envy::environment::get_env("AUTH_URL").await;
|
||||
let api_url_env = envy::environment::get_env("API_URL").await;
|
||||
let service_username_env = envy::environment::get_env("SERVICE_USERNAME").await;
|
||||
let service_passphrase_env = envy::environment::get_env("SERVICE_PASSPHRASE").await;
|
||||
|
||||
let envs = vec![
|
||||
auth_url_env,
|
||||
api_url_env,
|
||||
service_username_env,
|
||||
service_passphrase_env,
|
||||
];
|
||||
|
||||
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)));
|
||||
}
|
||||
}
|
||||
(true, None)
|
||||
};
|
||||
|
||||
let (envs_valid, reason) = check_envs(&envs);
|
||||
|
||||
if envs_valid {
|
||||
match textsender_models::config::auxiliary::load_config().await {
|
||||
Ok(twilio_config) => Ok(App {
|
||||
api_url: String::new(),
|
||||
auth_url: String::new(),
|
||||
service_username: String::new(),
|
||||
service_passphrase: String::new(),
|
||||
twilio_config: twilio_config,
|
||||
}),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
} else {
|
||||
Err(std::io::Error::other(reason.unwrap()))
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
pub mod app;
|
||||
pub mod service;
|
||||
pub mod version;
|
||||
|
||||
+19
-5
@@ -1,10 +1,24 @@
|
||||
// TODO: Move this somewhere else
|
||||
pub const SECONDS_TO_SLEEP: u64 = 5;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args = std::env::args().collect();
|
||||
if catapult::version::contains_version(&args) {
|
||||
catapult::version::print_version();
|
||||
std::process::exit(-1);
|
||||
}
|
||||
|
||||
let app = match catapult::app::load_app().await {
|
||||
Ok(app) => app,
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
std::process::exit(-1);
|
||||
}
|
||||
};
|
||||
|
||||
loop {
|
||||
println!("Do some work");
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(SECONDS_TO_SLEEP)).await;
|
||||
catapult::service::do_the_work(&app).await;
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(
|
||||
catapult::app::SECONDS_TO_SLEEP,
|
||||
))
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
pub async fn do_the_work(_app: &crate::app::App) {
|
||||
println!("Do some work");
|
||||
todo!(
|
||||
r#"
|
||||
Need to finish this
|
||||
|
||||
This function should fetch a token, check if the token is valid, and obtain a refresh token if
|
||||
it has expired.
|
||||
|
||||
Then do the real work.
|
||||
|
||||
1. Check the queue
|
||||
2. If there is an item, get the data,
|
||||
3. Evaluate if it is schedulable
|
||||
4. Get the events and iterate each one
|
||||
5. starting with the message
|
||||
6. Get the contact
|
||||
7. Send the message
|
||||
8. Record the message event response
|
||||
"#
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user