-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from adityak74/api-server-crawler
feat: create crawler api server
- Loading branch information
Showing
9 changed files
with
8,931 additions
and
502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
API_PORT=5000 | ||
API_HOST=localhost | ||
MAX_PAGES_TO_CRAWL=45 | ||
NODE_ENV=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ storage | |
|
||
# any output from the crawler | ||
*.json | ||
.env | ||
pnpm-lock.yaml |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import 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(); | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import swaggerAutogen from "swagger-autogen"; | ||
|
||
const doc = { | ||
info: { | ||
title: "GPT Crawler API", | ||
description: "GPT Crawler", | ||
}, | ||
host: "localhost:5000", | ||
}; | ||
|
||
const outputFile = "swagger-output.json"; | ||
const routes = ["./src/server.ts"]; | ||
|
||
swaggerAutogen()(outputFile, routes, doc); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters