|
| 1 | +import healthHandler from '../routes/health.js'; |
| 2 | +import syncHandler from '../routes/sync.js'; |
| 3 | +import apisIndexHandler from '../routes/apis/index.js'; |
| 4 | +import apisIdHandler from '../routes/apis/[id].js'; |
| 5 | +import environmentsIndexHandler from '../routes/environments/index.js'; |
| 6 | +import environmentsIdHandler from '../routes/environments/[id].js'; |
| 7 | +import foldersIndexHandler from '../routes/folders/index.js'; |
| 8 | +import foldersIdHandler from '../routes/folders/[id].js'; |
| 9 | +import rbacDocsHandler from '../routes/rbac/docs.js'; |
| 10 | +import rbacFoldersHandler from '../routes/rbac/folders.js'; |
| 11 | +import rbacIndexHandler from '../routes/rbac/index.js'; |
| 12 | +import syncQueueIndexHandler from '../routes/sync_queue/index.js'; |
| 13 | +import usersIndexHandler from '../routes/users/index.js'; |
| 14 | + |
| 15 | +export default async function handler(req: any, res: any) { |
| 16 | + // Basic CORS for unhandled preflights |
| 17 | + if (req.method === 'OPTIONS') { |
| 18 | + res.setHeader('Access-Control-Allow-Origin', '*'); |
| 19 | + res.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS'); |
| 20 | + res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); |
| 21 | + return res.status(200).end(); |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`); |
| 26 | + let pathname = url.pathname; |
| 27 | + |
| 28 | + // Remove trailing slash if present |
| 29 | + if (pathname.length > 1 && pathname.endsWith('/')) { |
| 30 | + pathname = pathname.slice(0, -1); |
| 31 | + } |
| 32 | + |
| 33 | + // Exact matches |
| 34 | + if (pathname === '/api/health') return healthHandler(req, res); |
| 35 | + if (pathname === '/api/sync') return syncHandler(req, res); |
| 36 | + if (pathname === '/api/apis') return apisIndexHandler(req, res); |
| 37 | + if (pathname === '/api/environments') return environmentsIndexHandler(req, res); |
| 38 | + if (pathname === '/api/folders') return foldersIndexHandler(req, res); |
| 39 | + if (pathname === '/api/rbac') return rbacIndexHandler(req, res); |
| 40 | + if (pathname === '/api/rbac/docs') return rbacDocsHandler(req, res); |
| 41 | + if (pathname === '/api/rbac/folders') return rbacFoldersHandler(req, res); |
| 42 | + if (pathname === '/api/sync_queue') return syncQueueIndexHandler(req, res); |
| 43 | + if (pathname === '/api/users') return usersIndexHandler(req, res); |
| 44 | + |
| 45 | + // Dynamic ID matches |
| 46 | + if (pathname.startsWith('/api/apis/')) { |
| 47 | + req.query = { ...req.query, id: pathname.split('/').pop() }; |
| 48 | + return apisIdHandler(req, res); |
| 49 | + } |
| 50 | + if (pathname.startsWith('/api/environments/')) { |
| 51 | + req.query = { ...req.query, id: pathname.split('/').pop() }; |
| 52 | + return environmentsIdHandler(req, res); |
| 53 | + } |
| 54 | + if (pathname.startsWith('/api/folders/')) { |
| 55 | + req.query = { ...req.query, id: pathname.split('/').pop() }; |
| 56 | + return foldersIdHandler(req, res); |
| 57 | + } |
| 58 | + |
| 59 | + // 404 Fallback |
| 60 | + res.status(404).json({ error: 'Route not found in consolidated API' }); |
| 61 | + } catch (err: any) { |
| 62 | + console.error('Master Router Error:', err); |
| 63 | + res.status(500).json({ error: 'Internal Server Error', details: err.message }); |
| 64 | + } |
| 65 | +} |
0 commit comments