feat(servers): cru added in to the server and dynamically updates vms036
This commit is contained in:
@@ -9,6 +9,7 @@ import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||
import type { DrizzleError } from "drizzle-orm";
|
||||
import axios from "axios";
|
||||
import { createLogger } from "../../../../pkg/logger/logger.js";
|
||||
import https from "https";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -43,6 +44,28 @@ router.post("/", async (req: Request, res: Response) => {
|
||||
|
||||
if (req.hostname === "localhost" && process.env.MAIN_SERVER) {
|
||||
log.info({}, "Running in dev server about to add in a new server");
|
||||
const axiosInstance = axios.create({
|
||||
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
|
||||
baseURL: process.env.MAIN_SERVER, // e.g. "https://example.com"
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
const loginRes = await axiosInstance.post(
|
||||
`${process.env.MAIN_SERVER}/lst/api/auth/sign-in/username`,
|
||||
{
|
||||
username: process.env.MAIN_SERVER_USERNAME,
|
||||
password: process.env.MAIN_SERVER_PASSWORD,
|
||||
},
|
||||
{
|
||||
headers: { "Content-Type": "application/json" },
|
||||
}
|
||||
);
|
||||
const setCookie = loginRes.headers["set-cookie"];
|
||||
|
||||
if (!setCookie) {
|
||||
throw new Error("Did not receive a Set-Cookie header from login");
|
||||
}
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
axios.post(
|
||||
`${process.env.MAIN_SERVER}/lst/api/admin/server`,
|
||||
@@ -50,7 +73,7 @@ router.post("/", async (req: Request, res: Response) => {
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Cookie: req.headers.cookie ?? "",
|
||||
Cookie: setCookie.join("; "),
|
||||
},
|
||||
withCredentials: true,
|
||||
}
|
||||
@@ -63,7 +86,10 @@ router.post("/", async (req: Request, res: Response) => {
|
||||
"There was an error adding the server to Main Server"
|
||||
);
|
||||
}
|
||||
log.info({ stack: data }, "A new Server was just added to the server.");
|
||||
log.info(
|
||||
{ stack: data?.data },
|
||||
"A new Server was just added to the server."
|
||||
);
|
||||
}
|
||||
|
||||
return res
|
||||
|
||||
Reference in New Issue
Block a user