93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import "dotenv/config";
|
|
import "reflect-metadata";
|
|
import cors from "cors";
|
|
import express from "express";
|
|
import swaggerUi from "swagger-ui-express";
|
|
import swaggerDocument from "./swagger.json";
|
|
import error from "./middlewares/error";
|
|
import { AppDataSource } from "./database/data-source";
|
|
import { RegisterRoutes } from "./routes";
|
|
import logMiddleware from "./middlewares/logs";
|
|
import axios from "axios";
|
|
|
|
async function main() {
|
|
await AppDataSource.initialize();
|
|
|
|
const app = express();
|
|
|
|
app.use(
|
|
cors({
|
|
origin: "*",
|
|
})
|
|
);
|
|
app.use(express.json());
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.use(logMiddleware);
|
|
app.use("/", express.static("static"));
|
|
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
|
|
|
|
app.get("/api/v1/probation/dashboard", async (req, res) => {
|
|
// ดึงค่า id จาก query params
|
|
const id = req.query.id as string;
|
|
if (!id) {
|
|
throw new Error("Missing 'id' parameter");
|
|
}
|
|
|
|
const params = new URLSearchParams(req.query as Record<string, string>);
|
|
const newUrl = params.toString() ? `${id}?${params.toString()}` : id;
|
|
|
|
const API_DASHBOARD = `${process.env.VITE_API_REPORT_URL}/html`;
|
|
const url = `${process.env.VITE_DASHBOARD_PANEL}/d/${newUrl}&kiosk`;
|
|
|
|
try {
|
|
// เรียก API generate-pdf
|
|
const apiResponse = await axios.post(
|
|
API_DASHBOARD,
|
|
{
|
|
template: url,
|
|
reportName: "dashboard_report",
|
|
htmlOption: {
|
|
querySelector: ".dashboard-content",
|
|
},
|
|
},
|
|
{
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
accept: "application/pdf",
|
|
},
|
|
responseType: "arraybuffer",
|
|
}
|
|
);
|
|
|
|
console.log("Response:", apiResponse.data);
|
|
|
|
const pdfBuffer = Buffer.from(apiResponse.data, "binary");
|
|
res.set({
|
|
"Content-Type": "application/pdf",
|
|
"Content-Disposition": "attachment; filename=dashboard.pdf",
|
|
});
|
|
res.send(pdfBuffer);
|
|
} catch (error) {
|
|
throw new Error("Failed to generate PDF: " + error);
|
|
}
|
|
});
|
|
|
|
RegisterRoutes(app);
|
|
|
|
app.use(error);
|
|
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
|
|
const APP_PORT = +(process.env.APP_PORT || 3000);
|
|
|
|
app.listen(
|
|
APP_PORT,
|
|
APP_HOST,
|
|
() => (
|
|
console.log(
|
|
`[APP] Application is running on: http://localhost:${APP_PORT}`
|
|
),
|
|
console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`)
|
|
)
|
|
);
|
|
}
|
|
|
|
main();
|