Compare commits
3 Commits
16bf414962
...
9878e0b450
| Author | SHA1 | Date | |
|---|---|---|---|
| 9878e0b450 | |||
| 71c685a16d | |||
| 42a60477c9 |
5
.editorconfig
Normal file
5
.editorconfig
Normal file
@@ -0,0 +1,5 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
end_of_line = lf
|
||||
charset = utf-8-bom
|
||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
Cargo.lock
|
||||
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "rust-simple-web-api"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.8.1"
|
||||
chrono = "0.4.40"
|
||||
tokio = { version = "1.44.1", features = ["full"] }
|
||||
88
src/main.rs
Normal file
88
src/main.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
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<String> {
|
||||
|
||||
let current_time = Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
|
||||
axum::response::Html(format!("<h1>Current Time: {}</h1>", current_time))
|
||||
}
|
||||
|
||||
// pub fn router() -> Router {
|
||||
// Router::new()
|
||||
// .route("/", get(root))
|
||||
// .nest("/api", nest_router())
|
||||
// }
|
||||
|
||||
// async fn root() -> axum::response::Html<String> {
|
||||
// axum::response::Html("<h1>root</h1>".to_string())
|
||||
// }
|
||||
|
||||
// pub fn nest_router() -> Router {
|
||||
// Router::new()
|
||||
// .route("/", get(api))
|
||||
// .route("/test", get(handler))
|
||||
// }
|
||||
|
||||
// async fn api() -> axum::response::Html<String> {
|
||||
// axum::response::Html("<h1>api</h1>".to_string())
|
||||
// }
|
||||
|
||||
// async fn handler() -> Html<String> {
|
||||
|
||||
// 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: {}<br> RWT_ENV_VAR_1: {}<br> RWT_ENV_VAR_2: {}<br>", env_0, env_1, env_2);
|
||||
// Html(format!("<h1>{}</h1>", combined_env))
|
||||
// }
|
||||
|
||||
// async fn add(axum::extract::Path((a, b)): axum::extract::Path<(i32, i32)>) -> Html<String> {
|
||||
// let sum = a + b;
|
||||
// Html(format!("<h1>Sum: {}</h1>", sum))
|
||||
// }
|
||||
Reference in New Issue
Block a user