import fs from 'fs' import path from 'path' import pool from './appDb' const CONFIG_PATH = path.join(process.cwd(), 'config.json') export interface Site { id: string name: string url: string interval_min: number } export interface Database { id: string name: string host: string port: number database: string username: string password: string ssl: boolean color: string } export interface Service { id: string name: string url: string icon: string description: string } export interface Config { sites: Site[] databases: Database[] services: Service[] } export function generateId(): string { return `${Date.now()}-${Math.random().toString(36).slice(2, 7)}` } // Migration ve okuma bir arada export async function readConfig(): Promise { // Migration kontrolü if (fs.existsSync(CONFIG_PATH)) { try { const raw = fs.readFileSync(CONFIG_PATH, 'utf-8') const oldConfig = JSON.parse(raw) as Config // Veritabanına aktar for (const site of oldConfig.sites || []) { await pool.query(`INSERT INTO config_sites (id, name, url, interval_min) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING`, [site.id, site.name, site.url, site.interval_min || 5]) } for (const db of oldConfig.databases || []) { await pool.query(`INSERT INTO config_databases (id, name, host, port, database, username, password, ssl, color) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) ON CONFLICT DO NOTHING`, [db.id, db.name, db.host, db.port, db.database, db.username, db.password, db.ssl || false, db.color]) } for (const svc of oldConfig.services || []) { await pool.query(`INSERT INTO config_services (id, name, url, icon, description) VALUES ($1, $2, $3, $4, $5) ON CONFLICT DO NOTHING`, [svc.id, svc.name, svc.url, svc.icon, svc.description]) } // Dosyanın uzantısını değiştirerek migration'ın bittiğini işaretle fs.renameSync(CONFIG_PATH, CONFIG_PATH + '.bak') console.log('[Migration] config.json verileri PostgreSQL tablolalarına aktarıldı ve dosya yedeklendi.') } catch (e) { console.error('[Migration] config.json veritabanına aktarılırken hata oluştu:', e) } } // DB'den oku const [sitesRes, dbsRes, svcsRes] = await Promise.all([ pool.query(`SELECT * FROM config_sites`), pool.query(`SELECT * FROM config_databases`), pool.query(`SELECT * FROM config_services`), ]) return { sites: sitesRes.rows as Site[], databases: dbsRes.rows as Database[], services: svcsRes.rows as Service[] } } export async function addConfigItem(type: 'site' | 'database' | 'service', item: any): Promise { const newItem = { ...item, id: generateId() } if (type === 'site') { await pool.query(`INSERT INTO config_sites (id, name, url, interval_min) VALUES ($1, $2, $3, $4)`, [newItem.id, newItem.name, newItem.url, newItem.interval_min || 5]) } else if (type === 'database') { await pool.query(`INSERT INTO config_databases (id, name, host, port, database, username, password, ssl, color) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`, [newItem.id, newItem.name, newItem.host, newItem.port, newItem.database, newItem.username, newItem.password, newItem.ssl || false, newItem.color]) } else if (type === 'service') { await pool.query(`INSERT INTO config_services (id, name, url, icon, description) VALUES ($1, $2, $3, $4, $5)`, [newItem.id, newItem.name, newItem.url, newItem.icon, newItem.description]) } return newItem } export async function updateConfigItem(type: 'site' | 'database' | 'service', id: string, item: any): Promise { if (type === 'site') { await pool.query(`UPDATE config_sites SET name=$1, url=$2, interval_min=$3 WHERE id=$4`, [item.name, item.url, item.interval_min || 5, id]) } else if (type === 'database') { await pool.query(`UPDATE config_databases SET name=$1, host=$2, port=$3, database=$4, username=$5, password=$6, ssl=$7, color=$8 WHERE id=$9`, [item.name, item.host, item.port, item.database, item.username, item.password, item.ssl || false, item.color, id]) } else if (type === 'service') { await pool.query(`UPDATE config_services SET name=$1, url=$2, icon=$3, description=$4 WHERE id=$5`, [item.name, item.url, item.icon, item.description, id]) } } export async function deleteConfigItem(type: 'site' | 'database' | 'service', id: string): Promise { if (type === 'site') await pool.query(`DELETE FROM config_sites WHERE id=$1`, [id]) else if (type === 'database') await pool.query(`DELETE FROM config_databases WHERE id=$1`, [id]) else if (type === 'service') await pool.query(`DELETE FROM config_services WHERE id=$1`, [id]) }