From dd88997d8ed3a91705e50d4cca760a0d0b57034d Mon Sep 17 00:00:00 2001 From: icy Date: Fri, 31 Oct 2025 14:09:28 +0800 Subject: [PATCH 1/7] Restore config.json.example --- CUSTOM_API.md | 4 ++-- DOCKER_DEPLOY.en.md | 10 ++++----- README.md | 15 +++++++------- README.zh-CN.md | 15 +++++++------- config.json.example | 24 ++++++++++++++++++++++ start.sh | 49 ++++++++++++++++++++++++++++++++++++++++++--- 6 files changed, 93 insertions(+), 24 deletions(-) create mode 100644 config.json.example diff --git a/CUSTOM_API.md b/CUSTOM_API.md index 7b3578c1..78e36d61 100644 --- a/CUSTOM_API.md +++ b/CUSTOM_API.md @@ -10,9 +10,9 @@ ## 配置方式 -~~在 `config.json` 中添加使用自定义 API 的 trader:~~ +在 `config.json` 中添加使用自定义 API 的 trader(~~已弃用~~): -*注意:现在通过Web界面配置自定义API,不再使用config.json文件* +*注意:现在通过Web界面配置自定义API和交易员,config.json仅保留基础设置* ```json { diff --git a/DOCKER_DEPLOY.en.md b/DOCKER_DEPLOY.en.md index cf114feb..06464b32 100644 --- a/DOCKER_DEPLOY.en.md +++ b/DOCKER_DEPLOY.en.md @@ -49,13 +49,13 @@ docker compose --version # Docker 24+ includes this, no separate installation n ### Step 1: Prepare Configuration File ```bash -# ~~Copy configuration template~~ -# ~~cp config.example.jsonc config.json~~ +# Copy configuration template +cp config.example.jsonc config.json -# ~~Edit configuration file with your API keys~~ -# ~~nano config.json # or use any other editor~~ +# Edit configuration file with your API keys +nano config.json # or use any other editor -⚠️ **Note**: Configuration is now done through the web interface, no longer using JSON files. +⚠️ **Note**: Basic config.json is still needed for some settings, but ~~trader configurations~~ are now done through the web interface. ``` **Required fields:** diff --git a/README.md b/README.md index deb1e496..b02a497e 100644 --- a/README.md +++ b/README.md @@ -156,7 +156,7 @@ A Binance-compatible decentralized perpetual futures exchange! ``` nofx/ ├── main.go # Program entry (multi-trader manager) -├── ~~config.json~~ # ~~Configuration file (API keys, multi-trader config)~~ (Deprecated: Use web interface) +├── config.json # Configuration file (API keys, ~~multi-trader config~~) (Trader config via web interface) │ ├── api/ # HTTP API service │ └── server.go # Gin framework, RESTful API @@ -251,15 +251,16 @@ Before using this system, you need a Binance Futures account. **Use our referral Docker automatically handles all dependencies (Go, Node.js, TA-Lib, SQLite) and environment setup. -#### ~~Step 1: Prepare Configuration~~ (Deprecated) +#### Step 1: Prepare Configuration ```bash -# ~~Copy configuration template~~ -# ~~cp config.example.jsonc config.json~~ +# Copy configuration template +cp config.example.jsonc config.json -# ~~Edit and fill in your API keys~~ -# ~~nano config.json # or use any editor~~ +# Edit and fill in your API keys +nano config.json # or use any editor ``` -⚠️ **Note**: Configuration is now done through the web interface, not JSON files. + +⚠️ **Note**: Basic config.json is still needed for some settings, but ~~trader configurations~~ are now done through the web interface. #### Step 2: One-Click Start ```bash diff --git a/README.zh-CN.md b/README.zh-CN.md index bd86de5c..a5fc01c9 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -150,7 +150,7 @@ NOFX现已支持**三大交易所**:Binance、Hyperliquid和Aster DEX! ``` nofx/ ├── main.go # 程序入口(多trader管理器) -├── ~~config.json~~ # ~~配置文件(API密钥、多trader配置)~~ (已弃用:使用Web界面) +├── config.json # 配置文件(API密钥、~~多trader配置~~)(交易员配置通过Web界面) │ ├── api/ # HTTP API服务 │ └── server.go # Gin框架,RESTful API @@ -245,15 +245,16 @@ nofx/ Docker会自动处理所有依赖(Go、Node.js、TA-Lib)和环境配置,完美适合新手! -#### ~~步骤1:准备配置文件~~ (已弃用) +#### 步骤1:准备配置文件 ```bash -# ~~复制配置文件模板~~ -# ~~cp config.example.jsonc config.json~~ +# 复制配置文件模板 +cp config.example.jsonc config.json -# ~~编辑并填入你的API密钥~~ -# ~~nano config.json # 或使用其他编辑器~~ +# 编辑并填入你的API密钥 +nano config.json # 或使用其他编辑器 ``` -⚠️ **注意**: 现在通过Web界面进行配置,不再使用JSON文件。 + +⚠️ **注意**: 基础config.json仍需要一些设置,但~~交易员配置~~现在通过Web界面进行。 #### 步骤2:一键启动 ```bash diff --git a/config.json.example b/config.json.example new file mode 100644 index 00000000..05616c1d --- /dev/null +++ b/config.json.example @@ -0,0 +1,24 @@ +{ + "leverage": { + "btc_eth_leverage": 5, + "altcoin_leverage": 5 + }, + "use_default_coins": true, + "default_coins": [ + "BTCUSDT", + "ETHUSDT", + "SOLUSDT", + "BNBUSDT", + "XRPUSDT", + "DOGEUSDT", + "ADAUSDT", + "HYPEUSDT" + ], + "coin_pool_api_url": "", + "oi_top_api_url": "", + "api_server_port": 8080, + "max_daily_loss": 10.0, + "max_drawdown": 20.0, + "stop_trading_minutes": 60, + "jwt_secret": "Qk0kAa+d0iIEzXVHXbNbm+UaN3RNabmWtH8rDWZ5OPf+4GX8pBflAHodfpbipVMyrw1fsDanHsNBjhgbDeK9Jg==" +} \ No newline at end of file diff --git a/start.sh b/start.sh index 7f5a8d81..dcb89672 100755 --- a/start.sh +++ b/start.sh @@ -77,6 +77,42 @@ check_env() { print_success "环境变量文件存在" } +# ------------------------------------------------------------------------ +# Validation: Configuration File (config.json) - DEPRECATED +# ------------------------------------------------------------------------ +check_config() { + if [ ! -f "config.json" ]; then + print_warning "config.json 不存在,从模板复制..." + cp config.example.jsonc config.json + print_info "⚠️ 注意:config.json 已弃用,请使用Web界面进行配置" + print_info "此文件仅作为参考保留" + fi + print_success "配置文件存在(已弃用,使用Web界面配置)" +} + +# ------------------------------------------------------------------------ +# Utility: Read Environment Variables +# ------------------------------------------------------------------------ +read_env_vars() { + if [ -f ".env" ]; then + # 读取端口配置,设置默认值 + NOFX_FRONTEND_PORT=$(grep "^NOFX_FRONTEND_PORT=" .env 2>/dev/null | cut -d'=' -f2 || echo "3000") + NOFX_BACKEND_PORT=$(grep "^NOFX_BACKEND_PORT=" .env 2>/dev/null | cut -d'=' -f2 || echo "8080") + + # 去除可能的引号和空格 + NOFX_FRONTEND_PORT=$(echo "$NOFX_FRONTEND_PORT" | tr -d '"'"'" | tr -d ' ') + NOFX_BACKEND_PORT=$(echo "$NOFX_BACKEND_PORT" | tr -d '"'"'" | tr -d ' ') + + # 如果为空则使用默认值 + NOFX_FRONTEND_PORT=${NOFX_FRONTEND_PORT:-3000} + NOFX_BACKEND_PORT=${NOFX_BACKEND_PORT:-8080} + else + # 如果.env不存在,使用默认端口 + NOFX_FRONTEND_PORT=3000 + NOFX_BACKEND_PORT=8080 + fi +} + # ------------------------------------------------------------------------ # Validation: Database File (trading.db) # ------------------------------------------------------------------------ @@ -123,6 +159,9 @@ check_database() { start() { print_info "正在启动 NOFX AI Trading System..." + # 读取环境变量 + read_env_vars + # Auto-build frontend if missing or forced # if [ ! -d "web/dist" ] || [ "$1" == "--build" ]; then # build_frontend @@ -138,8 +177,8 @@ start() { fi print_success "服务已启动!" - print_info "Web 界面: http://localhost:3000" - print_info "API 端点: http://localhost:8080" + print_info "Web 界面: http://localhost:${NOFX_FRONTEND_PORT}" + print_info "API 端点: http://localhost:${NOFX_BACKEND_PORT}" print_info "" print_info "查看日志: ./start.sh logs" print_info "停止服务: ./start.sh stop" @@ -178,11 +217,14 @@ logs() { # Monitoring: Status # ------------------------------------------------------------------------ status() { + # 读取环境变量 + read_env_vars + print_info "服务状态:" $COMPOSE_CMD ps echo "" print_info "健康检查:" - curl -s http://localhost:8080/health | jq '.' || echo "后端未响应" + curl -s "http://localhost:${NOFX_BACKEND_PORT}/health" | jq '.' || echo "后端未响应" } # ------------------------------------------------------------------------ @@ -243,6 +285,7 @@ main() { case "${1:-start}" in start) check_env + check_config check_database start "$2" ;; From 458f5c20ccca37a81e2c24ee78239fa3f186cbef Mon Sep 17 00:00:00 2001 From: icy Date: Fri, 31 Oct 2025 14:16:47 +0800 Subject: [PATCH 2/7] start script fix about config.json --- start.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/start.sh b/start.sh index dcb89672..8542b990 100755 --- a/start.sh +++ b/start.sh @@ -78,16 +78,18 @@ check_env() { } # ------------------------------------------------------------------------ -# Validation: Configuration File (config.json) - DEPRECATED +# Validation: Configuration File (config.json) - BASIC SETTINGS ONLY # ------------------------------------------------------------------------ check_config() { if [ ! -f "config.json" ]; then print_warning "config.json 不存在,从模板复制..." - cp config.example.jsonc config.json - print_info "⚠️ 注意:config.json 已弃用,请使用Web界面进行配置" - print_info "此文件仅作为参考保留" + cp config.json.example config.json + print_info "⚠️ 请编辑 config.json 配置基础设置(管理员模式、JWT密钥等)" + print_info "💡 交易员配置请使用Web界面,不再需要在config.json中配置" + print_info "运行: nano config.json 或使用其他编辑器" + exit 1 fi - print_success "配置文件存在(已弃用,使用Web界面配置)" + print_success "配置文件存在" } # ------------------------------------------------------------------------ From 517d0caf6fb091235e56c27889170b53a16e4e6b Mon Sep 17 00:00:00 2001 From: icy Date: Fri, 31 Oct 2025 14:34:53 +0800 Subject: [PATCH 3/7] admin_mode config --- config.json.example | 1 + config/database.go | 1 + 2 files changed, 2 insertions(+) diff --git a/config.json.example b/config.json.example index 05616c1d..6b0a32e1 100644 --- a/config.json.example +++ b/config.json.example @@ -1,4 +1,5 @@ { + "admin_mode": true, "leverage": { "btc_eth_leverage": 5, "altcoin_leverage": 5 diff --git a/config/database.go b/config/database.go index aeab9bbb..87e74b72 100644 --- a/config/database.go +++ b/config/database.go @@ -211,6 +211,7 @@ func (d *Database) initDefaultData() error { // 初始化系统配置 systemConfigs := map[string]string{ + "admin_mode": "true", "api_server_port": "8080", "use_default_coins": "true", "coin_pool_api_url": "", From 96757350735645e93e619f4d1aa90831eeb1dae1 Mon Sep 17 00:00:00 2001 From: icy Date: Fri, 31 Oct 2025 14:39:04 +0800 Subject: [PATCH 4/7] start scrpit fix --- start.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/start.sh b/start.sh index 8542b990..daf03a6c 100755 --- a/start.sh +++ b/start.sh @@ -84,10 +84,9 @@ check_config() { if [ ! -f "config.json" ]; then print_warning "config.json 不存在,从模板复制..." cp config.json.example config.json - print_info "⚠️ 请编辑 config.json 配置基础设置(管理员模式、JWT密钥等)" - print_info "💡 交易员配置请使用Web界面,不再需要在config.json中配置" - print_info "运行: nano config.json 或使用其他编辑器" - exit 1 + print_info "✓ 已使用默认配置创建 config.json" + print_info "💡 如需修改基础设置(杠杆大小、开仓币种、管理员模式、JWT密钥等),可编辑 config.json" + print_info "💡 模型/交易所/交易员配置请使用Web界面" fi print_success "配置文件存在" } From 3405f0dd3bb72859aa0fe1c7216219704b6da5ba Mon Sep 17 00:00:00 2001 From: icy Date: Fri, 31 Oct 2025 14:41:45 +0800 Subject: [PATCH 5/7] start scrip fix --- start.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/start.sh b/start.sh index daf03a6c..581495fa 100755 --- a/start.sh +++ b/start.sh @@ -70,9 +70,8 @@ check_env() { if [ ! -f ".env" ]; then print_warning ".env 不存在,从模板复制..." cp .env.example .env - print_info "请编辑 .env 填入你的环境变量配置" - print_info "运行: nano .env 或使用其他编辑器" - exit 1 + print_info "✓ 已使用默认环境变量创建 .env" + print_info "💡 如需修改端口等设置,可编辑 .env 文件" fi print_success "环境变量文件存在" } From 8296d9843ac7fffe4966c42dfdcb3e091ef16690 Mon Sep 17 00:00:00 2001 From: icy Date: Fri, 31 Oct 2025 15:24:18 +0800 Subject: [PATCH 6/7] Sync config.json to config.db for manual modify admin_mode and other configs --- config/database.go | 22 ++++++----- main.go | 95 ++++++++++++++++++++++++++++++++++++++++++++++ start.sh | 4 +- 3 files changed, 110 insertions(+), 11 deletions(-) diff --git a/config/database.go b/config/database.go index 87e74b72..20b08d5f 100644 --- a/config/database.go +++ b/config/database.go @@ -209,16 +209,20 @@ func (d *Database) initDefaultData() error { } } - // 初始化系统配置 + // 初始化系统配置 - 创建所有字段,设置默认值,后续由config.json同步更新 systemConfigs := map[string]string{ - "admin_mode": "true", - "api_server_port": "8080", - "use_default_coins": "true", - "coin_pool_api_url": "", - "oi_top_api_url": "", - "max_daily_loss": "10.0", - "max_drawdown": "20.0", - "stop_trading_minutes": "60", + "admin_mode": "true", // 默认开启管理员模式,便于首次使用 + "api_server_port": "8080", // 默认API端口 + "use_default_coins": "true", // 默认使用内置币种列表 + "default_coins": `["BTCUSDT","ETHUSDT","SOLUSDT","BNBUSDT","XRPUSDT","DOGEUSDT","ADAUSDT","HYPEUSDT"]`, // 默认币种列表(JSON格式) + "coin_pool_api_url": "", // 币种池API URL,默认为空 + "oi_top_api_url": "", // 持仓量API URL,默认为空 + "max_daily_loss": "10.0", // 最大日损失百分比 + "max_drawdown": "20.0", // 最大回撤百分比 + "stop_trading_minutes": "60", // 停止交易时间(分钟) + "btc_eth_leverage": "5", // BTC/ETH杠杆倍数 + "altcoin_leverage": "5", // 山寨币杠杆倍数 + "jwt_secret": "", // JWT密钥,默认为空,由config.json或系统生成 } for key, value := range systemConfigs { diff --git a/main.go b/main.go index a9ea34c9..547c0677 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "log" "nofx/api" @@ -15,6 +16,95 @@ import ( "syscall" ) +// LeverageConfig 杠杆配置 +type LeverageConfig struct { + BTCETHLeverage int `json:"btc_eth_leverage"` + AltcoinLeverage int `json:"altcoin_leverage"` +} + +// ConfigFile 配置文件结构,只包含需要同步到数据库的字段 +type ConfigFile struct { + AdminMode bool `json:"admin_mode"` + APIServerPort int `json:"api_server_port"` + UseDefaultCoins bool `json:"use_default_coins"` + DefaultCoins []string `json:"default_coins"` + CoinPoolAPIURL string `json:"coin_pool_api_url"` + OITopAPIURL string `json:"oi_top_api_url"` + MaxDailyLoss float64 `json:"max_daily_loss"` + MaxDrawdown float64 `json:"max_drawdown"` + StopTradingMinutes int `json:"stop_trading_minutes"` + Leverage LeverageConfig `json:"leverage"` + JWTSecret string `json:"jwt_secret"` +} + +// syncConfigToDatabase 从config.json读取配置并同步到数据库 +func syncConfigToDatabase(database *config.Database) error { + // 检查config.json是否存在 + if _, err := os.Stat("config.json"); os.IsNotExist(err) { + log.Printf("📄 config.json不存在,跳过同步") + return nil + } + + // 读取config.json + data, err := os.ReadFile("config.json") + if err != nil { + return fmt.Errorf("读取config.json失败: %w", err) + } + + // 解析JSON + var configFile ConfigFile + if err := json.Unmarshal(data, &configFile); err != nil { + return fmt.Errorf("解析config.json失败: %w", err) + } + + log.Printf("🔄 开始同步config.json到数据库...") + + // 同步各配置项到数据库 + configs := map[string]string{ + "admin_mode": fmt.Sprintf("%t", configFile.AdminMode), + "api_server_port": strconv.Itoa(configFile.APIServerPort), + "use_default_coins": fmt.Sprintf("%t", configFile.UseDefaultCoins), + "coin_pool_api_url": configFile.CoinPoolAPIURL, + "oi_top_api_url": configFile.OITopAPIURL, + "max_daily_loss": fmt.Sprintf("%.1f", configFile.MaxDailyLoss), + "max_drawdown": fmt.Sprintf("%.1f", configFile.MaxDrawdown), + "stop_trading_minutes": strconv.Itoa(configFile.StopTradingMinutes), + } + + // 同步default_coins(转换为JSON字符串存储) + if len(configFile.DefaultCoins) > 0 { + defaultCoinsJSON, err := json.Marshal(configFile.DefaultCoins) + if err == nil { + configs["default_coins"] = string(defaultCoinsJSON) + } + } + + // 同步杠杆配置 + if configFile.Leverage.BTCETHLeverage > 0 { + configs["btc_eth_leverage"] = strconv.Itoa(configFile.Leverage.BTCETHLeverage) + } + if configFile.Leverage.AltcoinLeverage > 0 { + configs["altcoin_leverage"] = strconv.Itoa(configFile.Leverage.AltcoinLeverage) + } + + // 如果JWT密钥不为空,也同步 + if configFile.JWTSecret != "" { + configs["jwt_secret"] = configFile.JWTSecret + } + + // 更新数据库配置 + for key, value := range configs { + if err := database.SetSystemConfig(key, value); err != nil { + log.Printf("⚠️ 更新配置 %s 失败: %v", key, err) + } else { + log.Printf("✓ 同步配置: %s = %s", key, value) + } + } + + log.Printf("✅ config.json同步完成") + return nil +} + func main() { fmt.Println("╔════════════════════════════════════════════════════════════╗") fmt.Println("║ 🤖 AI多模型交易系统 - 支持 DeepSeek & Qwen ║") @@ -34,6 +124,11 @@ func main() { } defer database.Close() + // 同步config.json到数据库 + if err := syncConfigToDatabase(database); err != nil { + log.Printf("⚠️ 同步config.json到数据库失败: %v", err) + } + // 获取系统配置 useDefaultCoinsStr, _ := database.GetSystemConfig("use_default_coins") useDefaultCoins := useDefaultCoinsStr == "true" diff --git a/start.sh b/start.sh index 581495fa..040d4b4d 100755 --- a/start.sh +++ b/start.sh @@ -114,10 +114,10 @@ read_env_vars() { } # ------------------------------------------------------------------------ -# Validation: Database File (trading.db) +# Validation: Database File (config.db) # ------------------------------------------------------------------------ check_database() { - if [ ! -f "trading.db" ]; then + if [ ! -f "config.db" ]; then print_info "数据库文件不存在,系统将在启动时自动创建" else print_success "数据库文件存在" From c8c185f147c62e3f1d137fb5aa0a11edf5dfb207 Mon Sep 17 00:00:00 2001 From: icy Date: Fri, 31 Oct 2025 15:31:47 +0800 Subject: [PATCH 7/7] Add config.db volume mapping to docker-compose MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Map config.db to host for database persistence - Ensures user configurations, traders, and AI models persist across container restarts - Enables easy backup of configuration database 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index 608fdfd8..b049f95d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ services: - "${NOFX_BACKEND_PORT:-8080}:8080" volumes: - ./config.json:/app/config.json:ro + - ./config.db:/app/config.db - ./decision_logs:/app/decision_logs - /etc/localtime:/etc/localtime:ro # Sync host time environment: