generated from pure_sagacity/rust-starter
added the server with a health check
added .env.example, also removed the old empty config file
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "server"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
axum = { version = "0.8.9", features = ["tokio", "ws"] }
|
||||
tokio = { version = "1.52.3", features = ["full"] }
|
||||
@@ -0,0 +1,28 @@
|
||||
use axum::{Router, routing::get};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Router::new().route("/health", get(health_check));
|
||||
|
||||
let address = {
|
||||
let port = std::env::var("PORT")
|
||||
.unwrap_or_else(|_| "8080".to_string())
|
||||
.parse::<u16>()
|
||||
.expect("PORT must be a valid u16");
|
||||
let host = std::env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
|
||||
format!("{}:{}", host, port)
|
||||
};
|
||||
|
||||
let listener = TcpListener::bind(&address)
|
||||
.await
|
||||
.expect("Failed to bind to address");
|
||||
|
||||
println!("Server running on http://{}", address);
|
||||
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn health_check() -> &'static str {
|
||||
"OK"
|
||||
}
|
||||
Reference in New Issue
Block a user