|
1 | | -mod git; |
2 | | -mod link; |
3 | | -mod schedule; |
| 1 | +use queensac::check_repository_links; |
4 | 2 |
|
5 | | -use crate::schedule::check_repository_links; |
6 | | -use axum::{Router, routing::get}; |
| 3 | +use axum::{ |
| 4 | + Json, Router, |
| 5 | + routing::{get, post}, |
| 6 | + serve, |
| 7 | +}; |
| 8 | +use serde::Deserialize; |
7 | 9 | use std::time::Duration; |
| 10 | +use tokio::net::TcpListener; |
| 11 | + |
| 12 | +async fn spawn_repository_checker(repo_url: &str, interval: Duration) { |
| 13 | + let repo_url = repo_url.to_string(); |
| 14 | + tokio::spawn(async move { |
| 15 | + check_repository_links(&repo_url, interval).await; |
| 16 | + }); |
| 17 | +} |
8 | 18 |
|
9 | 19 | async fn health_check() -> &'static str { |
10 | 20 | "OK" |
11 | 21 | } |
12 | 22 |
|
| 23 | +#[derive(Deserialize)] |
| 24 | +struct CheckRequest { |
| 25 | + repo_url: String, |
| 26 | + interval_secs: u64, |
| 27 | +} |
| 28 | + |
| 29 | +async fn check_handler(Json(payload): Json<CheckRequest>) -> &'static str { |
| 30 | + let interval = Duration::from_secs(payload.interval_secs); |
| 31 | + spawn_repository_checker(&payload.repo_url, interval).await; |
| 32 | + "Repository checker started" |
| 33 | +} |
| 34 | + |
13 | 35 | #[tokio::main] |
14 | 36 | async fn main() { |
15 | | - let app = Router::new().route("/check", get(health_check)); |
| 37 | + let app = app(); |
| 38 | + let listener = TcpListener::bind("localhost:3000").await.unwrap(); |
16 | 39 |
|
17 | | - let listener = tokio::net::TcpListener::bind("localhost:3000") |
18 | | - .await |
19 | | - .unwrap(); |
20 | | - |
21 | | - let repo_url = "https://github.com/reddevilmidzy/redddy-action"; |
22 | | - let interval_duration = Duration::from_secs(60); |
23 | | - let _repo_check = check_repository_links(repo_url, interval_duration); |
| 40 | + serve(listener, app).await.unwrap(); |
| 41 | +} |
24 | 42 |
|
25 | | - axum::serve(listener, app).await.unwrap(); |
| 43 | +fn app() -> Router { |
| 44 | + Router::new() |
| 45 | + .route("/", get(|| async { "Sacrifice the Queen!!" })) |
| 46 | + .route("/health", get(health_check)) |
| 47 | + .route("/check", post(check_handler)) |
26 | 48 | } |
0 commit comments