Skip to content

启动装配 ​

连接池(app.rs) ​

rust
let pool = PgPoolOptions::new()
    .max_connections(10)
    .min_connections(1)
    .acquire_timeout(Duration::from_secs(30))
    .idle_timeout(Duration::from_secs(600))
    .connect(database_url)
    .await?;
Ok(AppState::new(pool))

AppState 经 with_state 注入 handler。

路由与静态 ​

rust
pub fn create_app(state: AppState) -> Router {
    let static_files = ServeDir::new("public").append_index_html_on_directories(true);
    routes::api_router()
        .fallback_service(static_files)
        .with_state(state)
}

优先匹配 API;未匹配路径由 ServeDir("public") 提供静态文件(封面、MP3 等)。