From 9878e0b4501a5e9619ce6cdf43779e56bf4a9855 Mon Sep 17 00:00:00 2001 From: tymmkang Date: Sat, 22 Mar 2025 01:43:05 +0900 Subject: [PATCH] =?UTF-8?q?=ED=98=84=EC=9E=AC=20=EC=8B=9C=EA=B0=84?= =?UTF-8?q?=EC=9D=84=20=EC=B6=9C=EB=A0=A5=ED=95=98=EB=8A=94=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index e7a11a9..ac3e321 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,88 @@ -fn main() { - println!("Hello, world!"); +use tokio::signal; +use axum::Router; +use axum::routing::get; +// use axum::response::Html; +use chrono::Local; + +#[tokio::main] +async fn main() { + let listener = tokio::net::TcpListener::bind("0.0.0.0:3000") + .await + .unwrap(); + + println!("listening on {}", listener.local_addr().unwrap()); + axum::serve(listener, router()) + .with_graceful_shutdown(shutdown_signal()) + .await + .unwrap(); } + +async fn shutdown_signal() { + let ctrl_c = async { + signal::ctrl_c() + .await + .expect("failed to listen for ctrl-c event"); + }; + + #[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 => {}, + }; +} + +pub fn router() -> Router { + Router::new() + .route("/", get(root)) +} + +async fn root() -> axum::response::Html { + + let current_time = Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); + axum::response::Html(format!("

Current Time: {}

", current_time)) +} + +// pub fn router() -> Router { +// Router::new() +// .route("/", get(root)) +// .nest("/api", nest_router()) +// } + +// async fn root() -> axum::response::Html { +// axum::response::Html("

root

".to_string()) +// } + +// pub fn nest_router() -> Router { +// Router::new() +// .route("/", get(api)) +// .route("/test", get(handler)) +// } + +// async fn api() -> axum::response::Html { +// axum::response::Html("

api

".to_string()) +// } + +// async fn handler() -> Html { + +// let env_0 = std::env::var("RWT_ENV_VAR_0").unwrap_or_else(|_| "FALLBACK_0".to_string()); +// let env_1 = std::env::var("RWT_ENV_VAR_1").unwrap_or_else(|_| "FALLBACK_1".to_string()); +// let env_2 = std::env::var("RWT_ENV_VAR_2").unwrap_or_else(|_| "FALLBACK_2".to_string()); + +// let combined_env = format!("RWT_ENV_VAR_0: {}
RWT_ENV_VAR_1: {}
RWT_ENV_VAR_2: {}
", env_0, env_1, env_2); +// Html(format!("

{}

", combined_env)) +// } + +// async fn add(axum::extract::Path((a, b)): axum::extract::Path<(i32, i32)>) -> Html { +// let sum = a + b; +// Html(format!("

Sum: {}

", sum)) +// }