dashboard.accords-library.com/src/server.ts

73 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-07-22 18:32:18 +00:00
import "dotenv/config";
2023-08-11 21:11:10 +00:00
import express from "express";
2023-07-22 18:32:18 +00:00
import path from "path";
2023-08-11 21:11:10 +00:00
import payload from "payload";
import { Collections, RecordersRoles } from "./constants";
import { Recorder } from "./types/collections";
2023-08-14 20:09:29 +00:00
import { PayloadCreateData } from "./types/payload";
import { isDefined, isUndefined } from "./utils/asserts";
2023-07-14 11:03:01 +00:00
const app = express();
// Redirect root to Admin panel
app.get("/", (_, res) => {
res.redirect("/admin");
});
const start = async () => {
// Initialize Payload
2023-08-14 20:09:29 +00:00
if (isUndefined(process.env.PAYLOAD_SECRET)) {
throw new Error("Missing required env variable: PAYLOAD_SECRET");
}
if (isUndefined(process.env.MONGODB_URI)) {
throw new Error("Missing required env variable: MONGODB_URI");
}
2023-07-14 11:03:01 +00:00
await payload.init({
secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGODB_URI,
express: app,
onInit: async () => {
payload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
2023-08-11 21:11:10 +00:00
const recorders = await payload.find({ collection: Collections.Recorders });
// If no recorders, we seed some initial data
2023-08-14 20:09:29 +00:00
if (
isDefined(process.env.SEEDING_ADMIN_EMAIL) &&
isDefined(process.env.SEEDING_ADMIN_PASSWORD) &&
isDefined(process.env.SEEDING_ADMIN_USERNAME)
) {
if (recorders.docs.length === 0) {
payload.logger.info("Seeding some initial data");
const recorder: PayloadCreateData<Recorder> = {
email: process.env.SEEDING_ADMIN_EMAIL,
password: process.env.SEEDING_ADMIN_PASSWORD,
username: process.env.SEEDING_ADMIN_USERNAME,
role: [RecordersRoles.Admin],
anonymize: false,
};
await payload.create({
collection: Collections.Recorders,
data: recorder,
});
}
2023-08-11 21:11:10 +00:00
}
2023-07-14 11:03:01 +00:00
},
});
// Add your own express routes here
2023-07-22 18:32:18 +00:00
app.use("/public", express.static(path.join(__dirname, "../public")));
2023-07-14 11:03:01 +00:00
2023-08-11 21:11:10 +00:00
app.get("/robots.txt", (_, res) => {
res.type("text/plain");
res.send("User-agent: *\nDisallow: /");
});
2023-07-22 19:12:41 +00:00
app.listen(process.env.PAYLOAD_PORT);
2023-07-14 11:03:01 +00:00
};
start();