mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
fixed ui environment vars issue
This commit is contained in:
@@ -17,7 +17,7 @@
|
|||||||
"pree2e": "yarn run config:prod",
|
"pree2e": "yarn run config:prod",
|
||||||
"pree2e:ci": "yarn run config:prod",
|
"pree2e:ci": "yarn run config:prod",
|
||||||
"start": "yarn run start:prod",
|
"start": "yarn run start:prod",
|
||||||
"serve": "ng serve",
|
"serve": "ts-node --project ./tsconfig.ts-node.json scripts/serve.ts",
|
||||||
"start:dev": "npm-run-all --parallel config:dev:watch serve",
|
"start:dev": "npm-run-all --parallel config:dev:watch serve",
|
||||||
"start:prod": "yarn run build:prod && yarn run serve:ssr",
|
"start:prod": "yarn run build:prod && yarn run serve:ssr",
|
||||||
"build": "ng build",
|
"build": "ng build",
|
||||||
|
8
scripts/serve.ts
Normal file
8
scripts/serve.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { environment } from '../src/environments/environment';
|
||||||
|
|
||||||
|
import * as child from 'child_process';
|
||||||
|
|
||||||
|
child.spawn(
|
||||||
|
`ng serve --host ${environment.ui.host} --port ${environment.ui.port} --servePath ${environment.ui.nameSpace} --ssl ${environment.ui.ssl}`,
|
||||||
|
{ stdio:'inherit', shell: true }
|
||||||
|
);
|
164
server.ts
164
server.ts
@@ -1,45 +1,39 @@
|
|||||||
/**
|
|
||||||
* *** NOTE ON IMPORTING FROM ANGULAR AND NGUNIVERSAL IN THIS FILE ***
|
|
||||||
*
|
|
||||||
* If your application uses third-party dependencies, you'll need to
|
|
||||||
* either use Webpack or the Angular CLI's `bundleDependencies` feature
|
|
||||||
* in order to adequately package them for use on the server without a
|
|
||||||
* node_modules directory.
|
|
||||||
*
|
|
||||||
* However, due to the nature of the CLI's `bundleDependencies`, importing
|
|
||||||
* Angular in this file will create a different instance of Angular than
|
|
||||||
* the version in the compiled application code. This leads to unavoidable
|
|
||||||
* conflicts. Therefore, please do not explicitly import from @angular or
|
|
||||||
* @nguniversal in this file. You can export any needed resources
|
|
||||||
* from your application's main.server.ts file, as seen below with the
|
|
||||||
* import for `ngExpressEngine`.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import 'zone.js/dist/zone-node';
|
import 'zone.js/dist/zone-node';
|
||||||
import 'reflect-metadata';
|
import 'reflect-metadata';
|
||||||
|
import 'rxjs';
|
||||||
|
|
||||||
|
import * as fs from 'fs';
|
||||||
|
import * as pem from 'pem';
|
||||||
|
import * as https from 'https';
|
||||||
|
import * as morgan from 'morgan';
|
||||||
import * as express from 'express';
|
import * as express from 'express';
|
||||||
import { join } from 'path';
|
|
||||||
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
|
|
||||||
import * as bodyParser from 'body-parser';
|
import * as bodyParser from 'body-parser';
|
||||||
|
import * as compression from 'compression';
|
||||||
import * as cookieParser from 'cookie-parser';
|
import * as cookieParser from 'cookie-parser';
|
||||||
|
|
||||||
|
import { enableProdMode, NgModuleFactory, Type } from '@angular/core';
|
||||||
|
|
||||||
|
import { ngExpressEngine } from '@nguniversal/express-engine';
|
||||||
|
|
||||||
|
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
|
||||||
import { environment } from './src/environments/environment';
|
import { environment } from './src/environments/environment';
|
||||||
|
|
||||||
// Express server
|
export function startServer(bootstrap: Type<{}> | NgModuleFactory<{}>) {
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
const PORT = environment.ui.port || 4000;
|
if (environment.production) {
|
||||||
const DIST_FOLDER = join(process.cwd(), 'dist/browser');
|
enableProdMode();
|
||||||
|
app.use(compression());
|
||||||
|
}
|
||||||
|
|
||||||
// * NOTE :: leave this as require() since this file is built Dynamically from webpack
|
app.use(morgan('dev'));
|
||||||
const { ServerAppModuleNgFactory, LAZY_MODULE_MAP, ngExpressEngine, provideModuleMap } = require('./dist/server/main');
|
|
||||||
|
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
app.engine('html', (_, options, callback) =>
|
app.engine('html', (_, options, callback) =>
|
||||||
ngExpressEngine({
|
ngExpressEngine({
|
||||||
bootstrap: ServerAppModuleNgFactory,
|
bootstrap: bootstrap,
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: REQUEST,
|
provide: REQUEST,
|
||||||
@@ -49,27 +43,115 @@ app.engine('html', (_, options, callback) =>
|
|||||||
provide: RESPONSE,
|
provide: RESPONSE,
|
||||||
useValue: (options as any).req.res,
|
useValue: (options as any).req.res,
|
||||||
},
|
},
|
||||||
provideModuleMap(LAZY_MODULE_MAP)
|
|
||||||
],
|
],
|
||||||
})(_, options, callback)
|
})(_, (options as any), callback)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
app.set('view engine', 'ejs');
|
||||||
app.set('view engine', 'html');
|
app.set('view engine', 'html');
|
||||||
app.set('views', DIST_FOLDER);
|
app.set('views', 'src');
|
||||||
|
|
||||||
// Example Express Rest API endpoints
|
function cacheControl(req, res, next) {
|
||||||
// app.get('/api/**', (req, res) => { });
|
// instruct browser to revalidate
|
||||||
// Serve static files from /browser
|
res.header('Cache-Control', environment.cache.control || 'max-age=60');
|
||||||
app.get('*.*', express.static(DIST_FOLDER, {
|
next();
|
||||||
maxAge: '1y'
|
}
|
||||||
}));
|
|
||||||
|
|
||||||
// All regular routes use the Universal engine
|
app.use('/', cacheControl, express.static('dist', { index: false }));
|
||||||
app.get('*', (req, res) => {
|
|
||||||
res.render('index', { req });
|
// TODO: either remove or update mock backend
|
||||||
|
// app.get('/data.json', serverApi);
|
||||||
|
// app.use('/api', createMockApi());
|
||||||
|
|
||||||
|
function ngApp(req, res) {
|
||||||
|
const dspace = {
|
||||||
|
originalRequest: {
|
||||||
|
headers: req.headers,
|
||||||
|
body: req.body,
|
||||||
|
method: req.method,
|
||||||
|
params: req.params,
|
||||||
|
reportProgress: req.reportProgress,
|
||||||
|
withCredentials: req.withCredentials,
|
||||||
|
responseType: req.responseType,
|
||||||
|
urlWithParams: req.urlWithParams
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function onHandleError(parentZoneDelegate, currentZone, targetZone, error) {
|
||||||
|
if (!res._headerSent) {
|
||||||
|
console.warn('Error in SSR, serving for direct CSR. Error details : ', error);
|
||||||
|
res.sendFile('index.csr.html', { root: './src' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (environment.universal.preboot) {
|
||||||
|
Zone.current.fork({ name: 'CSR fallback', onHandleError }).run(() => {
|
||||||
|
res.render('../dist/index.html', {
|
||||||
|
req,
|
||||||
|
res,
|
||||||
|
preboot: environment.universal.preboot,
|
||||||
|
async: environment.universal.async,
|
||||||
|
time: environment.universal.time,
|
||||||
|
baseUrl: environment.ui.nameSpace,
|
||||||
|
originUrl: environment.ui.baseUrl,
|
||||||
|
requestUrl: req.originalUrl
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start up the Node server
|
|
||||||
app.listen(PORT, () => {
|
|
||||||
console.log(`Node Express server listening on http://localhost:${PORT}`);
|
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
console.log('Universal off, serving for direct CSR');
|
||||||
|
res.render('index-csr.ejs', {
|
||||||
|
root: './src',
|
||||||
|
scripts: `<script>window.dspace = ${JSON.stringify(dspace)}</script>`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function serverStarted() {
|
||||||
|
console.log(`[${new Date().toTimeString()}] Listening at ${environment.ui.baseUrl}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createHttpsServer(keys) {
|
||||||
|
https.createServer({
|
||||||
|
key: keys.serviceKey,
|
||||||
|
cert: keys.certificate
|
||||||
|
}, app).listen(environment.ui.port, environment.ui.host, () => {
|
||||||
|
serverStarted();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (environment.ui.ssl) {
|
||||||
|
let serviceKey;
|
||||||
|
try {
|
||||||
|
serviceKey = fs.readFileSync('./config/ssl/key.pem');
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Service key not found at ./config/ssl/key.pem');
|
||||||
|
}
|
||||||
|
|
||||||
|
let certificate;
|
||||||
|
try {
|
||||||
|
certificate = fs.readFileSync('./config/ssl/cert.pem');
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Certificate not found at ./config/ssl/key.pem');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (serviceKey && certificate) {
|
||||||
|
createHttpsServer({
|
||||||
|
serviceKey: serviceKey,
|
||||||
|
certificate: certificate
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
|
||||||
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||||
|
|
||||||
|
pem.createCertificate({
|
||||||
|
days: 1,
|
||||||
|
selfSigned: true
|
||||||
|
}, (error, keys) => {
|
||||||
|
createHttpsServer(keys);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
app.listen(environment.ui.port, environment.ui.host, () => {
|
||||||
|
serverStarted();
|
||||||
|
});
|
||||||
|
}}
|
||||||
|
@@ -1,15 +0,0 @@
|
|||||||
export const ROUTES: string[] = [
|
|
||||||
'home',
|
|
||||||
'items/:id',
|
|
||||||
'login',
|
|
||||||
'logout',
|
|
||||||
'collections/:id',
|
|
||||||
'communities/:id',
|
|
||||||
'login',
|
|
||||||
'logout',
|
|
||||||
'search',
|
|
||||||
'submit',
|
|
||||||
'workspaceitems/:id/edit',
|
|
||||||
'workflowitems/:id/edit',
|
|
||||||
'**'
|
|
||||||
];
|
|
Reference in New Issue
Block a user