2024-11-03 13:57:51 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { sites } from "@server/db/schema";
|
|
|
|
import { eq } from "drizzle-orm";
|
2024-10-01 21:34:07 -04:00
|
|
|
import response from "@server/utils/response";
|
2024-11-03 13:57:51 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
|
|
|
import { deletePeer } from "../gerbil/peers";
|
|
|
|
import { fromError } from "zod-validation-error";
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-10-03 22:31:20 -04:00
|
|
|
const API_BASE_URL = "http://localhost:3000";
|
|
|
|
|
2024-10-01 21:53:49 -04:00
|
|
|
const deleteSiteSchema = z.object({
|
2024-11-03 13:57:51 -05:00
|
|
|
siteId: z.string().transform(Number).pipe(z.number().int().positive()),
|
2024-10-01 21:53:49 -04:00
|
|
|
});
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
export async function deleteSite(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
|
|
|
const parsedParams = deleteSiteSchema.safeParse(req.params);
|
|
|
|
if (!parsedParams.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedParams.error).toString()
|
2024-10-06 18:05:20 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const { siteId } = parsedParams.data;
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-11-03 13:57:51 -05:00
|
|
|
const [deletedSite] = await db
|
|
|
|
.delete(sites)
|
2024-10-06 18:05:20 -04:00
|
|
|
.where(eq(sites.siteId, siteId))
|
|
|
|
.returning();
|
|
|
|
|
2024-10-26 22:44:34 -04:00
|
|
|
if (!deletedSite) {
|
2024-10-06 18:05:20 -04:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.NOT_FOUND,
|
|
|
|
`Site with ID ${siteId} not found`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-26 22:44:34 -04:00
|
|
|
await deletePeer(deletedSite.exitNodeId!, deletedSite.pubKey);
|
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
|
|
|
data: null,
|
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Site deleted successfully",
|
|
|
|
status: HttpCode.OK,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-11-03 13:57:51 -05:00
|
|
|
return next(
|
2024-11-05 23:55:46 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-11-03 13:57:51 -05:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
|
|
|
|
async function removePeer(publicKey: string) {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
2024-11-03 13:57:51 -05:00
|
|
|
const response = await fetch(
|
|
|
|
`${API_BASE_URL}/peer?public_key=${encodeURIComponent(publicKey)}`,
|
|
|
|
{
|
|
|
|
method: "DELETE",
|
|
|
|
}
|
|
|
|
);
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const data = await response.json();
|
2024-11-03 13:57:51 -05:00
|
|
|
console.log("Peer removed successfully:", data.status);
|
2024-10-06 18:05:20 -04:00
|
|
|
return data;
|
|
|
|
} catch (error: any) {
|
2024-11-03 13:57:51 -05:00
|
|
|
console.error("Error removing peer:", error.message);
|
2024-10-06 18:05:20 -04:00
|
|
|
throw error;
|
|
|
|
}
|
2024-10-03 22:31:20 -04:00
|
|
|
}
|