From 7340028c64e4abaf08f7e7a6e44428871929bf0d Mon Sep 17 00:00:00 2001 From: "yanlong.wang" Date: Thu, 20 Feb 2025 18:36:04 +0800 Subject: [PATCH] fix(stand-alone-server): set ip to local context --- backend/functions/src/services/serper-search.ts | 2 +- backend/functions/src/stand-alone/crawl.ts | 8 +++++--- backend/functions/src/stand-alone/search.ts | 8 +++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/backend/functions/src/services/serper-search.ts b/backend/functions/src/services/serper-search.ts index bd9ee76..b8f6507 100644 --- a/backend/functions/src/services/serper-search.ts +++ b/backend/functions/src/services/serper-search.ts @@ -53,13 +53,13 @@ export class SerperSearchService extends AsyncService { if (locationChunks.length) { query.location ??= locationChunks.join(', '); } - } let maxTries = 3; while (maxTries--) { try { + this.logger.debug(`Doing external search`, query); const r = await this.serperSearchHTTP.webSearch(query); return r.parsed; diff --git a/backend/functions/src/stand-alone/crawl.ts b/backend/functions/src/stand-alone/crawl.ts index 76f9bcd..0e2f75d 100644 --- a/backend/functions/src/stand-alone/crawl.ts +++ b/backend/functions/src/stand-alone/crawl.ts @@ -11,7 +11,7 @@ process.env['FIREBASE_CONFIG'] ??= JSON.stringify({ initializeApp(); -import { Logger, CloudFunctionRegistry } from '../shared'; +import { Logger, CloudFunctionRegistry, AsyncContext } from '../shared'; import { AbstractRPCRegistry, OpenAPIManager } from 'civkit/civ-rpc'; import { ExpressServer } from 'civkit/civ-rpc/express'; import http2 from 'http2'; @@ -46,6 +46,7 @@ export class CrawlStandAloneServer extends ExpressServer { protected globalLogger: Logger, protected registry: CloudFunctionRegistry, protected crawlerHost: CrawlerHost, + protected threadLocal: AsyncContext, ) { super(...arguments); @@ -98,11 +99,12 @@ export class CrawlStandAloneServer extends ExpressServer { }; } - makeIgnoreOPTIONSMiddleware() { + makeMiscMiddleware() { return (req: Request, res: Response, next: NextFunction) => { if (req.method === 'OPTIONS') { return res.status(200).end(); } + this.threadLocal.set('ip', req.ip); return next(); }; @@ -145,7 +147,7 @@ export class CrawlStandAloneServer extends ExpressServer { this.expressRootRouter.use('/', ...this.registry.expressMiddlewares, this.makeAssetsServingController(), - this.makeIgnoreOPTIONSMiddleware(), + this.makeMiscMiddleware(), this.registry.makeShimController('crawl') ); } diff --git a/backend/functions/src/stand-alone/search.ts b/backend/functions/src/stand-alone/search.ts index 0461d13..180cc9b 100644 --- a/backend/functions/src/stand-alone/search.ts +++ b/backend/functions/src/stand-alone/search.ts @@ -11,7 +11,7 @@ process.env['FIREBASE_CONFIG'] ??= JSON.stringify({ initializeApp(); -import { Logger, CloudFunctionRegistry } from '../shared'; +import { Logger, CloudFunctionRegistry, AsyncContext } from '../shared'; import { AbstractRPCRegistry, OpenAPIManager } from 'civkit/civ-rpc'; import { ExpressServer } from 'civkit/civ-rpc/express'; import http2 from 'http2'; @@ -46,6 +46,7 @@ export class SearchStandAloneServer extends ExpressServer { protected globalLogger: Logger, protected registry: CloudFunctionRegistry, protected searcherHost: SearcherHost, + protected threadLocal: AsyncContext, ) { super(...arguments); @@ -98,11 +99,12 @@ export class SearchStandAloneServer extends ExpressServer { }; } - makeIgnoreOPTIONSMiddleware() { + makeMiscMiddleware() { return (req: Request, res: Response, next: NextFunction) => { if (req.method === 'OPTIONS') { return res.status(200).end(); } + this.threadLocal.set('ip', req.ip); return next(); }; @@ -144,7 +146,7 @@ export class SearchStandAloneServer extends ExpressServer { this.expressRootRouter.use('/', ...this.registry.expressMiddlewares, - this.makeIgnoreOPTIONSMiddleware(), + this.makeMiscMiddleware(), this.makeAssetsServingController(), this.registry.makeShimController('search') );