use crate::api::response;
use axum::response::IntoResponse;
use chrono::Utc;
use serde_json::{Map, Value, json};
use std::env;
pub async fn root_handler() -> impl IntoResponse {
let pkg_name_raw = env!("CARGO_PKG_NAME");
let pkg_version = env!("CARGO_PKG_VERSION");
let repository = env!("CARGO_PKG_REPOSITORY");
let license = env!("CARGO_PKG_LICENSE");
let pkg_name_formatted = {
let lower = pkg_name_raw.to_lowercase();
let mut chars = lower.chars();
match chars.next() {
None => String::new(),
Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
}
};
let git_commit = env!("GIT_COMMIT_SHORT");
let rustc_version = env!("RUSTC_FULL_VERSION");
let cargo_version = env!("CARGO_FULL_VERSION");
let build_date = env!("BUILD_DATE");
let arch = env::consts::ARCH;
let os = env::consts::OS;
let request_timestamp = Utc::now().to_rfc3339();
let version_string = format!("{pkg_name_raw} {pkg_version} ({git_commit} {build_date})");
let mut build_map = Map::new();
build_map.insert(pkg_name_raw.to_lowercase(), Value::String(version_string));
build_map.insert("rust".to_owned(), Value::String(rustc_version.into()));
build_map.insert("cargo".to_owned(), Value::String(cargo_version.into()));
response::success(json!({
"package": {
"author": "Canmi(Canmi21) t@canmi.icu",
"version": format!("{} v{}", pkg_name_formatted, pkg_version),
"license": license,
"repository": repository,
},
"build": build_map, "runtime": {
"arch": arch,
"platform": os,
},
"timestamp": request_timestamp,
}))
}