import { Router, type Request, type Response } from 'express'; import { z } from 'zod'; import { chittyBeacon } from '../services/ChittyBeaconService.js'; const router = Router(); // Custom beacon schema const customBeaconSchema = z.object({ event: z.string().default('custom'), metadata: z.record(z.any()).optional(), timestamp: z.string().optional() }); // Health check router.get('/health', async (req: Request, res: Response) => { try { const config = chittyBeacon.getConfig(); const appInfo = chittyBeacon.getAppInfo(); res.json({ status: 'ok', service: 'ChittyBeacon v1.0.0', enabled: config.enabled, app: appInfo, timestamp: new Date().toISOString() }); } catch (error) { res.status(500).json({ status: 'error', service: 'ChittyBeacon v1.0.0', error: error instanceof Error ? error.message : 'Unknown error' }); } }); // Get beacon statistics router.get('/stats', async (req: Request, res: Response) => { try { const stats = await chittyBeacon.getStats(); res.json(stats); } catch (error) { console.error('Beacon stats error:', error); res.status(500).json({ error: error instanceof Error ? error.message : 'Unknown error occurred' }); } }); // Get beacon history router.get('/history', async (req: Request, res: Response) => { try { const history = await chittyBeacon.getBeaconHistory(); const limit = req.query.limit ? parseInt(req.query.limit as string) : 100; res.json({ beacons: history.slice(0, limit), total: history.length, limit }); } catch (error) { console.error('Beacon history error:', error); res.status(500).json({ error: error instanceof Error ? error.message : 'Unknown error occurred' }); } }); // Send custom beacon router.post('/track', async (req: Request, res: Response) => { try { const { event, metadata, timestamp } = customBeaconSchema.parse(req.body); const appInfo = chittyBeacon.getAppInfo(); if (!appInfo) { return res.status(400).json({ error: 'Beacon service not initialized' }); } await chittyBeacon.sendBeacon({ ...appInfo, event: event as any, timestamp: timestamp || new Date().toISOString(), metadata: { ...appInfo.metadata, ...metadata, custom: true, userAgent: req.get('User-Agent') } }); res.json({ status: 'ok', message: 'Beacon sent successfully', event, timestamp: timestamp || new Date().toISOString() }); } catch (error) { console.error('Custom beacon error:', error); res.status(500).json({ error: error instanceof Error ? error.message : 'Failed to send beacon' }); } }); // Get app detection info router.get('/detect', async (req: Request, res: Response) => { try { const detection = chittyBeacon.detectApp(); res.json(detection); } catch (error) { console.error('App detection error:', error); res.status(500).json({ error: error instanceof Error ? error.message : 'Detection failed' }); } }); // Simple dashboard (similar to the original tracker-server.js) router.get('/dashboard', async (req: Request, res: Response) => { try { const history = await chittyBeacon.getBeaconHistory(); const stats = await chittyBeacon.getStats(); res.setHeader('Content-Type', 'text/html'); res.send(`
Real-time tracking for ChittyChain ecosystem
Error: ${error instanceof Error ? error.message : 'Unknown error'}
`); } }); export default router;