feat: bind_address 绑定失败后尝试 fallback 到默认地址,避免无法启动 web 服务 (#590)

This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ
2026-01-05 12:13:50 +08:00
committed by GitHub
parent e2a24eff29
commit 97e1b6285e
3 changed files with 26 additions and 6 deletions

View File

@@ -13,6 +13,6 @@ pub(super) fn default_auth_token() -> String {
.collect()
}
pub(super) fn default_bind_address() -> String {
pub(crate) fn default_bind_address() -> String {
"0.0.0.0:12345".to_string()
}

View File

@@ -8,6 +8,7 @@ mod versioned_config;
pub use crate::config::args::{ARGS, version};
pub use crate::config::current::{CONFIG_DIR, Config};
pub(crate) use crate::config::default::default_bind_address;
pub use crate::config::handlebar::TEMPLATE;
pub use crate::config::item::{ConcurrentDownloadLimit, NFOTimeType, PathSafeTemplate, RateLimit, Trigger};
pub use crate::config::versioned_cache::VersionedCache;

View File

@@ -13,7 +13,7 @@ use sea_orm::DatabaseConnection;
use crate::api::{LogHelper, router};
use crate::bilibili::BiliClient;
use crate::config::VersionedConfig;
use crate::config::{VersionedConfig, default_bind_address};
#[derive(RustEmbed)]
#[preserve_source = false]
@@ -30,10 +30,29 @@ pub async fn http_server(
.layer(Extension(database_connection))
.layer(Extension(bili_client))
.layer(Extension(log_writer));
let bind_address = VersionedConfig::get().read().bind_address.to_owned();
let listener = tokio::net::TcpListener::bind(&bind_address)
.await
.context("bind address failed")?;
let (bind_address, listener) = {
let bind_address = VersionedConfig::get().read().bind_address.to_owned();
let listen_res = tokio::net::TcpListener::bind(&bind_address)
.await
.context("bind address failed");
match listen_res {
Ok(listener) => (bind_address, listener),
Err(e) => {
let default_bind_address = default_bind_address();
if default_bind_address == bind_address {
return Err(e);
}
warn!(
"绑定到地址 {} 失败:{:#},尝试绑定到默认地址 {}",
bind_address, e, default_bind_address
);
let listener = tokio::net::TcpListener::bind(&default_bind_address)
.await
.context("bind default address failed")?;
(default_bind_address, listener)
}
}
};
info!("开始运行管理页http://{}", bind_address);
Ok(axum::serve(listener, ServiceExt::<Request>::into_make_service(app)).await?)
}