import type { Request, Response, Application } from 'express'; import path from 'path'; import { readFileSync, existsSync } from 'fs'; function safeReadFile(filePath: string, encoding: BufferEncoding = 'utf-8'): string { if (existsSync(filePath)) { try { return readFileSync(filePath, encoding); } catch (e) { console.error(`Error reading file ${filePath} (safeReadFile):`, e); return ''; } } console.warn(`Warning: File not found at ${filePath} (safeReadFile). Returning empty string.`); return ''; } export function html(app: Application): void { const fileSystemPublicBasePath = path.resolve(__dirname, '..', 'public'); const header = safeReadFile(path.join(fileSystemPublicBasePath, 'templates', 'header.html')); const chatheadai = safeReadFile(path.join(fileSystemPublicBasePath, 'templates', 'chatheadai.html')); const footer = safeReadFile(path.join(fileSystemPublicBasePath, 'templates', 'footer.html')); function renderPage(title: string, scriptPath: string): string { const hostScript = safeReadFile(path.join(fileSystemPublicBasePath, 'src', scriptPath)); const basePublicPath = '/private/server/exocore/web/public'; return ` ${title}
${header}
${chatheadai} ${footer}
`; } const urlSegmentForPublicRoutes = '/private/server/exocore/web/public'; app.get(`${urlSegmentForPublicRoutes}/register`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Signup', 'register.js')); }); app.get(`${urlSegmentForPublicRoutes}/login`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Login', 'login.js')); }); app.get(`${urlSegmentForPublicRoutes}/project`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Project', 'project.js')); }); app.get(`${urlSegmentForPublicRoutes}/otp`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Otp', 'otp.js')); }); app.get(`${urlSegmentForPublicRoutes}/profile`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Profile', 'profile.js')); }); app.get(`${urlSegmentForPublicRoutes}/forgot-password`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('ForgotPass', 'forgot.js')); }); app.get(`${urlSegmentForPublicRoutes}/dashboard`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Dashboard', 'dashboard.js')); }); app.get(`${urlSegmentForPublicRoutes}/plans`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('plans', 'plans.js')); }); app.get(`${urlSegmentForPublicRoutes}/console`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Console', 'console.js')); }); app.get(`${urlSegmentForPublicRoutes}/shell`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('Shell', 'shell.js')); }); app.get(`${urlSegmentForPublicRoutes}/manager`, (_req: Request, res: Response): void => { res.setHeader('Content-Type', 'text/html'); res.send(renderPage('File Manager', 'FileManager.js')); }); app.get(urlSegmentForPublicRoutes, (_req: Request, res: Response): void => { const panelHtmlFilePath = path.join(fileSystemPublicBasePath, 'panel.html'); if (!existsSync(panelHtmlFilePath)) { console.warn(`Static file not found: ${panelHtmlFilePath} for URL ${urlSegmentForPublicRoutes}. Sending 404.`); res.status(404).send('Page not found.'); return; } try { const fileContent = readFileSync(panelHtmlFilePath, 'utf-8'); res.setHeader('Content-Type', 'text/html'); res.send(fileContent); } catch (error) { console.error(`Error reading static file ${panelHtmlFilePath} for URL ${urlSegmentForPublicRoutes}:`, error); res.status(500).send('Error loading page.'); } }); }