//! 插件路由表(最终形态):仅剩用量趋势组合端点。 //! //! 身份类端点在 `ipass-core::api`(CorePlugin),域名类在 //! `plugin-domain::api`,LLM 上游 / 用量端点在 `plugin-llm::api`, //! API 代理端点在 `plugin-apix::api`。本表只剩 //! //! * **`/api/admin/trend`** —— **刻意留在 gateway**(对方案的有意偏离): //! 日桶键带日窗 id、按前缀扫不出来,趋势接口需要 users + upstreams + //! apix 三方名册,这是跨插件组合逻辑,属于装配根职责。 //! //! 鉴权语义与内核共用一套:`ipass_core::api::{Guard, Actor, run_guarded}`。 use ngx::core::Status; use ngx::ffi::ngx_http_request_t; use ipass_core::api::{Actor, Guard, run_guarded}; use ipass_core::{http, util}; use crate::users; // =========================================================================== // 路由 // =========================================================================== /// 路由表。刻意用精确匹配而非正则——接口路径是固定的, /// 正则只会引入「写错锚点导致前缀误匹配」的风险。 fn route(method: &str, uri: &str) -> Option<(Guard, Handler)> { let entry: (Guard, Handler) = match (method, uri) { ("GET", "/api/admin/trend") => (Guard::Admin, h_admin_trend), _ => return None, }; Some(entry) } type Handler = fn(*mut ngx_http_request_t, Actor) -> Status; /// 本表分发:认领则返回响应状态,不认领返回 `None`(交给下一个插件)。 pub fn dispatch(r: *mut ngx_http_request_t) -> Option { let method = http::method_str(r); let uri = http::uri(r); let Some((guard, handler)) = route(&method, &uri) else { return None; }; Some(run_guarded(r, guard, handler)) } // =========================================================================== // 用量趋势(组合根:需要三方名册) // =========================================================================== /// 管理员:LLM 用量趋势(图表数据源)。 /// /// `days` 缺省 14 天,上限由 `plugin_llm::config::TREND_DAYS_MAX` 兜住。 /// 按账号 / 按上游两个维度需要**先知道有哪些名字**(键里带日窗 id,扫不出来), /// 所以这里把 users / upstreams / apix 的名册传进去,由 plugin_llm::stats /// 逐个精确取键。 fn h_admin_trend(r: *mut ngx_http_request_t, _actor: Actor) -> Status { let p = http::params(r); let days = p .get("days") .and_then(|v| util::trim(v).parse::().ok()) .unwrap_or_else(plugin_llm::stats::trend_default_days); let usernames: Vec = users::all().into_iter().map(|u| u.username).collect(); // API 代理与 LLM 上游共用同一片用量键空间,只是 slot 带 `api/` 前缀 // (见 `plugin_apix::config::APIX_STATS_PREFIX`)——这里一起列出,趋势图才会出现它们 let slots: Vec = plugin_llm::upstreams::all() .into_iter() .map(|(slot, _)| slot) .chain( plugin_apix::apix::all() .into_iter() .map(|(slug, _)| { format!("{}{}", plugin_apix::config::APIX_STATS_PREFIX, slug) }), ) .collect(); http::ok( r, plugin_llm::stats::trend(util::now(), days, &usernames, &slots), "ok", ) }