server cargo updates

This commit is contained in:
Robin Appelman 2024-02-23 20:33:39 +01:00
commit 781bd03ad2
3 changed files with 161 additions and 29 deletions

View file

@ -4,11 +4,13 @@ use axum::response::{IntoResponse, Response};
use axum::{routing::get, Json, Router};
use main_error::MainResult;
use std::env::var;
use std::net::SocketAddr;
use std::net::Ipv4Addr;
use std::str::FromStr;
use std::sync::Arc;
use steamid_ng::{SteamID, SteamIDError};
use thiserror::Error;
use tokio::net::TcpListener;
use tokio::signal;
use tracing::{debug, error, instrument};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use ugc_scraper::data::GameMode;
@ -71,12 +73,13 @@ async fn main() -> MainResult {
.route("/maps/:format", get(map_history))
.with_state(AppState::default());
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], port));
tracing::info!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;
let listener = TcpListener::bind((Ipv4Addr::new(127, 0, 0, 1), port)).await?;
tracing::info!("listening on http://{}", listener.local_addr().unwrap());
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
Ok(())
}
@ -199,3 +202,27 @@ async fn map_history(
let response = state.client.map_history(mode).await?;
Ok(Json(response))
}
async fn shutdown_signal() {
let ctrl_c = async {
signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
}