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