Commit bc672ef2 authored by nanahira's avatar nanahira

add etag to apps.json

parent 93546306
Pipeline #40983 canceled with stages
in 53 seconds
...@@ -11,15 +11,21 @@ export class UpdateController { ...@@ -11,15 +11,21 @@ export class UpdateController {
constructor(private readonly updateService: UpdateService) {} constructor(private readonly updateService: UpdateService) {}
@Get('apps.json') @Get('apps.json')
@ApiOperation({ summary: '获取 apps.json', description: '懒得解释这是啥了……' }) @ApiOperation({ summary: '获取 apps.json', description: '懒得解释这是啥了……' })
@Header('Cache-Control', 'public, max-age=600, stale-while-revalidate=600, stale-if-error=604800') async getAppsJson(@Res({ passthrough: true }) res: Response) {
getAppsJson() { const result = await this.updateService.getAppsJson();
return this.updateService.getAppsJson(); this.addETag(res, JSON.stringify(result));
res.setHeader('Cache-Control', 'public, max-age=600, stale-while-revalidate=600, stale-if-error=604800');
return result;
}
private addETag(res: Response, data: any) {
const hash = createHash('sha512').update(JSON.stringify(data)).digest('hex');
res.setHeader('ETag', `"${hash}"`);
} }
private async cacheResult<T>(res: Response, prom: Promise<T>) { private async cacheResult<T>(res: Response, prom: Promise<T>) {
const result = await prom; const result = await prom;
const hash = createHash('sha512').update(JSON.stringify(result)).digest('hex'); this.addETag(res, result);
res.setHeader('ETag', `"${hash}"`); // minio-compatibility requires quotes
if (res.getHeader('Cache-Control') === undefined) res.setHeader('Cache-Control', 'public, max-age=31536000, immutable'); if (res.getHeader('Cache-Control') === undefined) res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment