use axum::extract::Request; use axum::extract::{Query, Path, State}; use serde::Deserialize; use serde_json::{Value, json}; use crate::validation; use crate::AppState; use crate::errors::Error; use crate::template::Template; use crate::resources::dns::friendly; pub async fn get_records_page( Path(zone_name): Path, State(app): State, request: Request, ) -> Result, Error> { 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( "pages/records.html", app.template_engine, json!({ "current_zone": zone.name, "records": records, "url": request.uri().to_string(), }) )) } #[derive(Deserialize)] pub struct NewRecordQuery { subdomain: Option, config: Option, rtype: Option, } pub async fn get_new_record_page( Path(zone_name): Path, State(app): State, Query(params): Query, request: Request, ) -> Result, 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(), }) )) }