feat(gateway): P0-C SSE 流式转发 + 上游 key 加密 + 共享/钱包/交易 API (v0.2.2) - #74
Merged
Merged
Conversation
- SSE 流式转发(src/gateway.rs):stream:true 走 forward_stream,reqwest bytes_stream 逐块透传(data:/event: 原样),content-type text/event-stream + cache-control no-cache;openai 流尾 usage(include_usage 最后 chunk)、 anthropic message_start input + message_delta output 解析计量;客户端断开 自动中止上游且不入账;连接阶段故障转移复用七条定案 - 上游 key 加密(src/crypto.rs 新增):AES-256-GCM,v1:<nonce>:<cipher> 格式, 主密钥 env ATP_MASTER_KEY → config [server].master_key → dev 随机+告警; 启动时旧明文 key 自动加密迁移(db::migrate_key_encryption);转发前解密 - 共享管理 API(src/routes/sharing.rs):POST /api/sharings 上架(加密落库)、 GET 列表(key 脱敏 sk-****xxxx + 收益/额度/状态/可用时间段)、 PATCH /api/sharings/:id 暂停/恢复/软删(paused/on/off);keys 表新增 available_days/start/end + note 列(v2 迁移 ensure_column 幂等补列) - 钱包/交易/仪表盘 API(src/routes/wallet.rs):GET /api/wallet(balance/ month_use/month_earn)、GET /api/transactions(type 过滤+分页+时间倒序)、 GET /api/dashboard(本月聚合+净变化+近 7 天序列) - 测试 49 个全过(新增 16:crypto 5 / db 迁移 2 / SSE 4 / sharing 3 / wallet 2), clippy + fmt 干净;live smoke 全过(SSE 逐块+[DONE]、DB 全 v1: 密文、 列表脱敏、wallet/transactions/dashboard 记账正确) Rant: 2026-08-18T10:36:04(P0-C SSE 流式转发 + 上游 key 加密 + 共享/钱包/交易 API)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
P0-C backend implementation (rant
2026-08-18T10:36:04— SSE 流式转发 + 上游 key 加密 + 共享/钱包/交易 API). Completes streaming support (required by Cursor/Claude Code), upstream key encryption at rest, and user-facing sharing/wallet/transaction APIs aligned with the prototype. Version v0.2.2.Changes
1. SSE streaming (
src/gateway.rs)stream:truerequests take a streaming path: reqwestbytes_streampassthrough chunk-by-chunk, keepingdata:/event:lines verbatimcontent-type: text/event-stream+cache-control: no-cacheusage(withstream_options.include_usage), Anthropicmessage_startinput_tokens +message_deltaoutput_tokens2. Upstream key encryption (
src/crypto.rsnew)aes-gcm), formatv1:<nonce_hex>:<cipher_hex>ATP_MASTER_KEY→ config[server].master_key→ dev random + warningdb::migrate_key_encryption: legacy plaintext keys auto-encrypted (idempotent)3. Sharing API (
src/routes/sharing.rsnew, prototype US-8/9)POST /api/sharings— list a key (encrypted at rest, DB never holds plaintext)GET /api/sharings— my sharings with masked keysk-****xxxx, used/quota/earn/status/available windowPATCH /api/sharings/:id— pause/resume/soft-delete (paused/on/off)keysgainsavailable_days/available_start/available_end/notecolumns (v2 migration, idempotentensure_column)4. Wallet / Transactions / Dashboard (
src/routes/wallet.rsnew)GET /api/wallet→{balance, month_use, month_earn}GET /api/transactions?type=&page=&page_size=— type filter (consume/earn/all), pagination, time descGET /api/dashboard— month aggregation by type + net change + last-7-days series (sparkline)Tests
cargo build+cargo clippy --all-targets -- -D warnings+cargo fmt --checkcleanstream:true→ SSE chunked with[DONE]+ correct headers; DB keys allv1:ciphertext (incl. migrated placeholder); sharing list masked; wallet/transactions/dashboard ledger correct (consume + earn 90/10)Notes
Closes rant
2026-08-18T10:36:04.876683(P0-C).