Files
ipass__ipass/rust/gateway/src/api.rs
T
adminandClaude Opus 4.8 bf2528de0f refactor: depend on ../ipass-plugin-{domain,llm,apix} + ../ipass-core; strip local copies
Ruling 16:删除危险的 /static/plugins alias(暴露整个 workspace 根,含各仓
data/*.json),改为 scripts/collect-plugin-dist.sh 收集 core 壳与各启用插件
www dist 进 build/{shell,static-plugins}(build/ 已 gitignore),conf 与
tests 模板同构改指收集目录。www/*.html 多页管理台删除后由 ipass-core 壳接管。
deploy/data-seed/plugin-config.json 种子三插件配置(tracked)。

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-09-24 18:34:32 +08:00

87 lines
3.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 插件路由表(最终形态):仅剩用量趋势组合端点。
//!
//! 身份类端点在 `ipass-core::api`(CorePlugin),域名类在
//! `ipass-plugin-domain::api`,LLM 上游 / 用量端点在 `ipass-plugin-llm::api`,
//! API 代理端点在 `ipass-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<Status> {
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 天,上限由 `ipass_plugin_llm::config::TREND_DAYS_MAX` 兜住。
/// 按账号 / 按上游两个维度需要**先知道有哪些名字**(键里带日窗 id,扫不出来),
/// 所以这里把 users / upstreams / apix 的名册传进去,由 ipass_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::<i64>().ok())
.unwrap_or_else(ipass_plugin_llm::stats::trend_default_days);
let usernames: Vec<String> = users::all().into_iter().map(|u| u.username).collect();
// API 代理与 LLM 上游共用同一片用量键空间,只是 slot 带 `api/` 前缀
// (见 `ipass_plugin_apix::config::APIX_STATS_PREFIX`)——这里一起列出,趋势图才会出现它们
let slots: Vec<String> = ipass_plugin_llm::upstreams::all()
.into_iter()
.map(|(slot, _)| slot)
.chain(
ipass_plugin_apix::apix::all()
.into_iter()
.map(|(slug, _)| {
format!("{}{}", ipass_plugin_apix::config::APIX_STATS_PREFIX, slug)
}),
)
.collect();
http::ok(
r,
ipass_plugin_llm::stats::trend(util::now(), days, &usernames, &slots),
"ok",
)
}