use std::time::{SystemTime, UNIX_EPOCH};
use axum::Router;
use axum::routing::get;
use many_cpus::HardwareInfo;
use region_cached::{RegionCachedCopyExt, RegionCachedExt, region_cached};
region_cached!(static LAST_UPDATE: u128 = 0);
#[tokio::main]
async fn main() {
if std::env::var("IS_TESTING").is_ok() {
println!("Running in testing mode - exiting immediately to prevent infinite operation");
return;
}
let memory_region_count = HardwareInfo::max_memory_region_count();
println!("the current system has {memory_region_count} memory regions");
let app = Router::new()
.route("/", get(read))
.route("/update", get(update));
let listener = tokio::net::TcpListener::bind("0.0.0.0:1234").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn read() -> String {
let last_update_timestamp = LAST_UPDATE.get_cached();
format!("Last update: {last_update_timestamp}")
}
async fn update() -> String {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
LAST_UPDATE.set_global(now);
format!("Last update time set to: {now}")
}