diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f35d016..e8cc2919 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## 1.180.0 - 18.08.2022 +## 1.180.1 - 18.08.2022 ### Added diff --git a/apps/api/src/app/frontend.middleware.ts b/apps/api/src/app/frontend.middleware.ts index 6064f0f8..c45a3574 100644 --- a/apps/api/src/app/frontend.middleware.ts +++ b/apps/api/src/app/frontend.middleware.ts @@ -1,39 +1,57 @@ +import * as fs from 'fs'; import * as path from 'path'; +import { ConfigurationService } from '@ghostfolio/api/services/configuration.service'; import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config'; -import { Injectable, Logger, NestMiddleware } from '@nestjs/common'; +import { Injectable, NestMiddleware } from '@nestjs/common'; import { NextFunction, Request, Response } from 'express'; @Injectable() export class FrontendMiddleware implements NestMiddleware { - use(req: Request, res: Response, next: NextFunction) { - if (req.url.includes('cover.png')) { - Logger.log(`Referer: ${req.headers.referer}`, 'FrontendMiddleware'); + public indexHtmlDe = fs.readFileSync( + this.getPathOfIndexHtmlFile('de'), + 'utf8' + ); + public indexHtmlEn = fs.readFileSync( + this.getPathOfIndexHtmlFile(DEFAULT_LANGUAGE_CODE), + 'utf8' + ); - // Resolve feature graphic for blog post - if (req.headers.referer?.includes('500-stars-on-github')) { - res.sendFile( - path.join( - __dirname, - '..', - 'client', - 'assets', - 'images', - 'blog', - '500-stars-on-github.jpg' - ) - ); - } else { - // Skip - next(); - } - } else if (req.path.startsWith('/api/') || this.isFileRequest(req.url)) { + public constructor( + private readonly configurationService: ConfigurationService + ) {} + + public use(req: Request, res: Response, next: NextFunction) { + let featureGraphicPath = 'assets/cover.png'; + + if ( + req.path === '/en/blog/2022/08/500-stars-on-github' || + req.path === '/en/blog/2022/08/500-stars-on-github/' + ) { + featureGraphicPath = 'assets/images/blog/500-stars-on-github.jpg'; + } + + if (req.path.startsWith('/api/') || this.isFileRequest(req.url)) { // Skip next(); - } else if (req.path.startsWith('/de/')) { - res.sendFile(this.getPathOfIndexHtmlFile('de')); + } else if (req.path === '/de' || req.path.startsWith('/de/')) { + res.send( + this.interpolate(this.indexHtmlDe, { + featureGraphicPath, + languageCode: 'de', + path: req.path, + rootUrl: this.configurationService.get('ROOT_URL') + }) + ); } else { - res.sendFile(this.getPathOfIndexHtmlFile(DEFAULT_LANGUAGE_CODE)); + res.send( + this.interpolate(this.indexHtmlEn, { + featureGraphicPath, + languageCode: DEFAULT_LANGUAGE_CODE, + path: req.path, + rootUrl: this.configurationService.get('ROOT_URL') + }) + ); } } @@ -41,6 +59,16 @@ export class FrontendMiddleware implements NestMiddleware { return path.join(__dirname, '..', 'client', aLocale, 'index.html'); } + private interpolate(template: string, context: any) { + return template.replace(/[$]{([^}]+)}/g, (_, objectPath) => { + const properties = objectPath.split('.'); + return properties.reduce( + (previous, current) => previous?.[current], + context + ); + }); + } + private isFileRequest(filename: string) { if (filename === '/assets/LICENSE') { return true; diff --git a/apps/client/src/index.html b/apps/client/src/index.html index acbda874..27ec0328 100644 --- a/apps/client/src/index.html +++ b/apps/client/src/index.html @@ -1,5 +1,5 @@ - + Ghostfolio – Open Source Wealth Management Software @@ -19,7 +19,7 @@ name="twitter:description" content="Ghostfolio is a lightweight wealth management application for individuals to keep track of stocks, ETFs or cryptocurrencies" /> - + - - + +