({ request }: LoaderFunctionArgs)
| 23 | } |
| 24 | |
| 25 | export async function action({ request }: LoaderFunctionArgs) { |
| 26 | // Next authenticate the request |
| 27 | const authenticationResult = await authenticateApiRequest(request); |
| 28 | |
| 29 | if (!authenticationResult) { |
| 30 | return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 31 | } |
| 32 | |
| 33 | if (authenticationResult.environment.type !== "DEVELOPMENT") { |
| 34 | return json({ error: "Tunneling is only supported in development" }, { status: 501 }); |
| 35 | } |
| 36 | |
| 37 | if (!env.TUNNEL_HOST || !env.TUNNEL_SECRET_KEY) { |
| 38 | return json({ error: "Tunneling is not supported" }, { status: 501 }); |
| 39 | } |
| 40 | |
| 41 | const yaltClient = new YaltApiClient(env.TUNNEL_HOST, env.TUNNEL_SECRET_KEY); |
| 42 | |
| 43 | let tunnelId = authenticationResult.environment.tunnelId; |
| 44 | |
| 45 | if (!tunnelId) { |
| 46 | try { |
| 47 | tunnelId = await yaltClient.createTunnel(); |
| 48 | |
| 49 | await prisma.runtimeEnvironment.update({ |
| 50 | where: { |
| 51 | id: authenticationResult.environment.id, |
| 52 | }, |
| 53 | data: { |
| 54 | tunnelId, |
| 55 | }, |
| 56 | }); |
| 57 | } catch (error) { |
| 58 | logger.error("Failed to create tunnel", { error }); |
| 59 | |
| 60 | return json({ error: "Failed to create tunnel" }, { status: 500 }); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if (!tunnelId) { |
| 65 | return json({ error: "Failed to create tunnel" }, { status: 500 }); |
| 66 | } |
| 67 | |
| 68 | return json({ url: yaltClient.connectUrl(tunnelId) }); |
| 69 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…