Skip to content

Commit

Permalink
Apply async to tokio task
Browse files Browse the repository at this point in the history
  • Loading branch information
parksb committed Aug 31, 2024
1 parent 0852012 commit 625026e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/adapter/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn create(
}

let proxy = &config.producer.proxy;
let html_content = fetch_content(&arg.link, proxy).unwrap();
let html_content = fetch_content(&arg.link, proxy).await.unwrap();
let is_feed = html_content.parse::<SyndicationFeed>().is_ok();

let link = if is_feed {
Expand All @@ -36,7 +36,7 @@ pub async fn create(
return (StatusCode::BAD_REQUEST, Json(false));
};

let title = match fetch_feed_title(&link, proxy) {
let title = match fetch_feed_title(&link, proxy).await {
Ok(title) => title,
Err(_) => return (StatusCode::INTERNAL_SERVER_ERROR, Json(false)),
};
Expand All @@ -49,7 +49,7 @@ pub async fn create(

match feed::create(conn, &arg) {
Ok(_) => {
let _ = create_new_items(conn, proxy);
let _ = create_new_items(conn, proxy).await;
(StatusCode::OK, Json(true))
}
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, Json(false)),
Expand Down
8 changes: 4 additions & 4 deletions src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ pub async fn serve(app_state: Arc<AppState>, addr: &str) {
.nest("/", protected)
.with_state(app_state.clone());

let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();

tokio::spawn(async move {
let AppState { conn, config, .. } = &*app_state;

loop {
let _ = create_new_items(conn, &config.producer.proxy);
let _ = create_new_items(conn, &config.producer.proxy).await;
tokio::time::sleep(std::time::Duration::from_secs(
config.producer.polling_frequency,
))
.await;
}
});

let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app).await.unwrap();
}

async fn authenticate(
Expand Down

0 comments on commit 625026e

Please sign in to comment.