diff --git a/docker-compose.local.yml b/docker-compose.local.yml index 94d30d1..79c0592 100644 --- a/docker-compose.local.yml +++ b/docker-compose.local.yml @@ -14,7 +14,7 @@ services: - INIT_TABLE=true - ENABLE_CACHE=true # MySQL Database Configuration (uncomment to use MySQL instead of SQLite) - # - MYSQL_HOST=mysql + # - MYSQL_HOST=newsnow_mysql # - MYSQL_PORT=3306 # - MYSQL_USER=newsnow # - MYSQL_PASSWORD=your_password diff --git a/docker-compose.yml b/docker-compose.yml index caaf4f9..fe70a2e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3' - services: newsnow: image: ghcr.io/ourongxing/newsnow:latest @@ -18,12 +16,12 @@ services: - INIT_TABLE=true - ENABLE_CACHE=true # MySQL Database Configuration (uncomment to use MySQL instead of SQLite) - # - MYSQL_HOST=mysql - # - MYSQL_PORT=3306 - # - MYSQL_USER=newsnow - # - MYSQL_PASSWORD=your_password - # - MYSQL_DATABASE=newsnow - # - MYSQL_SSL=false + - MYSQL_HOST=newsnow_mysql + - MYSQL_PORT=3306 + - MYSQL_USER=newsnow + - MYSQL_PASSWORD=root_password + - MYSQL_DATABASE=newsnow + - MYSQL_SSL=false # Uncomment the following lines to use MySQL database # depends_on: # - mysql diff --git a/nitro.config.ts b/nitro.config.ts index fe96de1..8c64727 100644 --- a/nitro.config.ts +++ b/nitro.config.ts @@ -4,6 +4,41 @@ import viteNitro from "vite-plugin-with-nitro" import { RollopGlob } from "./tools/rollup-glob" import { projectDir } from "./shared/dir" +// 检测是否配置了 MySQL +function isMySQLConfigured(): boolean { + return !!(process.env.MYSQL_HOST && + process.env.MYSQL_USER && + process.env.MYSQL_PASSWORD && + process.env.MYSQL_DATABASE) +} + +// 根据环境变量动态配置数据库 +function getDatabaseConfig() { + if (isMySQLConfigured()) { + console.log('🔗 Using MySQL connector') + return { + default: { + connector: "mysql2", + options: { + host: process.env.MYSQL_HOST, + port: parseInt(process.env.MYSQL_PORT || "3306"), + user: process.env.MYSQL_USER, + password: process.env.MYSQL_PASSWORD, + database: process.env.MYSQL_DATABASE, + ssl: process.env.MYSQL_SSL === "true" ? {} : false, + }, + }, + } + } else { + console.log('🗃️ Using SQLite connector') + return { + default: { + connector: "better-sqlite3", + }, + } + } +} + const nitroOption: Parameters[0] = { experimental: { database: true, @@ -12,16 +47,8 @@ const nitroOption: Parameters[0] = { plugins: [RollopGlob()], }, sourceMap: false, - database: { - default: { - connector: "better-sqlite3", - }, - }, - devDatabase: { - default: { - connector: "better-sqlite3", - }, - }, + database: getDatabaseConfig(), + devDatabase: getDatabaseConfig(), imports: { dirs: ["server/utils", "shared"], }, diff --git a/package.json b/package.json index 1bda9a6..a076241 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "jose": "^6.0.8", "jotai": "^2.12.1", "md5": "^2.3.0", + "mysql2": "^3.11.4", "ofetch": "^1.4.1", "overlayscrollbars": "^2.11.1", "pnpm": "^10.5.2", diff --git a/server/database/cache.ts b/server/database/cache.ts index ce87a9e..a0c164c 100644 --- a/server/database/cache.ts +++ b/server/database/cache.ts @@ -11,10 +11,10 @@ function isMySQLDatabase(): boolean { process.env.MYSQL_DATABASE if (hasMySQLConfig) { - console.log('🔍 检测到 MySQL 配置,使用 MySQL 数据库') + console.log('🔗 Using MySQL database') return true } else { - console.log('🔍 未检测到 MySQL 配置,使用 SQLite 数据库') + console.log('🗃️ Using SQLite database') return false } }