fix(stand-alone-server): set ip to local context

This commit is contained in:
yanlong.wang 2025-02-20 18:36:04 +08:00
parent 9dbecbbc9f
commit 7340028c64
No known key found for this signature in database
GPG Key ID: C0A623C0BADF9F37
3 changed files with 11 additions and 7 deletions

View File

@ -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;

View File

@ -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')
);
}

View File

@ -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')
);