|
1 |
| -import express from 'express'; |
2 |
| -import cors from 'cors'; |
3 |
| -import { readFile } from 'fs/promises'; |
| 1 | +import express from "express"; |
| 2 | +import cors from "cors"; |
| 3 | +import { readFile } from "fs/promises"; |
4 | 4 | import { crawl, write } from "./core.js";
|
5 |
| -import { Config, configSchema } from './config.js'; |
6 |
| -import { configDotenv } from 'dotenv'; |
7 |
| -import swaggerUi from 'swagger-ui-express'; |
| 5 | +import { Config, configSchema } from "./config.js"; |
| 6 | +import { configDotenv } from "dotenv"; |
| 7 | +import swaggerUi from "swagger-ui-express"; |
8 | 8 | // @ts-ignore
|
9 |
| -import swaggerDocument from '../swagger-output.json' assert { type: 'json' }; |
| 9 | +import swaggerDocument from "../swagger-output.json" assert { type: "json" }; |
10 | 10 |
|
11 | 11 | configDotenv();
|
12 | 12 |
|
13 | 13 | const app = express();
|
14 | 14 | const port = Number(process.env.API_PORT) || 3000;
|
15 |
| -const hostname = process.env.API_HOST || 'localhost'; |
| 15 | +const hostname = process.env.API_HOST || "localhost"; |
16 | 16 |
|
17 | 17 | app.use(cors());
|
18 | 18 | app.use(express.json());
|
19 |
| -app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); |
| 19 | +app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument)); |
20 | 20 |
|
21 | 21 | // Define a POST route to accept config and run the crawler
|
22 |
| -app.post('/crawl', async (req, res) => { |
23 |
| - const config: Config = req.body; |
24 |
| - try { |
25 |
| - const validatedConfig = configSchema.parse(config); |
26 |
| - await crawl(validatedConfig); |
27 |
| - await write(validatedConfig); |
28 |
| - const outputFileContent = await readFile(validatedConfig.outputFileName, 'utf-8'); |
29 |
| - res.contentType('application/json'); |
30 |
| - return res.send(outputFileContent); |
31 |
| - } catch (error) { |
32 |
| - return res.status(500).json({ message: 'Error occurred during crawling', error }); |
33 |
| - } |
| 22 | +app.post("/crawl", async (req, res) => { |
| 23 | + const config: Config = req.body; |
| 24 | + try { |
| 25 | + const validatedConfig = configSchema.parse(config); |
| 26 | + await crawl(validatedConfig); |
| 27 | + await write(validatedConfig); |
| 28 | + const outputFileContent = await readFile( |
| 29 | + validatedConfig.outputFileName, |
| 30 | + "utf-8", |
| 31 | + ); |
| 32 | + res.contentType("application/json"); |
| 33 | + return res.send(outputFileContent); |
| 34 | + } catch (error) { |
| 35 | + return res |
| 36 | + .status(500) |
| 37 | + .json({ message: "Error occurred during crawling", error }); |
| 38 | + } |
34 | 39 | });
|
35 | 40 |
|
36 | 41 | app.listen(port, hostname, () => {
|
37 |
| - console.log(`API server listening at http://${hostname}:${port}`); |
| 42 | + console.log(`API server listening at http://${hostname}:${port}`); |
38 | 43 | });
|
39 | 44 |
|
40 | 45 | export default app;
|
0 commit comments