hrms-api-probation/src/app.ts

71 lines
2 KiB
TypeScript
Raw Normal View History

2024-09-05 13:59:43 +07:00
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";
2024-09-05 16:56:22 +07:00
import logMiddleware from "./middlewares/logs";
2025-02-13 16:54:17 +07:00
import axios from "axios";
2024-09-05 13:59:43 +07:00
async function main() {
await AppDataSource.initialize();
const app = express();
app.use(
cors({
origin: "*",
2024-09-05 16:56:22 +07:00
}),
2024-09-05 13:59:43 +07:00
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
2024-09-05 16:56:22 +07:00
app.use(logMiddleware);
2024-09-05 13:59:43 +07:00
app.use("/", express.static("static"));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
2025-02-13 16:54:17 +07:00
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 APIAPI_DASHBOARD = `${process.env.API_DASHBOARD}/generate-pdf`;
const body = `${process.env.API_DASHBOARD}/d/${newUrl}`;
try {
// เรียก API generate-pdf
const apiResponse = await axios.post(APIAPI_DASHBOARD, { url: body });
// console.log("Response:", apiResponse.data.pdfUrl);
res.redirect(apiResponse.data.pdfUrl);
} catch (error) {
throw new Error("Failed to generate PDF: " + error);
}
});
2024-09-05 13:59:43 +07:00
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,
() => (
2024-09-05 16:56:22 +07:00
console.log(`[APP] Application is running on: http://localhost:${APP_PORT}`),
2024-09-05 13:59:43 +07:00
console.log(`[APP] Swagger on: http://localhost:${APP_PORT}/api-docs`)
2024-09-05 16:56:22 +07:00
),
2024-09-05 13:59:43 +07:00
);
}
main();