mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-23 09:09:18 +02:00
Compare commits
2 commits
bdfd5a74a6
...
5e9ed3738c
Author | SHA1 | Date | |
---|---|---|---|
5e9ed3738c | |||
7bee1f5bae |
3 changed files with 85 additions and 49 deletions
35
src/main.rs
35
src/main.rs
|
@ -1,4 +1,7 @@
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
use tokio::time::{sleep, Duration};
|
||||||
|
use v1::{run_kcomebacks_command, run_likedsongs_command, run_projects_command};
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
pub mod v1;
|
pub mod v1;
|
||||||
pub mod logger;
|
pub mod logger;
|
||||||
|
@ -10,10 +13,38 @@ pub use logger::Logger;
|
||||||
pub use tools::parse_ip;
|
pub use tools::parse_ip;
|
||||||
|
|
||||||
|
|
||||||
|
async fn periodic_script_runner() {
|
||||||
|
loop {
|
||||||
|
Logger::info("Running periodic scripts...");
|
||||||
|
// Run all Functions
|
||||||
|
let _ = run_kcomebacks_command();
|
||||||
|
let _ = run_projects_command();
|
||||||
|
let _ = run_likedsongs_command();
|
||||||
|
|
||||||
|
// Sleep for 6 hours
|
||||||
|
sleep(Duration::from_secs(6 * 60 * 60)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// load .env file
|
// Load .env file
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
|
|
||||||
server::serve().await;
|
// Start the api
|
||||||
|
let server_task = tokio::spawn(async {
|
||||||
|
server::serve().await;
|
||||||
|
});
|
||||||
|
|
||||||
|
// periodic script runner
|
||||||
|
let second_task = tokio::spawn(async {
|
||||||
|
// check if the local repository exists, if not, clone it
|
||||||
|
if !fs::metadata("./resources/turbo_octo_potato").is_ok() {
|
||||||
|
v1::run_setup().unwrap();
|
||||||
|
};
|
||||||
|
periodic_script_runner().await;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait for both tasks to complete
|
||||||
|
let _ = tokio::try_join!(server_task, second_task);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,18 @@ mod builtin;
|
||||||
mod debug;
|
mod debug;
|
||||||
mod kcomebacks;
|
mod kcomebacks;
|
||||||
mod projects;
|
mod projects;
|
||||||
mod update;
|
mod run;
|
||||||
|
|
||||||
|
pub use run::setup as run_setup;
|
||||||
|
pub use run::run_kcomebacks_command;
|
||||||
|
pub use run::run_projects_command;
|
||||||
|
pub use run::run_likedsongs_command;
|
||||||
|
|
||||||
pub use builtin::get_builtin_routes as get_v1_builtin_routes;
|
pub use builtin::get_builtin_routes as get_v1_builtin_routes;
|
||||||
pub use debug::get_debug_routes as get_v1_debug_routes;
|
pub use debug::get_debug_routes as get_v1_debug_routes;
|
||||||
pub use kcomebacks::get_kcomebacks_routes as get_v1_kcomebacks_routes;
|
pub use kcomebacks::get_kcomebacks_routes as get_v1_kcomebacks_routes;
|
||||||
pub use projects::get_project_routes as get_v1_project_routes;
|
pub use projects::get_project_routes as get_v1_project_routes;
|
||||||
pub use update::get_run_routes as get_v1_updates_routes;
|
pub use run::get_run_routes as get_v1_updates_routes;
|
||||||
|
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::BufRead;
|
// use std::io::BufRead;
|
||||||
use std::process::{Stdio, Command};
|
use std::process::{Stdio, Command};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tokio::sync::mpsc;
|
// use tokio::sync::mpsc;
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
use crate::error_responses::InternalServerError;
|
use crate::error_responses::InternalServerError;
|
||||||
|
@ -80,7 +80,7 @@ async fn sync_liked_songs() -> Result<impl warp::Reply, warp::Rejection> {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup() -> Result<(), git2::Error> {
|
pub fn setup() -> Result<(), git2::Error> {
|
||||||
let repository_url = "https://github.com/JonasunderscoreJones/turbo-octo-potato.git";
|
let repository_url = "https://github.com/JonasunderscoreJones/turbo-octo-potato.git";
|
||||||
let local_directory = "resources/turbo_octo_potato";
|
let local_directory = "resources/turbo_octo_potato";
|
||||||
|
|
||||||
|
@ -131,8 +131,8 @@ fn setup() -> Result<(), git2::Error> {
|
||||||
// run_command with python file and args as parameters
|
// run_command with python file and args as parameters
|
||||||
|
|
||||||
|
|
||||||
fn run_kcomebacks_command() -> Result<(), std::io::Error> {
|
pub fn run_kcomebacks_command() -> Result<(), std::io::Error> {
|
||||||
let (tx, mut rx) = mpsc::channel(1);
|
// let (tx, mut rx) = mpsc::channel(1);
|
||||||
|
|
||||||
task::spawn_blocking(move || {
|
task::spawn_blocking(move || {
|
||||||
let mut child = Command::new("python3")
|
let mut child = Command::new("python3")
|
||||||
|
@ -143,20 +143,20 @@ fn run_kcomebacks_command() -> Result<(), std::io::Error> {
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("failed to execute child");
|
.expect("failed to execute child");
|
||||||
|
|
||||||
let stdout = child.stdout.as_mut().unwrap();
|
// let stdout = child.stdout.as_mut().unwrap();
|
||||||
|
|
||||||
let mut reader = std::io::BufReader::new(stdout);
|
// let mut reader = std::io::BufReader::new(stdout);
|
||||||
|
|
||||||
let mut line = String::new();
|
// let mut line = String::new();
|
||||||
|
|
||||||
loop {
|
// loop {
|
||||||
let len = reader.read_line(&mut line).unwrap();
|
// let len = reader.read_line(&mut line).unwrap();
|
||||||
if len == 0 {
|
// if len == 0 {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
tx.blocking_send(line.clone()).unwrap();
|
// tx.blocking_send(line.clone()).unwrap();
|
||||||
line.clear();
|
// line.clear();
|
||||||
}
|
// }
|
||||||
|
|
||||||
child.wait().unwrap();
|
child.wait().unwrap();
|
||||||
});
|
});
|
||||||
|
@ -166,13 +166,13 @@ fn run_kcomebacks_command() -> Result<(), std::io::Error> {
|
||||||
// Logger::info(&format!("[/v1/kcomebacks/update]: {}", line));
|
// Logger::info(&format!("[/v1/kcomebacks/update]: {}", line));
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
Logger::info("kcomebacks updated");
|
Logger::info("Updating kcomebacks...");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_projects_command() -> Result<(), std::io::Error> {
|
pub fn run_projects_command() -> Result<(), std::io::Error> {
|
||||||
let (tx, mut rx) = mpsc::channel(1);
|
// let (tx, mut rx) = mpsc::channel(1);
|
||||||
|
|
||||||
task::spawn_blocking(move || {
|
task::spawn_blocking(move || {
|
||||||
let mut child = Command::new("python3")
|
let mut child = Command::new("python3")
|
||||||
|
@ -183,20 +183,20 @@ fn run_projects_command() -> Result<(), std::io::Error> {
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("failed to execute child");
|
.expect("failed to execute child");
|
||||||
|
|
||||||
let stdout = child.stdout.as_mut().unwrap();
|
// let stdout = child.stdout.as_mut().unwrap();
|
||||||
|
|
||||||
let mut reader = std::io::BufReader::new(stdout);
|
// let mut reader = std::io::BufReader::new(stdout);
|
||||||
|
|
||||||
let mut line = String::new();
|
// let mut line = String::new();
|
||||||
|
|
||||||
loop {
|
// loop {
|
||||||
let len = reader.read_line(&mut line).unwrap();
|
// let len = reader.read_line(&mut line).unwrap();
|
||||||
if len == 0 {
|
// if len == 0 {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
tx.blocking_send(line.clone()).unwrap();
|
// tx.blocking_send(line.clone()).unwrap();
|
||||||
line.clear();
|
// line.clear();
|
||||||
}
|
// }
|
||||||
|
|
||||||
child.wait().unwrap();
|
child.wait().unwrap();
|
||||||
});
|
});
|
||||||
|
@ -206,13 +206,13 @@ fn run_projects_command() -> Result<(), std::io::Error> {
|
||||||
// Logger::info(&format!("[/v1/projects/update]: {}", line));
|
// Logger::info(&format!("[/v1/projects/update]: {}", line));
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
Logger::info("projects updated");
|
Logger::info("Updating projects...");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_likedsongs_command() -> Result<(), std::io::Error> {
|
pub fn run_likedsongs_command() -> Result<(), std::io::Error> {
|
||||||
let (tx, mut rx) = mpsc::channel(1);
|
// let (tx, mut rx) = mpsc::channel(1);
|
||||||
|
|
||||||
task::spawn_blocking(move || {
|
task::spawn_blocking(move || {
|
||||||
let mut child = Command::new("python3")
|
let mut child = Command::new("python3")
|
||||||
|
@ -222,20 +222,20 @@ fn run_likedsongs_command() -> Result<(), std::io::Error> {
|
||||||
.spawn()
|
.spawn()
|
||||||
.expect("failed to execute child");
|
.expect("failed to execute child");
|
||||||
|
|
||||||
let stdout = child.stdout.as_mut().unwrap();
|
// let stdout = child.stdout.as_mut().unwrap();
|
||||||
|
|
||||||
let mut reader = std::io::BufReader::new(stdout);
|
// let mut reader = std::io::BufReader::new(stdout);
|
||||||
|
|
||||||
let mut line = String::new();
|
// let mut line = String::new();
|
||||||
|
|
||||||
loop {
|
// loop {
|
||||||
let len = reader.read_line(&mut line).unwrap();
|
// let len = reader.read_line(&mut line).unwrap();
|
||||||
if len == 0 {
|
// if len == 0 {
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
tx.blocking_send(line.clone()).unwrap();
|
// tx.blocking_send(line.clone()).unwrap();
|
||||||
line.clear();
|
// line.clear();
|
||||||
}
|
// }
|
||||||
|
|
||||||
child.wait().unwrap();
|
child.wait().unwrap();
|
||||||
});
|
});
|
||||||
|
@ -245,7 +245,7 @@ fn run_likedsongs_command() -> Result<(), std::io::Error> {
|
||||||
// Logger::info(&format!("[/v1/synclikedsongs]: {}", line));
|
// Logger::info(&format!("[/v1/synclikedsongs]: {}", line));
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
Logger::info("liked songs synced");
|
Logger::info("Syncing liked songs...");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue