mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
Build setting some configs. Configs used by server.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
module.exports = {
|
||||
"production": false,
|
||||
// The REST API Location.
|
||||
"rest": {
|
||||
// NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
|
||||
@@ -8,7 +7,7 @@ module.exports = {
|
||||
"address": "localhost",
|
||||
"port": 3000
|
||||
},
|
||||
// Path and Port in use for this Angular2 UI
|
||||
// Protocol, path and port in use for this Angular2 UI
|
||||
"ui": {
|
||||
"nameSpace": "/",
|
||||
"protocol": "http",
|
||||
@@ -17,11 +16,10 @@ module.exports = {
|
||||
},
|
||||
"cache": {
|
||||
// how long should objects be cached for by default
|
||||
"msToLive": 15 * 60 * 1000, //15 minutes
|
||||
"msToLive": 1 * 60 * 1000, // 1 minute
|
||||
},
|
||||
"universal": {
|
||||
//on the client: start with the state on the server
|
||||
"shouldRehydrate": true,
|
||||
// universal settings
|
||||
"preboot": true,
|
||||
"async": true
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ import { PageNotFoundComponent } from './pagenotfound/pagenotfound.component';
|
||||
imports: [
|
||||
RouterModule.forChild([
|
||||
{ path: '', redirectTo: '/home', pathMatch: 'full' },
|
||||
{ path: '**', pathMatch: 'full', component: PageNotFoundComponent},
|
||||
{ path: '**', pathMatch: 'full', component: PageNotFoundComponent },
|
||||
])
|
||||
],
|
||||
})
|
||||
|
@@ -6,8 +6,10 @@
|
||||
<div class="container-fluid">
|
||||
<p>{{ 'example.with.data' | translate:data }}</p>
|
||||
<p>{{ example }}</p>
|
||||
<h2 *ngIf="!env" style="color:green">development</h2>
|
||||
<h2 *ngIf="env" style="color:red">production</h2>
|
||||
<h2 [ngClass]="{ 'red': EnvConfig.production, 'green': !EnvConfig.production }">
|
||||
<span *ngIf="!EnvConfig.production">development</span>
|
||||
<span *ngIf="EnvConfig.production">production</span>
|
||||
</h2>
|
||||
<router-outlet></router-outlet>
|
||||
</div>
|
||||
</main>
|
||||
|
@@ -16,3 +16,11 @@
|
||||
.main-content {
|
||||
flex: 1 0 auto;
|
||||
}
|
||||
|
||||
h2.red {
|
||||
color: red;
|
||||
}
|
||||
|
||||
h2.green {
|
||||
color: green;
|
||||
}
|
@@ -21,7 +21,7 @@ import { HostWindowState } from "./shared/host-window.reducer";
|
||||
import { HostWindowResizeAction } from "./shared/host-window.actions";
|
||||
import { MockTranslateLoader } from "./shared/testing/mock-translate-loader";
|
||||
|
||||
import { GLOBAL_CONFIG, config } from '../config';
|
||||
import { GLOBAL_CONFIG, EnvConfig } from '../config';
|
||||
|
||||
let comp: AppComponent;
|
||||
let fixture: ComponentFixture<AppComponent>;
|
||||
@@ -39,7 +39,7 @@ describe('App component', () => {
|
||||
})],
|
||||
declarations: [AppComponent], // declare the test component
|
||||
providers: [
|
||||
{ provide: GLOBAL_CONFIG, useValue: config },
|
||||
{ provide: GLOBAL_CONFIG, useValue: EnvConfig },
|
||||
AppComponent
|
||||
],
|
||||
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
||||
|
@@ -30,14 +30,8 @@ export class AppComponent implements OnDestroy, OnInit {
|
||||
recipient: 'World'
|
||||
};
|
||||
|
||||
env: string = this.config.production;
|
||||
|
||||
styles = {
|
||||
color: 'red'
|
||||
};
|
||||
|
||||
constructor(
|
||||
@Inject(GLOBAL_CONFIG) private config: GlobalConfig,
|
||||
@Inject(GLOBAL_CONFIG) public EnvConfig: GlobalConfig,
|
||||
private translate: TranslateService,
|
||||
private store: Store<HostWindowState>
|
||||
) {
|
||||
@@ -45,8 +39,6 @@ export class AppComponent implements OnDestroy, OnInit {
|
||||
translate.setDefaultLang('en');
|
||||
// the lang to use, if the lang isn't available, it will use the current loader to get them
|
||||
translate.use('en');
|
||||
|
||||
console.log(this.config);
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
|
@@ -14,13 +14,13 @@ import { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
|
||||
@Injectable()
|
||||
export class CollectionDataEffects extends DataEffects<Collection> {
|
||||
constructor(
|
||||
@Inject(GLOBAL_CONFIG) config: GlobalConfig,
|
||||
@Inject(GLOBAL_CONFIG) EnvConfig: GlobalConfig,
|
||||
actions$: Actions,
|
||||
restApi: DSpaceRESTv2Service,
|
||||
cache: ObjectCacheService,
|
||||
dataService: CollectionDataService
|
||||
) {
|
||||
super(config, actions$, restApi, cache, dataService);
|
||||
super(EnvConfig, actions$, restApi, cache, dataService);
|
||||
}
|
||||
|
||||
protected getFindAllEndpoint(action: RequestCacheFindAllAction): string {
|
||||
|
@@ -14,13 +14,13 @@ import { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
|
||||
@Injectable()
|
||||
export class ItemDataEffects extends DataEffects<Item> {
|
||||
constructor(
|
||||
@Inject(GLOBAL_CONFIG) config: GlobalConfig,
|
||||
@Inject(GLOBAL_CONFIG) EnvConfig: GlobalConfig,
|
||||
actions$: Actions,
|
||||
restApi: DSpaceRESTv2Service,
|
||||
cache: ObjectCacheService,
|
||||
dataService: ItemDataService
|
||||
) {
|
||||
super(config, actions$, restApi, cache, dataService);
|
||||
super(EnvConfig, actions$, restApi, cache, dataService);
|
||||
}
|
||||
|
||||
protected getFindAllEndpoint(action: RequestCacheFindAllAction): string {
|
||||
|
@@ -10,7 +10,7 @@ import { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
|
||||
*/
|
||||
@Injectable()
|
||||
export class DSpaceRESTv2Service {
|
||||
constructor(private http: Http, @Inject(GLOBAL_CONFIG) private config: GlobalConfig) {
|
||||
constructor(private http: Http, @Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig) {
|
||||
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ export class DSpaceRESTv2Service {
|
||||
* An Observablse<string> containing the response from the server
|
||||
*/
|
||||
get(relativeURL: string, options?: RequestOptionsArgs): Observable<string> {
|
||||
return this.http.get(new RESTURLCombiner(this.config, relativeURL).toString(), options)
|
||||
return this.http.get(new RESTURLCombiner(this.EnvConfig, relativeURL).toString(), options)
|
||||
.map(res => res.json())
|
||||
.catch(err => {
|
||||
console.log('Error: ', err);
|
||||
|
@@ -9,7 +9,7 @@ import { GlobalConfig } from '../../../config';
|
||||
* TODO write tests once GlobalConfig becomes injectable
|
||||
*/
|
||||
export class RESTURLCombiner extends URLCombiner {
|
||||
constructor(config: GlobalConfig, ...parts: Array<string>) {
|
||||
super(config.rest.baseURL, config.rest.nameSpace, ...parts);
|
||||
constructor(EnvConfig: GlobalConfig, ...parts: Array<string>) {
|
||||
super(EnvConfig.rest.baseUrl, EnvConfig.rest.nameSpace, ...parts);
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +1,9 @@
|
||||
<div class="page-not-found">
|
||||
<h1>404</h1>
|
||||
<h2><small>{{"404.page-not-found" | translate}}</small></h2>
|
||||
<br>
|
||||
<br/>
|
||||
<p>{{"404.help" | translate}}</p>
|
||||
<br>
|
||||
<br/>
|
||||
<p class="text-center">
|
||||
<a routerLink="/home" class="btn btn-primary">{{"404.link.home-page" | translate}}</a>
|
||||
</p>
|
||||
|
@@ -1,6 +1,5 @@
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'ds-pagenotfound',
|
||||
styleUrls: ['./pagenotfound.component.css'],
|
||||
|
@@ -1,48 +1,16 @@
|
||||
// Look in ./config folder for config
|
||||
import { OpaqueToken } from '@angular/core';
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
let configContext = require.context("../config", false, /js$/);
|
||||
|
||||
let EnvConfig: any = {};
|
||||
let EnvConfigFile: string;
|
||||
let DefaultConfig: any = {};
|
||||
|
||||
try {
|
||||
DefaultConfig = configContext('./environment.default.js');
|
||||
} catch (e) {
|
||||
throw new Error(`Cannot find file "${path.resolve('config', './environment.default.js')}"`);
|
||||
}
|
||||
|
||||
switch (process.env.NODE_ENV) {
|
||||
case 'prod':
|
||||
case 'production':
|
||||
EnvConfigFile = './environment.prod.js';
|
||||
break;
|
||||
case 'dev':
|
||||
case 'development':
|
||||
default:
|
||||
EnvConfigFile = './environment.dev.js';
|
||||
}
|
||||
try {
|
||||
EnvConfig = configContext(EnvConfigFile);
|
||||
} catch (e) {
|
||||
EnvConfig = {};
|
||||
}
|
||||
|
||||
const GLOBAL_CONFIG = new OpaqueToken('config');
|
||||
|
||||
interface ServerConfig {
|
||||
"nameSpace": string,
|
||||
"protocol": string,
|
||||
"address": string,
|
||||
"port": number,
|
||||
"baseURL": string
|
||||
"baseUrl": string
|
||||
}
|
||||
|
||||
interface GlobalConfig {
|
||||
"production": string,
|
||||
"production": boolean,
|
||||
"rest": ServerConfig,
|
||||
"ui": ServerConfig,
|
||||
"cache": {
|
||||
@@ -55,16 +23,55 @@ interface GlobalConfig {
|
||||
}
|
||||
}
|
||||
|
||||
const config: GlobalConfig = <GlobalConfig>Object.assign(DefaultConfig, EnvConfig);
|
||||
const GLOBAL_CONFIG = new OpaqueToken('config');
|
||||
|
||||
function buildURL(server: ServerConfig) {
|
||||
return [server.protocol, '://', server.address, (server.port !== 80) ? ':' + server.port : ''].join('');
|
||||
let configContext = require.context("../config", false, /js$/);
|
||||
|
||||
let EnvConfig: GlobalConfig;
|
||||
let EnvConfigFile: string;
|
||||
|
||||
let production: boolean = false;
|
||||
|
||||
switch (process.env.NODE_ENV) {
|
||||
case 'prod':
|
||||
case 'production':
|
||||
production = true;
|
||||
EnvConfigFile = './environment.prod.js';
|
||||
break;
|
||||
case 'dev':
|
||||
case 'development':
|
||||
EnvConfigFile = './environment.dev.js';
|
||||
break;
|
||||
case 'test':
|
||||
EnvConfigFile = './environment.test.js';
|
||||
break;
|
||||
default:
|
||||
console.warn('Environment file not specified. Using default.')
|
||||
}
|
||||
|
||||
for (let key in config) {
|
||||
if (config[key].protocol && config[key].address && config[key].port) {
|
||||
config[key].baseURL = buildURL(config[key]);
|
||||
try {
|
||||
EnvConfig = configContext('./environment.default.js');
|
||||
} catch (e) {
|
||||
throw new Error("Cannot find file ./environment.default.js");
|
||||
}
|
||||
|
||||
// if EnvConfigFile set try to get configs
|
||||
if(EnvConfigFile) {
|
||||
try {
|
||||
EnvConfig = <GlobalConfig> Object.assign(EnvConfig, configContext(EnvConfigFile));
|
||||
} catch (e) {
|
||||
console.warn("Cannot find file " + EnvConfigFile);
|
||||
}
|
||||
}
|
||||
|
||||
export { GLOBAL_CONFIG, GlobalConfig, config }
|
||||
// set base url if propery is object with protocol, address, and port. i.e. ServerConfig
|
||||
for (let key in EnvConfig) {
|
||||
if (EnvConfig[key].protocol && EnvConfig[key].address && EnvConfig[key].port) {
|
||||
EnvConfig[key].baseUrl = [EnvConfig[key].protocol, '://', EnvConfig[key].address, (EnvConfig[key].port !== 80) ? ':' + EnvConfig[key].port : ''].join('');
|
||||
}
|
||||
}
|
||||
|
||||
// set config for running in production
|
||||
EnvConfig.production = production;
|
||||
|
||||
export { GLOBAL_CONFIG, GlobalConfig, EnvConfig }
|
||||
|
@@ -23,7 +23,7 @@ import { effects } from '../../app/app.effects';
|
||||
import { Meta } from '../angular2-meta';
|
||||
import { RehydrateStoreAction } from "../../app/store.actions";
|
||||
|
||||
import { GLOBAL_CONFIG, config } from '../../config';
|
||||
import { GLOBAL_CONFIG, EnvConfig } from '../../config';
|
||||
|
||||
// import * as LRU from 'modern-lru';
|
||||
|
||||
@@ -72,7 +72,8 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
||||
effects
|
||||
],
|
||||
providers: [
|
||||
{ provide: GLOBAL_CONFIG, useValue: config },
|
||||
{ provide: GLOBAL_CONFIG, useValue: EnvConfig },
|
||||
|
||||
{ provide: 'isBrowser', useValue: isBrowser },
|
||||
{ provide: 'isNode', useValue: isNode },
|
||||
|
||||
|
@@ -21,7 +21,7 @@ import { effects } from '../../app/app.effects';
|
||||
// see https://github.com/angular/angular/pull/12322
|
||||
import { Meta } from '../angular2-meta';
|
||||
|
||||
import { GLOBAL_CONFIG, config } from '../../config';
|
||||
import { GLOBAL_CONFIG, EnvConfig } from '../../config';
|
||||
|
||||
export function createTranslateLoader(http: Http) {
|
||||
return new TranslateStaticLoader(http, './assets/i18n', '.json');
|
||||
@@ -63,8 +63,8 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
|
||||
effects
|
||||
],
|
||||
providers: [
|
||||
{ provide: GLOBAL_CONFIG, useValue: config },
|
||||
{ provide: 'res', useFactory: getResponse },
|
||||
{ provide: GLOBAL_CONFIG, useValue: EnvConfig },
|
||||
|
||||
{ provide: 'isBrowser', useValue: isBrowser },
|
||||
{ provide: 'isNode', useValue: isNode },
|
||||
|
||||
|
@@ -25,7 +25,7 @@ import { MainModuleNgFactory } from './platform/modules/node.module.ngfactory';
|
||||
// Routes
|
||||
import { routes } from './server.routes';
|
||||
|
||||
import { config } from './config';
|
||||
import { EnvConfig } from './config';
|
||||
|
||||
// enable prod for faster renders
|
||||
enableProdMode();
|
||||
@@ -44,8 +44,9 @@ app.engine('.html', createEngine({
|
||||
// stateless providers only since it's shared
|
||||
]
|
||||
}));
|
||||
app.set('port', process.env.PORT || 3000);
|
||||
app.set('address', process.env.ADDRESS || '127.0.0.1');
|
||||
|
||||
app.set('port', process.env.PORT || EnvConfig.ui.port || 3000);
|
||||
app.set('address', process.env.ADDRESS || EnvConfig.ui.address || '127.0.0.1');
|
||||
app.set('views', __dirname);
|
||||
app.set('view engine', 'html');
|
||||
app.set('json spaces', 2);
|
||||
@@ -66,13 +67,13 @@ function cacheControl(req, res, next) {
|
||||
res.header('Cache-Control', 'max-age=60');
|
||||
next();
|
||||
}
|
||||
|
||||
// Serve static files
|
||||
app.use('/assets', cacheControl, express.static(path.join(__dirname, 'assets'), { maxAge: 30 }));
|
||||
app.use('/styles', cacheControl, express.static(path.join(__dirname, 'styles'), { maxAge: 30 }));
|
||||
|
||||
app.use(cacheControl, express.static(path.join(ROOT, 'dist/client'), { index: false }));
|
||||
|
||||
//
|
||||
/////////////////////////
|
||||
// ** Example API
|
||||
// Notice API should be in a separate process
|
||||
@@ -95,11 +96,11 @@ function ngApp(req, res) {
|
||||
res,
|
||||
// use this to determine what part of your app is slow only in development
|
||||
// time: true,
|
||||
async: true,
|
||||
preboot: true,
|
||||
baseUrl: '/',
|
||||
async: EnvConfig.universal.async,
|
||||
preboot: EnvConfig.universal.preboot,
|
||||
baseUrl: EnvConfig.ui.nameSpace,
|
||||
requestUrl: req.originalUrl,
|
||||
originUrl: `http://${app.get('address')}:${app.get('port')}`
|
||||
originUrl: EnvConfig.ui.baseUrl
|
||||
});
|
||||
});
|
||||
|
||||
@@ -114,7 +115,6 @@ routes.forEach(route => {
|
||||
app.get(`/${route}/*`, ngApp);
|
||||
});
|
||||
|
||||
|
||||
app.get('*', function(req, res) {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
var pojo = { status: 404, message: 'No Content' };
|
||||
@@ -124,5 +124,5 @@ app.get('*', function(req, res) {
|
||||
|
||||
// Server
|
||||
let server = app.listen(app.get('port'), app.get('address'), () => {
|
||||
console.log(`Listening on: http://${server.address().address}:${server.address().port}`);
|
||||
console.log(`Listening on: ${EnvConfig.ui.protocol}://${server.address().address}:${server.address().port}`);
|
||||
});
|
||||
|
@@ -24,7 +24,7 @@ import { MainModule } from './platform/modules/node.module';
|
||||
// Routes
|
||||
import { routes } from './server.routes';
|
||||
|
||||
import { config } from './config';
|
||||
import { EnvConfig } from './config';
|
||||
|
||||
// enable prod for faster renders
|
||||
enableProdMode();
|
||||
@@ -42,8 +42,9 @@ app.engine('.html', createEngine({
|
||||
// stateless providers only since it's shared
|
||||
]
|
||||
}));
|
||||
app.set('port', process.env.PORT || 3000);
|
||||
app.set('address', process.env.ADDRESS || '127.0.0.1');
|
||||
|
||||
app.set('port', process.env.PORT || EnvConfig.ui.port || 3000);
|
||||
app.set('address', process.env.ADDRESS || EnvConfig.ui.address || '127.0.0.1');
|
||||
app.set('views', __dirname);
|
||||
app.set('view engine', 'html');
|
||||
app.set('json spaces', 2);
|
||||
@@ -59,13 +60,13 @@ function cacheControl(req, res, next) {
|
||||
res.header('Cache-Control', 'max-age=60');
|
||||
next();
|
||||
}
|
||||
|
||||
// Serve static files
|
||||
app.use('/assets', cacheControl, express.static(path.join(__dirname, 'assets'), { maxAge: 30 }));
|
||||
app.use('/styles', cacheControl, express.static(path.join(__dirname, 'styles'), { maxAge: 30 }));
|
||||
|
||||
app.use(cacheControl, express.static(path.join(ROOT, 'dist/client'), { index: false }));
|
||||
|
||||
//
|
||||
/////////////////////////
|
||||
// ** Example API
|
||||
// Notice API should be in aseparate process
|
||||
@@ -87,11 +88,11 @@ function ngApp(req, res) {
|
||||
req,
|
||||
res,
|
||||
// time: true, // use this to determine what part of your app is slow only in development
|
||||
async: true,
|
||||
preboot: true,
|
||||
baseUrl: '/',
|
||||
async: EnvConfig.universal.async,
|
||||
preboot: EnvConfig.universal.preboot,
|
||||
baseUrl: EnvConfig.ui.nameSpace,
|
||||
requestUrl: req.originalUrl,
|
||||
originUrl: `http://${app.get('address')}:${app.get('port')}`
|
||||
originUrl: EnvConfig.ui.baseUrl
|
||||
});
|
||||
});
|
||||
|
||||
@@ -115,5 +116,5 @@ app.get('*', function(req, res) {
|
||||
|
||||
// Server
|
||||
let server = app.listen(app.get('port'), app.get('address'), () => {
|
||||
console.log(`Listening on: http://${server.address().address}:${server.address().port}`);
|
||||
console.log(`Listening on: ${EnvConfig.ui.protocol}://${server.address().address}:${server.address().port}`);
|
||||
});
|
||||
|
Reference in New Issue
Block a user