nomilo/src/routes/ui/zones.rs

76 lines
2.2 KiB
Rust
Raw Normal View History

2025-03-31 13:06:33 +00:00
use axum::extract::Request;
use axum::extract::{Query, Path, State};
use serde::Deserialize;
2022-04-29 16:04:12 +00:00
use serde_json::{Value, json};
2022-04-29 02:29:10 +00:00
2025-03-31 13:06:33 +00:00
use crate::validation;
2024-12-22 21:36:26 +00:00
use crate::AppState;
use crate::errors::Error;
2022-04-29 02:29:10 +00:00
use crate::template::Template;
2025-03-31 13:06:33 +00:00
use crate::resources::dns::friendly;
2022-04-29 02:29:10 +00:00
2025-03-31 13:06:33 +00:00
pub async fn get_records_page(
2024-12-22 21:36:26 +00:00
Path(zone_name): Path<String>,
State(app): State<AppState>,
2025-03-31 13:06:33 +00:00
request: Request,
2024-12-22 21:36:26 +00:00
) -> Result<Template<'static, Value>, Error> {
2025-03-31 13:06:33 +00:00
let zone = app.db.get_zone_by_name(&zone_name).await?;
let records = zone.get_records(app.records).await?;
let records = friendly::FriendlyRecords::from(records.clone());
Ok(Template::new(
2024-12-22 21:36:26 +00:00
"pages/records.html",
app.template_engine,
json!({
2025-03-31 13:06:33 +00:00
"current_zone": zone.name,
"records": records,
"url": request.uri().to_string(),
})
))
}
#[derive(Deserialize)]
pub struct NewRecordQuery {
subdomain: Option<String>,
config: Option<friendly::ConfigurationType>,
rtype: Option<friendly::FriendlyRType>,
}
pub async fn get_new_record_page(
Path(zone_name): Path<String>,
State(app): State<AppState>,
Query(params): Query<NewRecordQuery>,
request: Request,
) -> Result<Template<'static, Value>, Error> {
let zone = app.db.get_zone_by_name(&zone_name).await?;
// Syntax validation of the subdomain
let (name, domain_error) = if let Some(input_name) = params.subdomain {
if input_name.is_empty() {
(Some(zone.name.clone()), None)
} else {
let new_name = format!("{input_name}.{zone_name}");
let res = validation::normalize_domain(&new_name);
match res {
Err(error) => (Some(input_name), Some(error)),
Ok(new_name) => (Some(new_name), None),
}
}
} else {
(None, None)
};
Ok(Template::new(
"pages/new_record.html",
app.template_engine,
json!({
"current_zone": zone.name,
"new_record_name": name,
"domain_error": domain_error,
"config": params.config,
"rtype": params.rtype,
"url": request.uri().to_string(),
})
))
}