129 lines
4 KiB
Rust
129 lines
4 KiB
Rust
use axum::extract::{Query, Path, State, OriginalUri};
|
|
use axum::Extension;
|
|
use serde_json::{Value, json};
|
|
use unic_langid::LanguageIdentifier;
|
|
|
|
use crate::form::Node;
|
|
use crate::macros::append_errors;
|
|
use crate::AppState;
|
|
use crate::errors::{Error, error_map};
|
|
use crate::template::Template;
|
|
use crate::resources::dns::friendly::{self, NewRecordQuery, ConfigurationType, FromValue, NewSectionMail, NewSectionWeb, ToInternal};
|
|
use crate::resources::dns::internal;
|
|
|
|
pub async fn get_records_page(
|
|
Path(zone_name): Path<String>,
|
|
State(app): State<AppState>,
|
|
OriginalUri(url): OriginalUri,
|
|
Extension(lang): Extension<LanguageIdentifier>,
|
|
) -> Result<Template<'static, Value>, 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": url.to_string(),
|
|
"lang": lang.to_string(),
|
|
})
|
|
))
|
|
}
|
|
|
|
|
|
pub async fn get_new_record_page(
|
|
Path(zone_name): Path<String>,
|
|
State(app): State<AppState>,
|
|
Query(mut params): Query<NewRecordQuery>,
|
|
OriginalUri(url): OriginalUri,
|
|
Extension(lang): Extension<LanguageIdentifier>,
|
|
) -> Result<Template<'static, Value>, Error> {
|
|
let zone = app.db.get_zone_by_name(&zone_name).await?;
|
|
|
|
let mut errors = Vec::new();
|
|
append_errors!(params.validate(&zone.name), errors);
|
|
|
|
Ok(Template::new(
|
|
"pages/new_record.html",
|
|
app.template_engine,
|
|
json!({
|
|
"current_zone": zone.name,
|
|
"new_record_name": params.name,
|
|
"errors": error_map(errors),
|
|
"config": params.config,
|
|
"rtype": params.rtype,
|
|
"url": url.to_string(),
|
|
"lang": lang.to_string(),
|
|
})
|
|
))
|
|
}
|
|
|
|
pub async fn post_new_record(
|
|
Path(zone_name): Path<String>,
|
|
State(app): State<AppState>,
|
|
Query(mut params): Query<NewRecordQuery>,
|
|
OriginalUri(url): OriginalUri,
|
|
Extension(lang): Extension<LanguageIdentifier>,
|
|
form: Node,
|
|
) -> Result<Template<'static, Value>, Error> {
|
|
let zone = app.db.get_zone_by_name(&zone_name).await?;
|
|
let mut errors = Vec::new();
|
|
append_errors!(params.validate(&zone.name), errors);
|
|
|
|
if !errors.is_empty() || params.name.is_none() || !(params.config.is_none() ^ params.config.is_some()) {
|
|
// TODO: return 404
|
|
todo!()
|
|
}
|
|
|
|
let name = params.name.clone().unwrap();
|
|
let input_data = form.to_json_value();
|
|
|
|
let new_records = if errors.is_empty() {
|
|
let name = internal::Name::new(name);
|
|
|
|
let new_records = if let Some(config_type) = params.config.clone() {
|
|
match config_type {
|
|
ConfigurationType::Mail => {
|
|
NewSectionMail::from_value(input_data.clone())
|
|
.map(|section| section.internal(3600, name))
|
|
},
|
|
ConfigurationType::Web => {
|
|
NewSectionWeb::from_value(input_data.clone())
|
|
.map(|section| section.internal(3600, name))
|
|
},
|
|
}
|
|
} else if let Some(_rtype) = params.rtype {
|
|
unimplemented!()
|
|
} else {
|
|
unreachable!()
|
|
};
|
|
|
|
append_errors!(new_records, errors)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
if !errors.is_empty() {
|
|
Ok(Template::new(
|
|
"pages/new_record.html",
|
|
app.template_engine,
|
|
json!({
|
|
"current_zone": zone.name,
|
|
"new_record_name": params.name,
|
|
"input_data": input_data,
|
|
"errors": error_map(errors),
|
|
"config": params.config,
|
|
"rtype": params.rtype,
|
|
"url": url.to_string(),
|
|
"lang": lang.to_string(),
|
|
})
|
|
))
|
|
} else {
|
|
println!("{:#?}", new_records);
|
|
todo!()
|
|
}
|
|
|
|
}
|