minor modification
This commit is contained in:
parent
a6c0f916b7
commit
7a9b3aba6a
@ -2,7 +2,8 @@ use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::{ErrorResponse, Html, Redirect},
|
||||
http::{HeaderMap, HeaderValue},
|
||||
response::{ErrorResponse, Html, IntoResponse, Redirect},
|
||||
routing::{get, post, IntoMakeService},
|
||||
Json, Router,
|
||||
};
|
||||
@ -10,28 +11,65 @@ use serde::Serialize;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::{
|
||||
web::templates::load_templates,
|
||||
task::{collect_local_address, MY_ADDRESS},
|
||||
types::{AppState, PageContext},
|
||||
task::{MY_ADDRESS, collect_local_address},
|
||||
web::templates::load_templates,
|
||||
};
|
||||
|
||||
async fn root(State(state): State<Arc<RwLock<AppState>>>) -> axum::response::Result<Html<String>> {
|
||||
let state = state.read().await;
|
||||
enum Response<T> {
|
||||
Html(Html<String>),
|
||||
Json(Json<T>),
|
||||
}
|
||||
|
||||
impl<T> IntoResponse for Response<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
match self {
|
||||
Response::Html(html) => html.into_response(),
|
||||
Response::Json(json) => json.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Default)]
|
||||
struct AddressResult {
|
||||
address: String,
|
||||
}
|
||||
|
||||
async fn root(
|
||||
State(state): State<Arc<RwLock<AppState>>>,
|
||||
headers: HeaderMap,
|
||||
) -> axum::response::Result<Response<AddressResult>> {
|
||||
let address_guard = MY_ADDRESS.lock().await;
|
||||
let address = address_guard.clone();
|
||||
drop(address_guard);
|
||||
|
||||
let ctx = match tera::Context::from_serialize(&PageContext {
|
||||
address,
|
||||
}) {
|
||||
let accept: Vec<&str> = headers
|
||||
.get("Accept")
|
||||
.map(|h| h.to_str().unwrap())
|
||||
.unwrap_or("text/html")
|
||||
.split(",")
|
||||
.collect();
|
||||
|
||||
tracing::info!(?accept, "accept");
|
||||
if accept.contains(&"text/html") {
|
||||
let state = state.read().await;
|
||||
|
||||
let ctx = match tera::Context::from_serialize(&PageContext { address }) {
|
||||
Ok(ctx) => ctx,
|
||||
Err(err) => return Err(ErrorResponse::from(format!("{err}"))),
|
||||
};
|
||||
match state.templates.render("index.html", &ctx) {
|
||||
Ok(result) => Ok(Html::from(result)),
|
||||
Ok(result) => Ok(Response::Html(Html::from(result))),
|
||||
Err(err) => Err(ErrorResponse::from(format!("{err}"))),
|
||||
}
|
||||
} else if accept.contains(&"application/json") {
|
||||
Ok(Response::Json(Json(AddressResult { address })))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Default)]
|
||||
@ -40,7 +78,9 @@ struct ReloadResult {
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
async fn reload(State(state): State<Arc<RwLock<AppState>>>) -> Result<Redirect, Json<ReloadResult>> {
|
||||
async fn reload(
|
||||
State(state): State<Arc<RwLock<AppState>>>,
|
||||
) -> Result<Redirect, Json<ReloadResult>> {
|
||||
let mut state = state.write_owned().await;
|
||||
if let Err(err) = state.templates.full_reload() {
|
||||
return Err(Json(ReloadResult {
|
||||
|
Loading…
x
Reference in New Issue
Block a user