fix(contorller): env corrections on where to look for the file when running
This commit is contained in:
74
app/src/internal/admin/routes/servers/addServer.ts
Normal file
74
app/src/internal/admin/routes/servers/addServer.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { Router } from "express";
|
||||
import type { Request, Response } from "express";
|
||||
import {
|
||||
insertServerDataSchema,
|
||||
serverData,
|
||||
} from "../../../../pkg/db/schema/servers.js";
|
||||
import { db } from "../../../../pkg/db/db.js";
|
||||
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||
import type { DrizzleError } from "drizzle-orm";
|
||||
import axios from "axios";
|
||||
import { createLogger } from "../../../../pkg/logger/logger.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
// when a new server is posted from localhost or 127.0.0.1 we also want to post it to the test server so we can see it from there
|
||||
//res.status(200).json({ message: "Server added", ip: req.hostname });
|
||||
const log = createLogger({ module: "admin", subModule: "add server" });
|
||||
const parsed = insertServerDataSchema.safeParse(req.body);
|
||||
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ errors: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.insert(serverData)
|
||||
.values(parsed.data)
|
||||
//.onConflictDoNothing()
|
||||
.returning({
|
||||
name: serverData.name,
|
||||
plantToken: serverData.plantToken,
|
||||
})
|
||||
);
|
||||
|
||||
if (error) {
|
||||
const err: DrizzleError = error;
|
||||
return res.status(400).json({
|
||||
message: `Error adding the server`,
|
||||
error: err.cause,
|
||||
});
|
||||
}
|
||||
|
||||
if (req.hostname === "localhost" && process.env.MAIN_SERVER) {
|
||||
log.info({}, "Running in dev server about to add in a new server");
|
||||
const { data, error } = await tryCatch(
|
||||
axios.post(
|
||||
`${process.env.MAIN_SERVER}/lst/api/admin/server`,
|
||||
parsed.data,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Cookie: req.headers.cookie ?? "",
|
||||
},
|
||||
withCredentials: true,
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
if (error) {
|
||||
log.error(
|
||||
{ stack: error },
|
||||
"There was an error adding the server to Main Server"
|
||||
);
|
||||
}
|
||||
log.info({ stack: data }, "A new Server was just added to the server.");
|
||||
}
|
||||
|
||||
return res
|
||||
.status(201)
|
||||
.json({ message: `Server ${data[0]?.name} added`, data: data });
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user