import { Command } from "jsr:@cliffy/command@1.0.0-rc.7"; import * as colors from "jsr:@std/fmt@0.225.2/colors"; import { AuthSetupConfig, DEFAULT_AUTH_CONFIG, readEnv } from "../env.ts"; export async function handleTestConnection(domainName: string): Promise { console.log( colors.bold( colors.blue(`\n=== Testing Auth Connection (https://${domainName}) ===`), ), ); try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 5000); const res1 = await fetch(`https://${domainName}/`, { signal: controller.signal, }); clearTimeout(timeoutId); if (res1.status === 502 || res1.status === 503 || res1.status === 504) { throw new Error(`Gateway error: ${res1.status}`); } if (res1.body) { await res1.body.cancel(); } console.log( colors.green( `✓ Auth service is up and reachable at https://${domainName}/`, ), ); } catch (error) { console.log( colors.red( `✗ Error: Could not reach the auth service at https://${domainName}/`, ), ); if (error instanceof Error) { console.log(colors.red(` Reason: ${error.message}`)); } else { console.log(colors.red(` Reason: ${error}`)); } console.log( colors.red( " Please verify the container is running and DNS/Traefik is resolving.", ), ); } } export const testCommand = new Command() .description("Test Auth Connection") .action(async () => { const loadedEnv = await readEnv(); const currentConfig: AuthSetupConfig = { ...DEFAULT_AUTH_CONFIG, ...loadedEnv, }; await handleTestConnection(currentConfig.domainName); });