Skip to content

Commit

Permalink
src: Change web server to run in the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso committed Oct 3, 2024
1 parent a3595bd commit c81691a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async fn main() -> Result<()> {

let _stats = stats::Stats::new(tokio::time::Duration::from_secs(1)).await;

web::start_server("0.0.0.0:8080".parse().unwrap());
web::run("0.0.0.0:8080".parse().unwrap()).await;

for (id, driver_info) in hub::drivers().await? {
debug!("Removing driver id {id:?} ({driver_info:?})");
Expand Down
27 changes: 13 additions & 14 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,23 +154,22 @@ struct AppState {

type ClientSender = mpsc::UnboundedSender<Message>;

pub fn start_server(address: String) {
pub async fn run(address: String) {
let router = SERVER.router.lock().unwrap().clone();
tokio::spawn(async move {
loop {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let listener = match tokio::net::TcpListener::bind(&address).await {
Ok(listener) => listener,
Err(e) => {
error!("WebServer TCP bind error: {}", e);
continue;
}
};
if let Err(error) = axum::serve(listener, router.clone()).into_future().await {
error!("WebServer error: {}", error);

loop {
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let listener = match tokio::net::TcpListener::bind(&address).await {
Ok(listener) => listener,
Err(e) => {
error!("WebServer TCP bind error: {}", e);
continue;
}
};
if let Err(error) = axum::serve(listener, router.clone()).into_future().await {
error!("WebServer error: {}", error);
}
});
}
}

pub fn configure_router<F>(modifier: F)
Expand Down

0 comments on commit c81691a

Please sign in to comment.