diff --git a/docker-compose.local.yml b/docker-compose.local.yml index 79c0592..ef2394e 100644 --- a/docker-compose.local.yml +++ b/docker-compose.local.yml @@ -1,5 +1,3 @@ -version: '3' - services: newsnow: build: . @@ -13,35 +11,7 @@ services: - JWT_SECRET= - INIT_TABLE=true - ENABLE_CACHE=true - # MySQL Database Configuration (uncomment to use MySQL instead of SQLite) - # - MYSQL_HOST=newsnow_mysql - # - MYSQL_PORT=3306 - # - MYSQL_USER=newsnow - # - MYSQL_PASSWORD=your_password - # - MYSQL_DATABASE=newsnow - # - MYSQL_SSL=false - # Uncomment the following lines to use MySQL database - # depends_on: - # - mysql - - # Uncomment the following service to use MySQL database - # mysql: - # image: mysql:8.0 - # container_name: newsnow_mysql - # restart: always - # environment: - # MYSQL_ROOT_PASSWORD: root_password - # MYSQL_DATABASE: newsnow - # MYSQL_USER: newsnow - # MYSQL_PASSWORD: your_password - # ports: - # - "3306:3306" - # volumes: - # - mysql_data:/var/lib/mysql - # command: --default-authentication-plugin=mysql_native_password volumes: newsnow_data: - name: newsnow_data - # mysql_data: - # name: newsnow_mysql_data + name: newsnow_data \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index fe70a2e..2e5a82c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,35 +15,7 @@ services: - JWT_SECRET= - INIT_TABLE=true - ENABLE_CACHE=true - # MySQL Database Configuration (uncomment to use MySQL instead of SQLite) - - 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 - - # Uncomment the following service to use MySQL database - # mysql: - # image: mysql:8.0 - # container_name: newsnow_mysql - # restart: always - # environment: - # MYSQL_ROOT_PASSWORD: root_password - # MYSQL_DATABASE: newsnow - # MYSQL_USER: newsnow - # MYSQL_PASSWORD: your_password - # ports: - # - "3306:3306" - # volumes: - # - mysql_data:/var/lib/mysql - # command: --default-authentication-plugin=mysql_native_password volumes: newsnow_data: name: newsnow_data - # mysql_data: - # name: newsnow_mysql_data diff --git a/example.env.server b/example.env.server index ff1de4b..8c920c5 100644 --- a/example.env.server +++ b/example.env.server @@ -2,12 +2,4 @@ G_CLIENT_ID= G_CLIENT_SECRET= JWT_SECRET= INIT_TABLE=true -ENABLE_CACHE=true -# MySQL Database Configuration -# Uncomment and configure these variables to use MySQL instead of SQLite -# MYSQL_HOST=localhost -# MYSQL_PORT=3306 -# MYSQL_USER=newsnow -# MYSQL_PASSWORD=your_password -# MYSQL_DATABASE=newsnow -# MYSQL_SSL=false \ No newline at end of file +ENABLE_CACHE=true \ No newline at end of file diff --git a/nitro.config.ts b/nitro.config.ts index 8c64727..8e75863 100644 --- a/nitro.config.ts +++ b/nitro.config.ts @@ -4,41 +4,6 @@ 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, @@ -47,8 +12,16 @@ const nitroOption: Parameters[0] = { plugins: [RollopGlob()], }, sourceMap: false, - database: getDatabaseConfig(), - devDatabase: getDatabaseConfig(), + database: { + default: { + connector: "better-sqlite3", + }, + }, + devDatabase: { + default: { + connector: "better-sqlite3", + }, + }, imports: { dirs: ["server/utils", "shared"], }, @@ -94,4 +67,4 @@ if (process.env.VERCEL) { export default function () { return viteNitro(nitroOption) -} +} \ No newline at end of file diff --git a/scripts/init-mysql.sql b/scripts/init-mysql.sql deleted file mode 100644 index febb2dc..0000000 --- a/scripts/init-mysql.sql +++ /dev/null @@ -1,33 +0,0 @@ --- MySQL Database Initialization Script for NewsNow --- Run this script to create the required database and tables - --- Create database if not exists -CREATE DATABASE IF NOT EXISTS newsnow CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - --- Use the database -USE newsnow; - --- Create cache table -CREATE TABLE IF NOT EXISTS cache ( - id VARCHAR(255) PRIMARY KEY, - updated BIGINT, - data TEXT -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - --- Create user table -CREATE TABLE IF NOT EXISTS user ( - id VARCHAR(255) PRIMARY KEY, - email VARCHAR(255), - data TEXT, - type VARCHAR(50), - created BIGINT, - updated BIGINT -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - --- Create index for user table -CREATE INDEX IF NOT EXISTS idx_user_id ON user(id); - --- Create MySQL user (optional - you can create this manually) --- CREATE USER IF NOT EXISTS 'newsnow'@'%' IDENTIFIED BY 'your_password'; --- GRANT ALL PRIVILEGES ON newsnow.* TO 'newsnow'@'%'; --- FLUSH PRIVILEGES; \ No newline at end of file diff --git a/server/database/cache.ts b/server/database/cache.ts index a0c164c..c294316 100644 --- a/server/database/cache.ts +++ b/server/database/cache.ts @@ -3,68 +3,28 @@ import type { NewsItem } from "@shared/types" import type { Database } from "db0" import type { CacheInfo, CacheRow } from "../types" -// 改进的数据库类型检测函数 -function isMySQLDatabase(): boolean { - const hasMySQLConfig = process.env.MYSQL_HOST && - process.env.MYSQL_USER && - process.env.MYSQL_PASSWORD && - process.env.MYSQL_DATABASE - - if (hasMySQLConfig) { - console.log('🔗 Using MySQL database') - return true - } else { - console.log('🗃️ Using SQLite database') - return false - } -} - export class Cache { private db - private isMySQL: boolean - constructor(db: Database) { this.db = db - this.isMySQL = isMySQLDatabase() } async init() { - if (this.isMySQL) { - // MySQL syntax - await this.db.prepare(` - CREATE TABLE IF NOT EXISTS cache ( - id VARCHAR(255) PRIMARY KEY, - updated BIGINT, - data TEXT - ); - `).run() - } else { - // SQLite syntax - await this.db.prepare(` - CREATE TABLE IF NOT EXISTS cache ( - id TEXT PRIMARY KEY, - updated INTEGER, - data TEXT - ); - `).run() - } + await this.db.prepare(` + CREATE TABLE IF NOT EXISTS cache ( + id TEXT PRIMARY KEY, + updated INTEGER, + data TEXT + ); + `).run() logger.success(`init cache table`) } async set(key: string, value: NewsItem[]) { const now = Date.now() - - if (this.isMySQL) { - // MySQL syntax - use ON DUPLICATE KEY UPDATE - await this.db.prepare( - `INSERT INTO cache (id, data, updated) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data), updated = VALUES(updated)`, - ).run(key, JSON.stringify(value), now) - } else { - // SQLite syntax - use INSERT OR REPLACE - await this.db.prepare( - `INSERT OR REPLACE INTO cache (id, data, updated) VALUES (?, ?, ?)`, - ).run(key, JSON.stringify(value), now) - } + await this.db.prepare( + `INSERT OR REPLACE INTO cache (id, data, updated) VALUES (?, ?, ?)`, + ).run(key, JSON.stringify(value), now) logger.success(`set ${key} cache`) } @@ -122,4 +82,4 @@ export async function getCacheTable() { } catch (e) { logger.error("failed to init database ", e) } -} +} \ No newline at end of file diff --git a/server/database/user.ts b/server/database/user.ts index 0c35e0c..26f1bc1 100644 --- a/server/database/user.ts +++ b/server/database/user.ts @@ -1,4 +1,3 @@ -import process from "node:process" import type { Database } from "db0" import type { UserInfo } from "#/types" @@ -9,58 +8,28 @@ export class UserTable { } async init() { - // Check if we're using MySQL by looking for MySQL environment variables - const isMySQL = process.env.MYSQL_HOST && process.env.MYSQL_USER && process.env.MYSQL_PASSWORD && process.env.MYSQL_DATABASE - - if (isMySQL) { - // MySQL syntax - await this.db.prepare(` - CREATE TABLE IF NOT EXISTS user ( - id VARCHAR(255) PRIMARY KEY, - email VARCHAR(255), - data TEXT, - type VARCHAR(50), - created BIGINT, - updated BIGINT - ); - `).run() - await this.db.prepare(` - CREATE INDEX IF NOT EXISTS idx_user_id ON user(id); - `).run() - } else { - // SQLite syntax - await this.db.prepare(` - CREATE TABLE IF NOT EXISTS user ( - id TEXT PRIMARY KEY, - email TEXT, - data TEXT, - type TEXT, - created INTEGER, - updated INTEGER - ); - `).run() - await this.db.prepare(` - CREATE INDEX IF NOT EXISTS idx_user_id ON user(id); - `).run() - } + await this.db.prepare(` + CREATE TABLE IF NOT EXISTS user ( + id TEXT PRIMARY KEY, + email TEXT, + data TEXT, + type TEXT, + created INTEGER, + updated INTEGER + ); + `).run() + await this.db.prepare(` + CREATE INDEX IF NOT EXISTS idx_user_id ON user(id); + `).run() logger.success(`init user table`) } async addUser(id: string, email: string, type: "github") { const u = await this.getUser(id) const now = Date.now() - const isMySQL = process.env.MYSQL_HOST && process.env.MYSQL_USER && process.env.MYSQL_PASSWORD && process.env.MYSQL_DATABASE - if (!u) { - if (isMySQL) { - // MySQL syntax - use ON DUPLICATE KEY UPDATE - await this.db.prepare(`INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE email = VALUES(email), updated = VALUES(updated)`) - .run(id, email, "", type, now, now) - } else { - // SQLite syntax - await this.db.prepare(`INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?)`) - .run(id, email, "", type, now, now) - } + await this.db.prepare(`INSERT INTO user (id, email, data, type, created, updated) VALUES (?, ?, ?, ?, ?, ?)`) + .run(id, email, "", type, now, now) logger.success(`add user ${id}`) } else if (u.email !== email && u.type !== type) { await this.db.prepare(`UPDATE user SET email = ?, updated = ? WHERE id = ?`).run(email, now, id) @@ -97,4 +66,4 @@ export class UserTable { if (!state.success) throw new Error(`delete user ${key} failed`) logger.success(`delete user ${key}`) } -} +} \ No newline at end of file