← back to Hub

app/api/apps/status/route.ts

55 lines

import { NextRequest, NextResponse } from 'next/server';
import { verifyAuth } from '@/lib/auth';

export const dynamic = 'force-dynamic';

const APPS = [
  { name: 'SDCC', port: 7400, color: '#3b82f6' },
  { name: 'Grant', port: 7450, color: '#059669' },
  { name: 'Patty', port: 7460, color: '#7c3aed' },
  { name: 'Freddy', port: 7470, color: '#d97706' },
];

async function checkApp(app: { name: string; port: number; color: string }) {
  const start = Date.now();
  try {
    const controller = new AbortController();
    const timeout = setTimeout(() => controller.abort(), 3000);
    const res = await fetch(`http://127.0.0.1:${app.port}`, {
      signal: controller.signal,
      redirect: 'follow',
    });
    clearTimeout(timeout);
    const responseTime = Date.now() - start;
    return {
      ...app,
      status: res.ok || res.status === 307 || res.status === 302 ? 'online' as const : 'offline' as const,
      responseTime,
    };
  } catch {
    return {
      ...app,
      status: 'offline' as const,
      responseTime: Date.now() - start,
    };
  }
}

export async function GET(request: NextRequest) {
  const user = verifyAuth(request);
  if (!user) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  try {
    const results = await Promise.all(APPS.map(checkApp));
    return NextResponse.json({ apps: results });
  } catch (err) {
    console.error('[apps/status] Error:', (err as Error).message);
    return NextResponse.json(
      { error: 'Failed to check app status' },
      { status: 500 },
    );
  }
}