2024-05-17 23:18:15 -07:00
|
|
|
"use server";
|
2024-04-23 01:53:37 -04:00
|
|
|
|
2024-05-17 23:18:15 -07:00
|
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
import ecsClient from "./ecs";
|
|
|
|
import { CreateServiceCommand, StartTaskCommand } from "@aws-sdk/client-ecs";
|
2024-04-27 21:24:20 -04:00
|
|
|
|
2024-04-23 01:53:37 -04:00
|
|
|
export async function createSandbox(body: {
|
2024-05-17 23:18:15 -07:00
|
|
|
type: string;
|
|
|
|
name: string;
|
|
|
|
userId: string;
|
|
|
|
visibility: string;
|
2024-04-23 01:53:37 -04:00
|
|
|
}) {
|
2024-05-05 22:33:24 -07:00
|
|
|
const res = await fetch(
|
|
|
|
"https://database.ishaan1013.workers.dev/api/sandbox",
|
|
|
|
{
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
|
|
|
}
|
2024-05-17 23:18:15 -07:00
|
|
|
);
|
2024-04-27 16:22:35 -04:00
|
|
|
|
2024-05-17 23:18:15 -07:00
|
|
|
return await res.text();
|
2024-04-23 01:53:37 -04:00
|
|
|
}
|
2024-04-27 21:24:20 -04:00
|
|
|
|
|
|
|
export async function updateSandbox(body: {
|
2024-05-17 23:18:15 -07:00
|
|
|
id: string;
|
|
|
|
name?: string;
|
|
|
|
visibility?: "public" | "private";
|
2024-04-27 21:24:20 -04:00
|
|
|
}) {
|
2024-05-05 22:33:24 -07:00
|
|
|
await fetch("https://database.ishaan1013.workers.dev/api/sandbox", {
|
2024-04-27 21:24:20 -04:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(body),
|
2024-05-17 23:18:15 -07:00
|
|
|
});
|
2024-04-27 21:24:20 -04:00
|
|
|
|
2024-05-17 23:18:15 -07:00
|
|
|
revalidatePath("/dashboard");
|
2024-04-27 21:24:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function deleteSandbox(id: string) {
|
2024-05-05 22:33:24 -07:00
|
|
|
await fetch(`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`, {
|
2024-04-27 21:24:20 -04:00
|
|
|
method: "DELETE",
|
2024-05-17 23:18:15 -07:00
|
|
|
});
|
2024-04-27 21:24:20 -04:00
|
|
|
|
2024-05-17 23:18:15 -07:00
|
|
|
revalidatePath("/dashboard");
|
2024-04-27 21:24:20 -04:00
|
|
|
}
|
2024-05-01 01:29:16 -04:00
|
|
|
|
|
|
|
export async function shareSandbox(sandboxId: string, email: string) {
|
2024-05-05 22:33:24 -07:00
|
|
|
const res = await fetch(
|
|
|
|
"https://database.ishaan1013.workers.dev/api/sandbox/share",
|
|
|
|
{
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ sandboxId, email }),
|
|
|
|
}
|
2024-05-17 23:18:15 -07:00
|
|
|
);
|
|
|
|
const text = await res.text();
|
2024-05-01 01:53:49 -04:00
|
|
|
|
|
|
|
if (res.status !== 200) {
|
2024-05-17 23:18:15 -07:00
|
|
|
return { success: false, message: text };
|
2024-05-01 01:29:16 -04:00
|
|
|
}
|
2024-05-01 01:53:49 -04:00
|
|
|
|
2024-05-17 23:18:15 -07:00
|
|
|
revalidatePath(`/code/${sandboxId}`);
|
|
|
|
return { success: true, message: "Shared successfully." };
|
2024-05-01 01:29:16 -04:00
|
|
|
}
|
2024-05-01 02:22:02 -04:00
|
|
|
|
|
|
|
export async function unshareSandbox(sandboxId: string, userId: string) {
|
2024-05-05 22:33:24 -07:00
|
|
|
await fetch("https://database.ishaan1013.workers.dev/api/sandbox/share", {
|
2024-05-01 02:22:02 -04:00
|
|
|
method: "DELETE",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ sandboxId, userId }),
|
2024-05-17 23:18:15 -07:00
|
|
|
});
|
2024-05-01 02:22:02 -04:00
|
|
|
|
2024-05-17 23:18:15 -07:00
|
|
|
revalidatePath(`/code/${sandboxId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function startServer(serviceName: string) {
|
|
|
|
const command = new CreateServiceCommand({
|
|
|
|
cluster: "arn:aws:ecs:us-east-1:767398085538:service/Sandbox/Sandbox",
|
|
|
|
serviceName,
|
|
|
|
taskDefinition: "Sandbox1",
|
|
|
|
desiredCount: 1,
|
|
|
|
networkConfiguration: {
|
|
|
|
awsvpcConfiguration: {
|
|
|
|
securityGroups: ["sg-07e489fcf3299af52"],
|
|
|
|
subnets: [
|
|
|
|
"subnet-06d04f2a6ebb1710c",
|
|
|
|
"subnet-097c000f157c26a78",
|
|
|
|
"subnet-00f931ecbabaf87dd",
|
|
|
|
"subnet-0adcb82d77db9f263",
|
|
|
|
"subnet-0c6874150d8e63a7c",
|
|
|
|
"subnet-0b76f9ee3fe20660d",
|
|
|
|
],
|
|
|
|
assignPublicIp: "ENABLED",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await ecsClient.send(command);
|
|
|
|
console.log("started server:", response);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error starting server:", error);
|
|
|
|
}
|
2024-05-01 02:22:02 -04:00
|
|
|
}
|