Browse Source

feat(api): rewrite HtmlTemplateMiddleware

pull/4889/head
KenTandrian 3 weeks ago
committed by Thomas Kaul
parent
commit
25a37927ef
  1. 154
      apps/api/src/middlewares/html-template.middleware.ts

154
apps/api/src/middlewares/html-template.middleware.ts

@ -7,30 +7,14 @@ import {
} from '@ghostfolio/common/config'; } from '@ghostfolio/common/config';
import { DATE_FORMAT, interpolate } from '@ghostfolio/common/helper'; import { DATE_FORMAT, interpolate } from '@ghostfolio/common/helper';
import { Injectable, NestMiddleware } from '@nestjs/common';
import { format } from 'date-fns'; import { format } from 'date-fns';
import { NextFunction, Request, Response } from 'express'; import { NextFunction, Request, Response } from 'express';
import * as fs from 'fs'; import * as fs from 'fs';
import { join } from 'path'; import { join } from 'path';
const i18nService = new I18nService();
let indexHtmlMap: { [languageCode: string]: string } = {};
const title = 'Ghostfolio'; const title = 'Ghostfolio';
try {
indexHtmlMap = SUPPORTED_LANGUAGE_CODES.reduce(
(map, languageCode) => ({
...map,
[languageCode]: fs.readFileSync(
join(__dirname, '..', 'client', languageCode, 'index.html'),
'utf8'
)
}),
{}
);
} catch {}
const locales = { const locales = {
'/de/blog/2023/01/ghostfolio-auf-sackgeld-vorgestellt': { '/de/blog/2023/01/ghostfolio-auf-sackgeld-vorgestellt': {
featureGraphicPath: 'assets/images/blog/ghostfolio-x-sackgeld.png', featureGraphicPath: 'assets/images/blog/ghostfolio-x-sackgeld.png',
@ -94,71 +78,89 @@ const locales = {
} }
}; };
const isFileRequest = (filename: string) => { @Injectable()
if (filename === '/assets/LICENSE') { export class HtmlTemplateMiddleware implements NestMiddleware {
return true; private indexHtmlMap: { [languageCode: string]: string } = {};
} else if (
filename.includes('auth/ey') ||
filename.includes(
'personal-finance-tools/open-source-alternative-to-de.fi'
) ||
filename.includes(
'personal-finance-tools/open-source-alternative-to-markets.sh'
)
) {
return false;
}
return filename.split('.').pop() !== filename; constructor(private readonly i18nService: I18nService) {
}; try {
this.indexHtmlMap = SUPPORTED_LANGUAGE_CODES.reduce(
(map, languageCode) => ({
...map,
[languageCode]: fs.readFileSync(
join(__dirname, '..', 'client', languageCode, 'index.html'),
'utf8'
)
}),
{}
);
} catch (error) {
console.error('Failed to load index.html files:', error);
}
}
export const HtmlTemplateMiddleware = async ( use(request: Request, response: Response, next: NextFunction) {
request: Request, const path = request.originalUrl.replace(/\/$/, '');
response: Response, let languageCode = path.substr(1, 2);
next: NextFunction
) => {
const path = request.originalUrl.replace(/\/$/, '');
let languageCode = path.substr(1, 2);
if (!SUPPORTED_LANGUAGE_CODES.includes(languageCode)) { if (!SUPPORTED_LANGUAGE_CODES.includes(languageCode)) {
languageCode = DEFAULT_LANGUAGE_CODE; languageCode = DEFAULT_LANGUAGE_CODE;
} }
const currentDate = format(new Date(), DATE_FORMAT); const currentDate = format(new Date(), DATE_FORMAT);
const rootUrl = process.env.ROOT_URL || environment.rootUrl; const rootUrl = process.env.ROOT_URL || environment.rootUrl;
if ( if (
path.startsWith('/api/') || path.startsWith('/api/') ||
path.startsWith(STORYBOOK_PATH) || path.startsWith(STORYBOOK_PATH) ||
isFileRequest(path) || this.isFileRequest(path) ||
!environment.production !environment.production
) { ) {
// Skip // Skip
next(); next();
} else { } else {
const indexHtml = interpolate(indexHtmlMap[languageCode], { const indexHtml = interpolate(this.indexHtmlMap[languageCode], {
currentDate, currentDate,
languageCode,
path,
rootUrl,
description: i18nService.getTranslation({
languageCode, languageCode,
id: 'metaDescription' path,
}), rootUrl,
featureGraphicPath: description: this.i18nService.getTranslation({
locales[path]?.featureGraphicPath ?? 'assets/cover.png', languageCode,
keywords: i18nService.getTranslation({ id: 'metaDescription'
languageCode, }),
id: 'metaKeywords' featureGraphicPath:
}), locales[path]?.featureGraphicPath ?? 'assets/cover.png',
title: keywords: this.i18nService.getTranslation({
locales[path]?.title ??
`${title}${i18nService.getTranslation({
languageCode, languageCode,
id: 'slogan' id: 'metaKeywords'
})}` }),
}); title:
locales[path]?.title ??
`${title}${this.i18nService.getTranslation({
languageCode,
id: 'slogan'
})}`
});
return response.send(indexHtml); return response.send(indexHtml);
}
} }
};
private isFileRequest(filename: string) {
if (filename === '/assets/LICENSE') {
return true;
} else if (
filename.includes('auth/ey') ||
filename.includes(
'personal-finance-tools/open-source-alternative-to-de.fi'
) ||
filename.includes(
'personal-finance-tools/open-source-alternative-to-markets.sh'
)
) {
return false;
}
return filename.split('.').pop() !== filename;
}
}

Loading…
Cancel
Save