use axum::{
Json,
extract::State,
http::{StatusCode, header},
response::IntoResponse,
};
use serde_json::{Value, json};
use tracing::debug;
use crate::extractors::acme::AcmePostAsGet;
use crate::router::AppState;
use acme_proxy_core::routes;
pub const NO_STORE: (header::HeaderName, &str) = (header::CACHE_CONTROL, "no-store");
pub async fn get_health_check() -> Json<Value> {
debug!(event = "server_health_requested", outcome = "progress");
Json(json!({
"healthy": true,
}))
}
pub async fn get_directory(State(state): State<AppState>) -> Json<Value> {
debug!(event = "directory_requested", outcome = "progress");
let base = &state.profile.base_url;
let mut directory = json!({
"newNonce": format!("{base}{}", routes::NEW_NONCE),
"newAccount": format!("{base}{}", routes::NEW_ACCOUNT),
"newOrder": format!("{base}{}", routes::NEW_ORDER),
"revokeCert": format!("{base}{}", routes::REVOKE_CERT),
"keyChange": format!("{base}{}", routes::KEY_CHANGE),
"renewalInfo": format!("{base}{}", routes::RENEWAL_INFO),
});
let meta = &state.profile.meta;
let mut object = serde_json::Map::new();
if state.profile.eab.enabled {
object.insert("externalAccountRequired".to_string(), json!(true));
}
if !meta.terms_of_service.is_empty() {
object.insert(
"termsOfService".to_string(),
json!(meta.terms_of_service.clone()),
);
}
if !meta.website.is_empty() {
object.insert("website".to_string(), json!(meta.website.clone()));
}
if !meta.caa_identities.is_empty() {
object.insert(
"caaIdentities".to_string(),
json!(meta.caa_identities.clone()),
);
}
if !object.is_empty() {
directory["meta"] = Value::Object(object);
}
Json(directory)
}
pub async fn post_directory(state: State<AppState>, _: AcmePostAsGet) -> Json<Value> {
get_directory(state).await
}
pub async fn head_new_nonce() -> impl IntoResponse {
debug!(event = "nonce_new_head_requested", outcome = "progress");
(StatusCode::OK, [NO_STORE])
}
pub async fn get_new_nonce() -> impl IntoResponse {
debug!(event = "nonce_new_get_requested", outcome = "progress");
(StatusCode::NO_CONTENT, [NO_STORE])
}
pub async fn post_new_nonce(_: AcmePostAsGet) -> impl IntoResponse {
debug!(event = "nonce_new_post_requested", outcome = "progress");
(StatusCode::OK, [NO_STORE])
}