2026-01-14 16:25:01 +08:00
|
|
|
import { Controller, Post, Get, Param, Body } from '@nestjs/common';
|
2026-01-09 23:18:52 +08:00
|
|
|
import { BidCrawlerService } from './services/bid-crawler.service';
|
|
|
|
|
|
|
|
|
|
@Controller('api/crawler')
|
|
|
|
|
export class CrawlerController {
|
2026-01-12 02:09:48 +08:00
|
|
|
private isCrawling = false;
|
2026-01-14 16:25:01 +08:00
|
|
|
private crawlingSources = new Set<string>();
|
2026-01-12 02:09:48 +08:00
|
|
|
|
2026-01-09 23:18:52 +08:00
|
|
|
constructor(private readonly crawlerService: BidCrawlerService) {}
|
|
|
|
|
|
2026-01-12 02:09:48 +08:00
|
|
|
@Get('status')
|
|
|
|
|
getStatus() {
|
2026-01-14 16:25:01 +08:00
|
|
|
return {
|
|
|
|
|
isCrawling: this.isCrawling,
|
2026-01-14 22:26:32 +08:00
|
|
|
crawlingSources: Array.from(this.crawlingSources),
|
2026-01-14 16:25:01 +08:00
|
|
|
};
|
2026-01-12 02:09:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-09 23:18:52 +08:00
|
|
|
@Post('run')
|
|
|
|
|
async runCrawl() {
|
2026-01-12 02:09:48 +08:00
|
|
|
if (this.isCrawling) {
|
|
|
|
|
return { message: 'Crawl is already running' };
|
|
|
|
|
}
|
2026-01-14 22:26:32 +08:00
|
|
|
|
2026-01-12 02:09:48 +08:00
|
|
|
this.isCrawling = true;
|
2026-01-14 22:26:32 +08:00
|
|
|
|
2026-01-14 16:25:01 +08:00
|
|
|
// We don't await this because we want it to run in the background
|
2026-01-12 02:09:48 +08:00
|
|
|
// and return immediately, or we can await if we want to user to wait.
|
2026-01-09 23:18:52 +08:00
|
|
|
// Given the requirement "Immediate Crawl", usually implies triggering it.
|
|
|
|
|
// However, for a better UI experience, we might want to wait or just trigger.
|
2026-01-14 16:25:01 +08:00
|
|
|
// Let's await it so that user knows when it's done (or failed),
|
|
|
|
|
// assuming it doesn't take too long for the mock.
|
2026-01-09 23:18:52 +08:00
|
|
|
// Real crawling might take long, so background is better.
|
|
|
|
|
// For this prototype, I'll await it to show completion.
|
2026-01-12 02:09:48 +08:00
|
|
|
try {
|
|
|
|
|
await this.crawlerService.crawlAll();
|
|
|
|
|
return { message: 'Crawl completed successfully' };
|
|
|
|
|
} finally {
|
|
|
|
|
this.isCrawling = false;
|
|
|
|
|
}
|
2026-01-09 23:18:52 +08:00
|
|
|
}
|
2026-01-14 16:25:01 +08:00
|
|
|
|
|
|
|
|
@Post('crawl/:sourceName')
|
|
|
|
|
async crawlSingleSource(@Param('sourceName') sourceName: string) {
|
|
|
|
|
if (this.crawlingSources.has(sourceName)) {
|
|
|
|
|
return { message: `Source ${sourceName} is already being crawled` };
|
|
|
|
|
}
|
2026-01-14 22:26:32 +08:00
|
|
|
|
2026-01-14 16:25:01 +08:00
|
|
|
this.crawlingSources.add(sourceName);
|
2026-01-14 22:26:32 +08:00
|
|
|
|
2026-01-14 16:25:01 +08:00
|
|
|
try {
|
|
|
|
|
const result = await this.crawlerService.crawlSingleSource(sourceName);
|
|
|
|
|
return result;
|
|
|
|
|
} finally {
|
|
|
|
|
this.crawlingSources.delete(sourceName);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-01-09 23:18:52 +08:00
|
|
|
}
|