add an express endpoint to run the crawl endpoint

This commit is contained in:
Harsh Gupta 2024-08-14 19:34:53 +05:30
parent 57b07507d1
commit 3e2bf6d39d

View File

@ -1,3 +1,4 @@
import "reflect-metadata"
import express from 'express'; import express from 'express';
import { container } from 'tsyringe'; import { container } from 'tsyringe';
import { CrawlerHost } from './cloud-functions/crawler'; import { CrawlerHost } from './cloud-functions/crawler';
@ -5,12 +6,17 @@ import { CrawlerHost } from './cloud-functions/crawler';
const app = express(); const app = express();
const port = process.env.PORT || 3000; const port = process.env.PORT || 3000;
container.registerSingleton(CrawlerHost);
const crawlerHost = container.resolve(CrawlerHost); const crawlerHost = container.resolve(CrawlerHost);
app.use(express.json()); app.use(express.json());
app.post('/crawl', async (req, res) => { // Example curl for /crawl:
// curl -X GET "http://localhost:3000/https://example.com"
app.get('/:url(*)', async (req, res) => {
try { try {
const url = req.params.url;
await crawlerHost.crawl(req, res); await crawlerHost.crawl(req, res);
} catch (error) { } catch (error) {
console.error('Error during crawl:', error); console.error('Error during crawl:', error);
@ -18,29 +24,10 @@ app.post('/crawl', async (req, res) => {
} }
}); });
app.listen(port, () => { // Example curl for /hello:
console.log(`Server is running on port ${port}`); // curl -X GET "http://localhost:3000/hello"
}); app.get('/hello', (req, res) => {
res.json({ message: 'Hello, World!' });
export default app;
import express from 'express';
import { container } from 'tsyringe';
import { CrawlerHost } from './cloud-functions/crawler';
const app = express();
const port = process.env.PORT || 3000;
const crawlerHost = container.resolve(CrawlerHost);
app.use(express.json());
app.post('/crawl', async (req, res) => {
try {
await crawlerHost.crawl(req, res);
} catch (error) {
console.error('Error during crawl:', error);
res.status(500).json({ error: 'An error occurred during the crawl' });
}
}); });
app.listen(port, () => { app.listen(port, () => {