2022-09-23 13:25:38 +02:00
|
|
|
import 'zone.js/node';
|
2018-01-26 16:00:07 +01:00
|
|
|
|
2021-07-14 16:51:20 +02:00
|
|
|
import {ngExpressEngine} from '@nguniversal/express-engine';
|
2018-01-26 16:00:07 +01:00
|
|
|
import * as express from 'express';
|
2021-07-14 16:51:20 +02:00
|
|
|
import * as compression from 'compression';
|
|
|
|
import {join} from 'path';
|
2018-01-26 16:00:07 +01:00
|
|
|
|
2021-07-14 16:51:20 +02:00
|
|
|
import {AppServerModule} from './src/main.server';
|
|
|
|
import {APP_BASE_HREF} from '@angular/common';
|
|
|
|
import {existsSync} from 'fs';
|
|
|
|
import {REQUEST, RESPONSE} from "./src/app/openaireLibrary/utils/tokens";
|
2019-11-14 11:12:48 +01:00
|
|
|
import {isArray} from "util";
|
2022-07-21 14:01:31 +02:00
|
|
|
import {properties} from "./src/environments/environment";
|
2023-06-19 10:24:13 +02:00
|
|
|
import {CustomizationOptions, Layout} from "./src/app/openaireLibrary/connect/community/CustomizationOptions";
|
2022-07-25 10:52:56 +02:00
|
|
|
import {Response} from "express";
|
2022-07-21 14:01:31 +02:00
|
|
|
|
|
|
|
const fs = require('fs');
|
2022-09-27 13:16:03 +02:00
|
|
|
const less = require('less/index');
|
2022-07-21 14:01:31 +02:00
|
|
|
const minify = require('@node-minify/core');
|
|
|
|
const cleanCSS = require('@node-minify/clean-css');
|
|
|
|
const axios = require('axios');
|
|
|
|
|
|
|
|
const browser = process.cwd() + '/dist/connect/browser/';
|
|
|
|
const node_modules = process.cwd() + '/node_modules';
|
2022-07-22 17:33:54 +02:00
|
|
|
var bodyParser = require('body-parser')
|
2022-07-21 14:01:31 +02:00
|
|
|
|
2022-07-22 17:33:54 +02:00
|
|
|
var jsonParser = bodyParser.json()
|
|
|
|
|
2022-07-25 10:52:56 +02:00
|
|
|
function buildCss(portal: string, suffix = null, variables: {} = null) {
|
2022-07-21 14:01:31 +02:00
|
|
|
let lessFile = 'community.less'
|
2022-07-25 10:52:56 +02:00
|
|
|
if (portal === 'connect') {
|
2022-07-21 14:01:31 +02:00
|
|
|
lessFile = 'connect.less'
|
|
|
|
}
|
|
|
|
let input = fs.readFileSync(browser + '/assets/' + lessFile, 'utf8');
|
|
|
|
/* Change fonts path */
|
|
|
|
let modifyVars = {
|
|
|
|
'@font-media-url': 'e("assets/openaire-theme/media/fonts/aileron/")',
|
|
|
|
'@font-media-icon-url': 'e("assets/openaire-theme/media/fonts/material-icons/")'
|
|
|
|
}
|
|
|
|
/* Change variables (optional)*/
|
2022-07-25 10:52:56 +02:00
|
|
|
if (variables) {
|
2022-07-21 14:01:31 +02:00
|
|
|
Object.entries(variables).forEach(([key, value]) => {
|
|
|
|
modifyVars[key] = value;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let options = {
|
|
|
|
paths: [browser + '/assets/', node_modules],
|
|
|
|
rewriteUrls: 'all',
|
|
|
|
modifyVars: modifyVars
|
|
|
|
};
|
|
|
|
less.render(input, options, function (error, result) {
|
2022-07-25 10:52:56 +02:00
|
|
|
if (error) {
|
2022-07-21 14:01:31 +02:00
|
|
|
console.log(error);
|
|
|
|
} else {
|
2022-07-25 10:52:56 +02:00
|
|
|
let file = browser + portal + (suffix ? ("-" + suffix) : "") + '.css';
|
|
|
|
fs.writeFile(file, result.css, function (error) {
|
|
|
|
if (error) {
|
2022-07-21 14:01:31 +02:00
|
|
|
console.error(error);
|
|
|
|
} else {
|
|
|
|
minify({
|
|
|
|
compressor: cleanCSS,
|
|
|
|
replaceInPlace: true,
|
2022-07-25 10:52:56 +02:00
|
|
|
input: file,
|
|
|
|
output: file,
|
|
|
|
callback: function (err) {
|
|
|
|
if (err) {
|
2022-07-21 14:01:31 +02:00
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-07-25 10:52:56 +02:00
|
|
|
|
|
|
|
function buildAll(res: Response = null) {
|
2022-07-22 17:33:54 +02:00
|
|
|
let layoutsURL = properties.adminToolsAPIURL + '/community/layouts';
|
|
|
|
axios.get(layoutsURL).then(response => {
|
2022-07-25 10:52:56 +02:00
|
|
|
if (response.data && Array.isArray(response.data) && response.data.length > 0) {
|
2022-07-22 17:33:54 +02:00
|
|
|
response.data.forEach((layout: Layout) => {
|
2023-06-19 10:24:13 +02:00
|
|
|
let variables = Layout.getVariables(CustomizationOptions.checkForObsoleteVersion(layout.layoutOptions, layout.portalPid));
|
2022-08-12 15:16:03 +02:00
|
|
|
buildCss(layout.portalPid, layout.date ? layout.date : null, variables);
|
2022-07-22 17:33:54 +02:00
|
|
|
});
|
2022-07-25 10:52:56 +02:00
|
|
|
if (res) {
|
|
|
|
res.status(200).send({
|
|
|
|
code: 200,
|
|
|
|
message: 'CSS build for all available layouts was successful'
|
|
|
|
});
|
|
|
|
}
|
2022-07-22 17:33:54 +02:00
|
|
|
} else {
|
2022-07-25 10:52:56 +02:00
|
|
|
res.status(500).send({code: 500, message: 'No available layouts found'});
|
2022-07-22 17:33:54 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-08-08 12:14:51 +02:00
|
|
|
if(properties.environment == 'development') {
|
|
|
|
buildAll();
|
|
|
|
}
|
2018-01-26 16:00:07 +01:00
|
|
|
|
2021-07-14 16:51:20 +02:00
|
|
|
// The Express app is exported so that it can be used by serverless Functions.
|
|
|
|
export function app() {
|
|
|
|
const server = express();
|
|
|
|
server.use(compression());
|
|
|
|
const distFolder = join(process.cwd(), 'dist/connect/browser');
|
|
|
|
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
|
|
|
|
|
|
|
|
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
|
|
|
|
server.engine('html', ngExpressEngine({
|
|
|
|
bootstrap: AppServerModule,
|
2022-10-03 11:34:37 +02:00
|
|
|
inlineCriticalCss: false
|
2021-07-14 16:51:20 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
server.set('view engine', 'html');
|
|
|
|
server.set('views', distFolder);
|
|
|
|
|
2022-07-21 17:07:07 +02:00
|
|
|
server.use('/build-css', function (req, res, next) {
|
|
|
|
res.header('Access-Control-Allow-Origin', req.headers.origin);
|
|
|
|
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST, DELETE');
|
|
|
|
res.header('Access-Control-Max-Age', "1800");
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2021-07-14 16:51:20 +02:00
|
|
|
server.use(function (req, res, next) {
|
2022-07-25 10:52:56 +02:00
|
|
|
var XFRAME_WHITELIST = ['http://spitoo.di.uoa.gr:5000/', 'http://scoobydoo.di.uoa.gr:5000/', 'https://beta.admin.connect.openaire.eu/', 'https://admin.connect.openaire.eu/'];
|
2021-07-14 16:51:20 +02:00
|
|
|
let referer: string;
|
2022-07-25 10:52:56 +02:00
|
|
|
if (req.headers.referer) {
|
|
|
|
referer = isArray(req.headers.referer) ? req.headers.referer[0] : (<string>req.headers.referer);
|
2021-07-14 16:51:20 +02:00
|
|
|
referer = referer.split("?")[0];
|
|
|
|
}
|
2024-04-19 11:40:17 +02:00
|
|
|
if (referer && (XFRAME_WHITELIST.indexOf(referer) != -1 || referer.indexOf("/customize-layout") != -1 || referer.indexOf(".d4science.org") != -1 || referer.indexOf(".di.uoa.gr") != -1 || referer.indexOf(".openaire.eu") != -1 )) {
|
|
|
|
res.header('X-FRAME-OPTIONS', 'allow from ' +req.headers.referer);
|
|
|
|
res.header('Access-Control-Allow-Origin',req.headers.origin);
|
|
|
|
res.header('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS');
|
2024-06-10 08:09:48 +02:00
|
|
|
res.header('Access-Control-Allow-Headers', 'Cache-control, Expires, Content-Type, Pragma');
|
2024-04-19 11:40:17 +02:00
|
|
|
res.header('Allow', 'GET, HEAD, OPTIONS');
|
|
|
|
|
2022-07-25 10:52:56 +02:00
|
|
|
} else {
|
2021-07-14 16:51:20 +02:00
|
|
|
res.header('X-FRAME-OPTIONS', 'SAMEORIGIN');
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// - Serve sitemap based on the host -
|
|
|
|
server.get('/sitemap.xml', (req, res) => {
|
2020-08-10 17:32:33 +02:00
|
|
|
let host = req.get("host").split(":")[0];
|
2022-07-25 10:52:56 +02:00
|
|
|
let connectLinks = ["/", "/about/learn-how", "/about/learn-in-depth", "/about/faq", "/search/find/communities"];
|
|
|
|
let communityLinks = ["/", "/search/find/research-outcomes?size=20", "/search/advanced/research-outcomes?size=20", "/participate/deposit/learn-how", "/organizations", "/content"];
|
2020-08-10 17:32:33 +02:00
|
|
|
let sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
|
|
|
|
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";
|
|
|
|
let urlPre = "<url>\n" +
|
2020-11-30 18:04:05 +01:00
|
|
|
" <loc>";
|
|
|
|
let urlSuf = "</loc>\n" +
|
2020-08-10 17:32:33 +02:00
|
|
|
" </url>";
|
2022-07-25 10:52:56 +02:00
|
|
|
for (let link of (host.indexOf("connect.openaire.eu") == -1 ? communityLinks : connectLinks)) {
|
2020-08-10 17:32:33 +02:00
|
|
|
sitemap += urlPre + "https://" + host + link + urlSuf;
|
2021-07-14 16:51:20 +02:00
|
|
|
}
|
2020-08-10 17:32:33 +02:00
|
|
|
sitemap += "\n</urlset>";
|
|
|
|
res.setHeader('content-type', 'application/xml');
|
2022-07-25 10:52:56 +02:00
|
|
|
res.send(sitemap);
|
2020-08-10 17:32:33 +02:00
|
|
|
});
|
2021-07-14 16:51:20 +02:00
|
|
|
|
|
|
|
// - Serve robots based on the host -
|
|
|
|
server.get('/robots.txt', (req, res) => {
|
|
|
|
let host = req.get("host").split(":")[0];
|
|
|
|
let robots = "";
|
|
|
|
if (host.indexOf(".openaire.eu") != -1 && host.indexOf("beta.") == -1) {
|
|
|
|
//link to disallow
|
|
|
|
let connectLinks = ["/about/*", "/search/find/communities"];
|
|
|
|
let communityLinks = ["/search/advanced/*", "/participate/*",
|
|
|
|
"/search/project", '/search/result', '/search/publication', '/search/dataset', '/search/software', '/search/other', '/search/dataprovider', '/search/organization', '/project-report',
|
|
|
|
'/search/find/publications', '/search/find/datasets', '/search/find/software', '/search/find/other', '/search/find/projects', '/search/find/dataproviders', '/search/find/research-outcomes'
|
|
|
|
];
|
|
|
|
robots = "User-Agent: *\n" +
|
|
|
|
"Crawl-delay: 30\n" +
|
|
|
|
"Sitemap: /sitemap.xml\n";
|
|
|
|
for (let link of (host.indexOf("connect.openaire.eu") == -1 ? connectLinks : communityLinks)) {
|
|
|
|
robots += "Disallow: " + link + "\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
robots = "User-Agent: *\n" +
|
|
|
|
"Disallow: /\n"
|
2020-12-02 18:41:53 +01:00
|
|
|
}
|
2021-07-14 16:51:20 +02:00
|
|
|
res.setHeader('content-type', 'text/plain');
|
|
|
|
res.send(robots);
|
|
|
|
});
|
|
|
|
|
2022-07-21 14:01:31 +02:00
|
|
|
server.post('/build-css/all', (req, res) => {
|
2022-07-25 10:52:56 +02:00
|
|
|
buildAll(res);
|
2022-07-21 14:01:31 +02:00
|
|
|
});
|
|
|
|
|
2022-08-12 15:16:03 +02:00
|
|
|
server.post('/build-css/:id/:suffix', jsonParser,(req, res) => {
|
2023-06-19 10:24:13 +02:00
|
|
|
let variables = Layout.getVariables(CustomizationOptions.checkForObsoleteVersion(req.body, req.params.id));
|
2022-08-12 15:16:03 +02:00
|
|
|
buildCss(req.params.id , req.params.suffix, variables);
|
2022-07-25 10:52:56 +02:00
|
|
|
res.status(200).send({
|
|
|
|
code: 200,
|
2022-08-12 15:16:03 +02:00
|
|
|
message: 'CSS build for ' + req.params.id + ' layout was successful'
|
2022-07-25 10:52:56 +02:00
|
|
|
});
|
2022-07-21 14:01:31 +02:00
|
|
|
});
|
2022-07-25 10:52:56 +02:00
|
|
|
|
2022-07-22 17:33:54 +02:00
|
|
|
server.post('/build-css/preview/:id/:suffix', jsonParser, (req, res) => {
|
2023-06-19 10:24:13 +02:00
|
|
|
let variables = Layout.getVariables(CustomizationOptions.checkForObsoleteVersion(req.body, req.params.id));
|
2022-07-25 10:52:56 +02:00
|
|
|
buildCss(req.params.id, req.params.suffix, variables);
|
|
|
|
res.status(200).send({code: 200, message: 'CSS build for ' + req.params.id + ' layout was successful'});
|
2022-07-22 17:33:54 +02:00
|
|
|
});
|
2024-02-29 08:35:33 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2021-07-14 16:51:20 +02:00
|
|
|
// Example Express Rest API endpoints
|
|
|
|
// server.get('/api/**', (req, res) => { });
|
|
|
|
// Serve static files from /browser
|
|
|
|
server.get('*.*', express.static(distFolder, {
|
|
|
|
maxAge: '1y'
|
|
|
|
}));
|
|
|
|
|
|
|
|
// All regular routes use the Universal engine
|
|
|
|
server.get('*', (req, res) => {
|
|
|
|
res.render(indexHtml, {
|
|
|
|
req, providers: [
|
|
|
|
{
|
|
|
|
provide: APP_BASE_HREF,
|
|
|
|
useValue: req.baseUrl
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: REQUEST, useValue: (req)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
provide: RESPONSE, useValue: (res)
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
const port = process.env.PORT || 4000;
|
|
|
|
|
|
|
|
// Start up the Node server
|
|
|
|
const server = app();
|
|
|
|
server.listen(port, () => {
|
|
|
|
console.log(`Node Express server listening on http://localhost:${port}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Webpack will replace 'require' with '__webpack_require__'
|
|
|
|
// '__non_webpack_require__' is a proxy to Node 'require'
|
|
|
|
// The below code is to ensure that the server is run only when not requiring the bundle.
|
|
|
|
declare const __non_webpack_require__: NodeRequire;
|
|
|
|
const mainModule = __non_webpack_require__.main;
|
|
|
|
const moduleFilename = mainModule && mainModule.filename || '';
|
|
|
|
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
|
|
|
|
run();
|
|
|
|
}
|
|
|
|
|
|
|
|
export * from './src/main.server';
|