[angular-18 | DONE | FIXED]: Updated libraries | app.module.ts: Replaced provideHttpClient(withInterceptorsFromDi()) with provideHttpClient(withFetch()) | package.json: Changed version of "@types/node" from "^16.18.50" to "18.19.1" | server.ts: Added response codes in server.ts & missing functions for metrics and health-check & fixed configurations.

This commit is contained in:
Konstantina Galouni 2024-09-23 22:10:05 +03:00
parent 0ec14135bc
commit fb508e53e6
6 changed files with 79 additions and 16 deletions

View File

@ -1,6 +1,6 @@
# Aggregator
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.10 and has been updated to 11.2.14.
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.3.10 and has been updated to 18.1.0.
## Install packages

View File

@ -55,7 +55,7 @@
"@types/express": "^4.17.0",
"@types/jasmine": "~3.6.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^16.18.50",
"@types/node": "^18.19.1",
"browser-sync": "^3.0.0",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.8.0",

View File

@ -6,16 +6,23 @@ import { CommonEngine } from '@angular/ssr';
import * as express from 'express';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import bootstrap from './src/main.server';
import {AppServerModule} from './src/main.server';
import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens";
import * as compression from "compression";
import {Prometheus} from "./prometheus";
import {Counter} from "prom-client";
// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
const server = express();
server.use(compression());
const distFolder = join(process.cwd(), 'dist/aggregator/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html'))
? join(distFolder, 'index.original.html')
: join(distFolder, 'index.html');
const prometheus: Prometheus = new Prometheus();
const commonEngine = new CommonEngine();
server.set('view engine', 'html');
@ -28,21 +35,79 @@ export function app(): express.Express {
maxAge: '1y'
}));
server.get('/metrics', (req, res) => {
res.set('Content-Type', prometheus.register.contentType);
res.end(prometheus.register.metrics());
});
server.get('/health-check', async (_req, res, _next) => {
var uptime = process.uptime();
const date = new Date(uptime*1000);
const days = date.getUTCDate() - 1,
hours = date.getUTCHours(),
minutes = date.getUTCMinutes(),
seconds = date.getUTCSeconds(),
milliseconds = date.getUTCMilliseconds();
const healthcheck = {
uptime: days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds, " + milliseconds + " milliseconds",
message: 'OK',
timestamp: new Date()
};
try {
res.send(healthcheck);
} catch (error) {
healthcheck.message = error;
res.status(503).send();
}
});
// All regular routes use the Angular engine
server.get('*', (req, res, next) => {
const { protocol, originalUrl, baseUrl, headers } = req;
commonEngine
.render({
bootstrap,
let start = new Date();
let counter: Counter = prometheus.counters.get(req.path);
if(counter !== undefined) {
counter.inc(1, new Date());
commonEngine.render({
bootstrap: AppServerModule,
inlineCriticalCss: false,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
providers: [
{ provide: APP_BASE_HREF, useValue: baseUrl },],
{provide: APP_BASE_HREF, useValue: baseUrl},
{provide: REQUEST, useValue: (req)},
{provide: RESPONSE, useValue: (res)}
],
})
.then((html) => res.send(html))
.catch((err) => next(err));
.then((html) => {
// event triggers when express is done sending response
res.on('finish', function() {
console.log(new Date().getTime() - start.getTime());
});
res.status(200).send(html)
})
.catch((err) => res.status(500).send('Error during SSR'))
} else {
commonEngine.render({
bootstrap: AppServerModule,
inlineCriticalCss: false,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: distFolder,
providers: [
{provide: APP_BASE_HREF, useValue: baseUrl},
{provide: REQUEST, useValue: (req)},
{provide: RESPONSE, useValue: (res)}
],
})
.then((html) => res.status(200).send(html))
.catch((err) => res.status(500).send('Error during SSR'))
}
});
return server;
@ -67,5 +132,3 @@ const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export default bootstrap;

View File

@ -3,7 +3,7 @@ import {SharedModule} from './shared/shared.module';
import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {CommonModule} from '@angular/common';
import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { HTTP_INTERCEPTORS, provideHttpClient, withFetch } from '@angular/common/http';
import {AppComponent} from './app.component';
import {NavigationBarModule} from './openaireLibrary/sharedComponents/navigationBar.module';
import {CookieLawModule} from './openaireLibrary/sharedComponents/cookie-law/cookie-law.module';
@ -42,7 +42,7 @@ import {ConfigurationService} from "./openaireLibrary/utils/configuration/config
},
[{ provide: HTTP_INTERCEPTORS, useClass: TimeoutInterceptor, multi: true }],
[{ provide: DEFAULT_TIMEOUT, useValue: 30000 }],
provideHttpClient(withInterceptorsFromDi())
provideHttpClient(withFetch())
] })
export class AppModule {
}

@ -1 +1 @@
Subproject commit 5f691d18c4ed14f23c35cbdb9746d9d5cd71b221
Subproject commit 6e7f450cf2dd7a15b5d040822939189f6f667097

@ -1 +1 @@
Subproject commit eb0051dc347cdaa44476048dea9f6b61200f257f
Subproject commit a88e24accc23599b43577994011e424eb693e817