Skip to content

Commit 6dbd8d1

Browse files
committed
Make strategy switch setup fork friendly
1 parent 151610e commit 6dbd8d1

16 files changed

Lines changed: 660 additions & 37 deletions

.github/workflows/manual-strategy-switch.yml

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ jobs:
155155
PLATFORM_SYNC_WORKFLOW: ${{ inputs.platform_sync_workflow }}
156156
STRATEGY_SWITCH_CONSOLE_URL: ${{ vars.STRATEGY_SWITCH_CONSOLE_URL }}
157157
STRATEGY_SWITCH_SYNC_TOKEN: ${{ secrets.STRATEGY_SWITCH_SYNC_TOKEN || secrets.RUNTIME_SETTINGS_GH_TOKEN }}
158+
RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON: ${{ vars.RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON }}
159+
RUNTIME_SETTINGS_LONGBRIDGE_REPO: ${{ vars.RUNTIME_SETTINGS_LONGBRIDGE_REPO }}
160+
RUNTIME_SETTINGS_IBKR_REPO: ${{ vars.RUNTIME_SETTINGS_IBKR_REPO }}
161+
RUNTIME_SETTINGS_SCHWAB_REPO: ${{ vars.RUNTIME_SETTINGS_SCHWAB_REPO }}
162+
RUNTIME_SETTINGS_FIRSTRADE_REPO: ${{ vars.RUNTIME_SETTINGS_FIRSTRADE_REPO }}
158163
steps:
159164
- name: Checkout
160165
uses: actions/checkout@v6
@@ -195,24 +200,7 @@ jobs:
195200
id: platform
196201
run: |
197202
set -euo pipefail
198-
case "${PLATFORM}" in
199-
longbridge)
200-
repo="QuantStrategyLab/LongBridgePlatform"
201-
;;
202-
ibkr)
203-
repo="QuantStrategyLab/InteractiveBrokersPlatform"
204-
;;
205-
schwab)
206-
repo="QuantStrategyLab/CharlesSchwabPlatform"
207-
;;
208-
firstrade)
209-
repo="QuantStrategyLab/FirstradePlatform"
210-
;;
211-
*)
212-
echo "Unsupported platform: ${PLATFORM}" >&2
213-
exit 2
214-
;;
215-
esac
203+
repo="$(python3 scripts/runtime_settings.py repository "${PLATFORM}")"
216204
echo "repository=${repo}" >> "$GITHUB_OUTPUT"
217205
218206
- name: Fetch existing service targets

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Notes:
6868

6969
## Useful docs
7070

71+
- [Fork guide for the strategy switch console](docs/strategy_switch_fork_guide.md)
7172
- [Strategy switch console Worker](web/strategy-switch-console/README.md)
7273
- [Strategy switch admin backend](docs/strategy_switch_admin_backend.md)
7374
- [Manual strategy switch permission-control plan](docs/manual_strategy_switch_permission_control.zh-CN.md)

README.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ confirm_apply=APPLY_AND_SYNC
6868

6969
## 延伸文档
7070

71+
- [策略切换控制台 Fork 指南](docs/strategy_switch_fork_guide.zh-CN.md)
7172
- [策略切换控制台 Worker](web/strategy-switch-console/README.zh-CN.md)
7273
- [策略切换登录权限后台方案](docs/strategy_switch_admin_backend.zh-CN.md)
7374
- [手动策略切换权限控制方案](docs/manual_strategy_switch_permission_control.zh-CN.md)

docs/manual_strategy_switch_permission_control.zh-CN.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@
2828

2929
## Token 权限
3030

31-
优先用 fine-grained PAT,只授权这些目标仓库
31+
优先用 fine-grained PAT,只授权你实际使用的目标平台仓库。QuantStrategyLab 默认仓库是
3232

3333
- `QuantStrategyLab/LongBridgePlatform`
3434
- `QuantStrategyLab/InteractiveBrokersPlatform`
3535
- `QuantStrategyLab/CharlesSchwabPlatform`
3636
- `QuantStrategyLab/FirstradePlatform`
3737

38+
如果你 fork 到自己的组织,把这些替换成你的平台仓库,并在本仓 repository variables 里配置 `RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON``RUNTIME_SETTINGS_LONGBRIDGE_REPO``RUNTIME_SETTINGS_IBKR_REPO``RUNTIME_SETTINGS_SCHWAB_REPO``RUNTIME_SETTINGS_FIRSTRADE_REPO`。完整步骤见 [策略切换控制台 Fork 指南](strategy_switch_fork_guide.zh-CN.md)
39+
3840
需要的能力只有:
3941

4042
- 读取和写入 GitHub Actions variables。

docs/strategy_switch_fork_guide.md

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
# Fork Guide: Strategy Switch Console
2+
3+
This guide explains how to fork this repository and deploy the same public-readonly, login-to-switch console for your own platform repositories.
4+
5+
## What You Need
6+
7+
- A GitHub account or organization that owns your forked runtime settings repository.
8+
- Optional platform repositories for LongBridge, IBKR, Schwab, and Firstrade automation.
9+
- A Cloudflare account with Workers enabled.
10+
- A GitHub OAuth App for login.
11+
- A fine-grained GitHub token that can dispatch this repository's workflow.
12+
- A separate GitHub Actions secret that can write variables in your platform repositories.
13+
14+
Do not commit broker credentials, cloud credentials, API keys, account passwords, or personal access tokens.
15+
16+
## Repository Mapping
17+
18+
The default repository mapping points to QuantStrategyLab:
19+
20+
```text
21+
longbridge -> QuantStrategyLab/LongBridgePlatform
22+
ibkr -> QuantStrategyLab/InteractiveBrokersPlatform
23+
schwab -> QuantStrategyLab/CharlesSchwabPlatform
24+
firstrade -> QuantStrategyLab/FirstradePlatform
25+
```
26+
27+
Fork users can override these without editing source code.
28+
29+
For GitHub Actions, set platform repository variables in your fork:
30+
31+
```text
32+
RUNTIME_SETTINGS_LONGBRIDGE_REPO=your-org/LongBridgePlatform
33+
RUNTIME_SETTINGS_IBKR_REPO=your-org/InteractiveBrokersPlatform
34+
RUNTIME_SETTINGS_SCHWAB_REPO=your-org/CharlesSchwabPlatform
35+
RUNTIME_SETTINGS_FIRSTRADE_REPO=your-org/FirstradePlatform
36+
```
37+
38+
You can also use one JSON variable:
39+
40+
```json
41+
{
42+
"longbridge": "your-org/LongBridgePlatform",
43+
"ibkr": "your-org/InteractiveBrokersPlatform",
44+
"schwab": "your-org/CharlesSchwabPlatform",
45+
"firstrade": "your-org/FirstradePlatform"
46+
}
47+
```
48+
49+
Store that JSON as `RUNTIME_SETTINGS_PLATFORM_REPOSITORIES_JSON`.
50+
51+
For the Cloudflare Worker, use the same JSON as `STRATEGY_SWITCH_PLATFORM_REPOSITORIES_JSON`, or set individual Worker variables:
52+
53+
```text
54+
RUNTIME_SETTINGS_REPO=your-org/QuantRuntimeSettings
55+
STRATEGY_SWITCH_LONGBRIDGE_REPO=your-org/LongBridgePlatform
56+
STRATEGY_SWITCH_IBKR_REPO=your-org/InteractiveBrokersPlatform
57+
STRATEGY_SWITCH_SCHWAB_REPO=your-org/CharlesSchwabPlatform
58+
STRATEGY_SWITCH_FIRSTRADE_REPO=your-org/FirstradePlatform
59+
```
60+
61+
## GitHub Actions Setup
62+
63+
Create a GitHub Environment named `runtime-strategy-switch`.
64+
65+
Add this secret to that environment:
66+
67+
```text
68+
RUNTIME_SETTINGS_GH_TOKEN
69+
```
70+
71+
This token is used by `.github/workflows/manual-strategy-switch.yml` to write GitHub Actions variables in your platform repositories and optionally dispatch each platform's sync workflow. Prefer a fine-grained PAT scoped only to the repositories you actually use.
72+
73+
The token should not need `contents: write`.
74+
75+
## Worker Setup
76+
77+
Create a GitHub OAuth App:
78+
79+
```text
80+
Homepage URL: https://your-worker-domain
81+
Authorization callback URL: https://your-worker-domain/callback
82+
```
83+
84+
Copy the Worker config:
85+
86+
```bash
87+
cp web/strategy-switch-console/wrangler.toml.example web/strategy-switch-console/wrangler.toml
88+
```
89+
90+
Edit `wrangler.toml`:
91+
92+
```toml
93+
name = "your-strategy-switch-console"
94+
95+
[vars]
96+
RUNTIME_SETTINGS_REPO = "your-org/QuantRuntimeSettings"
97+
STRATEGY_SWITCH_PLATFORM_REPOSITORIES_JSON = '{"longbridge":"your-org/LongBridgePlatform","ibkr":"your-org/InteractiveBrokersPlatform","schwab":"your-org/CharlesSchwabPlatform","firstrade":"your-org/FirstradePlatform"}'
98+
```
99+
100+
Set Worker secrets:
101+
102+
```bash
103+
cd web/strategy-switch-console
104+
wrangler secret put GITHUB_CLIENT_ID
105+
wrangler secret put GITHUB_CLIENT_SECRET
106+
wrangler secret put SESSION_SECRET
107+
wrangler secret put RUNTIME_SETTINGS_DISPATCH_TOKEN
108+
wrangler secret put STRATEGY_SWITCH_SYNC_TOKEN
109+
wrangler secret put ALLOWED_GITHUB_LOGINS
110+
wrangler secret put ALLOWED_GITHUB_ORGS
111+
wrangler secret put STRATEGY_SWITCH_ADMIN_LOGINS
112+
wrangler secret put STRATEGY_SWITCH_ADMIN_ORGS
113+
```
114+
115+
`RUNTIME_SETTINGS_DISPATCH_TOKEN` only needs permission to dispatch the runtime settings workflow in your fork. It is not the token that writes platform variables.
116+
117+
## Account Options
118+
119+
Copy the generic example:
120+
121+
```bash
122+
cp web/strategy-switch-console/account-options.example.json /tmp/strategy-switch-accounts.json
123+
```
124+
125+
Edit it with your own route names, service names, account selectors, and supported strategy domains. Keep it to routing metadata only.
126+
127+
Store it as a Worker secret:
128+
129+
```bash
130+
wrangler secret put STRATEGY_SWITCH_ACCOUNT_OPTIONS_JSON < /tmp/strategy-switch-accounts.json
131+
```
132+
133+
For editable settings, create a KV namespace:
134+
135+
```bash
136+
wrangler kv namespace create STRATEGY_SWITCH_CONFIG
137+
```
138+
139+
Add the returned id to `wrangler.toml`, deploy, then use `/admin` to edit:
140+
141+
```text
142+
auth_config
143+
account_options
144+
strategy_profiles
145+
audit_log
146+
```
147+
148+
## Strategy Catalog
149+
150+
Runtime-enabled strategies live in:
151+
152+
```text
153+
web/strategy-switch-console/strategy-profiles.example.json
154+
```
155+
156+
Each item needs:
157+
158+
```json
159+
{
160+
"profile": "my_strategy_profile",
161+
"label": "My Strategy Profile",
162+
"domain": "us_equity",
163+
"runtime_enabled": true
164+
}
165+
```
166+
167+
Supported domains are currently:
168+
169+
```text
170+
us_equity
171+
hk_equity
172+
```
173+
174+
After editing the strategy catalog or page:
175+
176+
```bash
177+
python3 scripts/sync_strategy_switch_page_asset.py
178+
```
179+
180+
## Deploy and Verify
181+
182+
Deploy the Worker:
183+
184+
```bash
185+
cd web/strategy-switch-console
186+
wrangler deploy
187+
```
188+
189+
Verify public mode:
190+
191+
```bash
192+
curl -s https://your-worker-domain/api/config
193+
```
194+
195+
Expected unauthenticated response:
196+
197+
```json
198+
{
199+
"accountOptions": null
200+
}
201+
```
202+
203+
Then open the Worker URL:
204+
205+
- Signed-out users should only see the public read-only page.
206+
- Signed-in allowlisted users should see account, strategy, mode, current status, and the switch button.
207+
- Admin users should be able to open `/admin`.
208+
209+
## Local Checks
210+
211+
Run these before opening a PR:
212+
213+
```bash
214+
jq empty web/strategy-switch-console/account-options.example.json web/strategy-switch-console/strategy-profiles.example.json
215+
node --experimental-default-type=module tests/strategy_switch_worker_validation.mjs
216+
python3 scripts/runtime_settings.py validate
217+
python3 -m unittest discover -s tests -v
218+
git diff --check
219+
```

0 commit comments

Comments
 (0)