Skip to content

Commit a661a2f

Browse files
committed
feat: generate api for lick check
1 parent f8dcb1f commit a661a2f

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/main.rs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
use queensac::check_repository_links;
22

3-
use axum::{Router, routing::get};
3+
use axum::{
4+
Json, Router,
5+
routing::{get, post},
6+
serve,
7+
};
8+
use serde::Deserialize;
49
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+
}
518

619
async fn health_check() -> &'static str {
720
"OK"
821
}
922

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+
1035
#[tokio::main]
1136
async fn main() {
12-
let app = Router::new().route("/check", get(health_check));
37+
let app = app();
38+
let listener = TcpListener::bind("localhost:3000").await.unwrap();
1339

14-
let listener = tokio::net::TcpListener::bind("localhost:3000")
15-
.await
16-
.unwrap();
17-
18-
let repo_url = "https://github.com/reddevilmidzy/redddy-action";
19-
let interval_duration = Duration::from_secs(60);
20-
let _repo_check = check_repository_links(repo_url, interval_duration);
40+
serve(listener, app).await.unwrap();
41+
}
2142

22-
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))
2348
}

0 commit comments

Comments
 (0)