import { ipcMain, app } from 'electron' import { processManager } from '../services/ProcessManager' import { configService } from '../services/ConfigService' import { downloadService } from '../services/DownloadService' import { projectService } from '../services/ProjectService' import { phpManagerService } from '../services/PhpManagerService' import path from 'path' export function registerIpcHandlers(): void { // Service management ipcMain.handle('services:start', async (_event, serviceName: string) => { try { if (serviceName === 'nginx') { const rootPath = path.join(app.getAppPath(), 'www') const configPath = await configService.generateConfig('nginx.conf.template', 'nginx.conf', { PORT: '80', ROOT: rootPath }) const binPath = downloadService.getBinPath('nginx', 'nginx.exe') const success = await processManager.startService('nginx', binPath, ['-c', configPath]) return { success, message: success ? 'Nginx başlatıldı.' : 'Nginx başlatılamadı.' } } if (serviceName === 'php') { const configPath = await configService.generateConfig('php.ini.template', 'php.ini', {}) const binPath = downloadService.getBinPath('php8.2', 'php-cgi.exe') const success = await processManager.startService('php', binPath, ['-b', '127.0.0.1:9001', '-c', configPath]) return { success, message: success ? 'PHP başlatıldı.' : 'PHP başlatılamadı.' } } if (serviceName === 'mysql') { const dataDir = path.join(app.getPath('userData'), 'mysql_data') const configPath = await configService.generateConfig('my.ini.template', 'my.ini', { DATADIR: dataDir, SOCKET: '/tmp/mysql.sock' }) const binPath = downloadService.getBinPath('mysql', 'bin/mysqld.exe') const success = await processManager.startService('mysql', binPath, ['--defaults-file=' + configPath]) return { success, message: success ? 'MySQL başlatıldı.' : 'MySQL başlatılamadı.' } } return { success: false, message: 'Bilinmeyen servis.' } } catch (error: any) { console.error('Service start error:', error) return { success: false, message: `Hata: ${error.message}` } } }) ipcMain.handle('services:stop', async (_event, serviceName: string) => { const success = await processManager.stopService(serviceName) return { success, message: success ? `${serviceName} durduruldu.` : `${serviceName} zaten durmuş.` } }) ipcMain.handle('services:status', async () => { return processManager.getStatuses() }) // Binary management ipcMain.handle('binaries:list-php', async () => { return phpManagerService.getVersions() }) ipcMain.handle('binaries:download-php', async (event, id: string) => { const versions = await phpManagerService.getVersions() const version = versions.find(v => v.id === id) if (!version) return { success: false, message: 'Geçersiz sürüm.' } try { // Signal immediate start phpManagerService.updateProgress(id, 0, 'downloading') event.sender.send('binaries:progress', { id, progress: 0 }) await downloadService.downloadAndExtract(version.url, id, (p) => { phpManagerService.updateProgress(id, p) event.sender.send('binaries:progress', { id, progress: p }) }) phpManagerService.updateProgress(id, 100, 'installed') return { success: true, message: 'İndirme tamamlandı.' } } catch (error: any) { phpManagerService.updateProgress(id, 0, 'available') return { success: false, message: `Hata: ${error.message}` } } }) ipcMain.handle('binaries:download', async (_event, { name, url }) => { return downloadService.downloadAndExtract(url, name, (p) => { // TODO: Emit progress to renderer via webContents.send console.log(`Download progress for ${name}: ${p}%`) }) }) // Project management ipcMain.handle('projects:list', async () => { return projectService.getProjects() }) ipcMain.handle('projects:add', async (_event, project) => { return projectService.addProject(project) }) ipcMain.handle('projects:remove', async (_event, id) => { return projectService.removeProject(id) }) // Config management ipcMain.handle('config:get', async (_event, key: string) => { return null }) ipcMain.handle('config:save', async (_event, config: any) => { return { success: true } }) }