2025-08-25 17:11:15 +08:00
|
|
|
import dotenv from "dotenv";
|
|
|
|
|
import express from "express";
|
|
|
|
|
import cors from "cors";
|
|
|
|
|
import compression from "compression";
|
|
|
|
|
import nocache from "nocache";
|
|
|
|
|
import path from "path";
|
|
|
|
|
import expressOasGenerator, {
|
|
|
|
|
OpenAPIV3,
|
|
|
|
|
SPEC_OUTPUT_FILE_BEHAVIOR,
|
|
|
|
|
} from "express-oas-generator";
|
|
|
|
|
|
|
|
|
|
dotenv.config({ path: path.resolve(__dirname, "../../../.env") });
|
|
|
|
|
|
|
|
|
|
import "./database";
|
2025-09-12 16:03:27 +08:00
|
|
|
import { sequelize } from "@/database/instance";
|
2025-08-25 17:11:15 +08:00
|
|
|
import { createApis } from "./api";
|
|
|
|
|
|
|
|
|
|
const port = process.env.PORT || 3005;
|
|
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
app.set("etag", false);
|
|
|
|
|
app.use(nocache());
|
|
|
|
|
app.use(cors());
|
|
|
|
|
app.use(express.json({ limit: "100mb" }));
|
|
|
|
|
app.use(compression());
|
|
|
|
|
|
|
|
|
|
app.use(express.static(path.resolve(__dirname, "client")));
|
|
|
|
|
|
|
|
|
|
createApis(app);
|
|
|
|
|
|
|
|
|
|
expressOasGenerator.handleResponses(app, {
|
|
|
|
|
specOutputFileBehavior: SPEC_OUTPUT_FILE_BEHAVIOR.RECREATE,
|
|
|
|
|
swaggerDocumentOptions: {},
|
|
|
|
|
predefinedSpec: (spec: OpenAPIV3.Document) => {
|
|
|
|
|
if (spec.info) {
|
|
|
|
|
spec.info.description = "Egret App Server";
|
|
|
|
|
}
|
|
|
|
|
if (spec.paths) {
|
|
|
|
|
delete spec.paths["/v3/api-docs"];
|
|
|
|
|
}
|
|
|
|
|
return spec;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expressOasGenerator.handleRequests();
|
|
|
|
|
|
|
|
|
|
app.get("/v3/api-docs", async (req, res) => {
|
|
|
|
|
expressOasGenerator.getSpecV3((err, doc) => {
|
|
|
|
|
res.status(200).json(doc);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const host = "0.0.0.0";
|
2025-09-12 11:01:39 +08:00
|
|
|
app.listen(Number(port), host, async () => {
|
|
|
|
|
try {
|
|
|
|
|
await sequelize.sync({ alter: true });
|
2025-09-12 16:03:27 +08:00
|
|
|
console.log("[server]: sequelize.sync() executed");
|
2025-09-12 11:01:39 +08:00
|
|
|
} catch (e) {
|
2025-09-12 16:03:27 +08:00
|
|
|
console.error("Failed to sync database:", e);
|
2025-09-12 11:01:39 +08:00
|
|
|
}
|
2025-08-25 17:11:15 +08:00
|
|
|
console.log(`[server]: Server is running at http://${host}:${port}`);
|
|
|
|
|
});
|