Redirect to web ui on root

This commit is contained in:
Igor Katson 2025-01-13 22:26:40 +00:00
parent 6cb4289917
commit e79b51ea1f
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5

View file

@ -2,13 +2,14 @@ use anyhow::Context;
use axum::body::Bytes; use axum::body::Bytes;
use axum::extract::{ConnectInfo, Path, Query, Request, State}; use axum::extract::{ConnectInfo, Path, Query, Request, State};
use axum::middleware::Next; use axum::middleware::Next;
use axum::response::IntoResponse; use axum::response::{IntoResponse, Redirect};
use axum::routing::{get, post}; use axum::routing::{get, post};
use base64::Engine; use base64::Engine;
use bencode::AsDisplay; use bencode::AsDisplay;
use buffers::ByteBuf; use buffers::ByteBuf;
use futures::future::BoxFuture; use futures::future::BoxFuture;
use futures::{FutureExt, TryStreamExt}; use futures::{FutureExt, TryStreamExt};
use http::request::Parts;
use http::{HeaderMap, HeaderValue, StatusCode}; use http::{HeaderMap, HeaderValue, StatusCode};
use itertools::Itertools; use itertools::Itertools;
@ -161,7 +162,19 @@ impl HttpApi {
) -> BoxFuture<'static, anyhow::Result<()>> { ) -> BoxFuture<'static, anyhow::Result<()>> {
let state = self.inner; let state = self.inner;
async fn api_root() -> impl IntoResponse { let api_root = move |parts: Parts| async move {
// If browser, and webui enabled, redirect to web
#[cfg(feature = "webui")]
{
if parts.headers.get("Accept").map_or(false, |h| {
std::str::from_utf8(h.as_bytes()).map_or(false, |s| s.contains("text/html"))
}) {
return Redirect::temporary("./web/").into_response();
}
}
(
[("Content-Type", "application/json")],
axum::Json(serde_json::json!({ axum::Json(serde_json::json!({
"apis": { "apis": {
"GET /": "list all available APIs", "GET /": "list all available APIs",
@ -188,8 +201,9 @@ impl HttpApi {
}, },
"server": "rqbit", "server": "rqbit",
"version": env!("CARGO_PKG_VERSION"), "version": env!("CARGO_PKG_VERSION"),
})) })),
} ).into_response()
};
async fn dht_stats(State(state): State<ApiState>) -> Result<impl IntoResponse> { async fn dht_stats(State(state): State<ApiState>) -> Result<impl IntoResponse> {
state.api_dht_stats().map(axum::Json) state.api_dht_stats().map(axum::Json)