diff --git a/config/environment.default.js b/config/environment.default.js index 5391908e52..53ad5e9c0f 100644 --- a/config/environment.default.js +++ b/config/environment.default.js @@ -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 } diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 1186af1993..5099b3899d 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -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 }, ]) ], }) diff --git a/src/app/app.component.html b/src/app/app.component.html index 3fb7748dd6..87d3703586 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -6,8 +6,10 @@

{{ 'example.with.data' | translate:data }}

{{ example }}

-

development

-

production

+

+ development + production +

diff --git a/src/app/app.component.scss b/src/app/app.component.scss index 5a9b8df543..4222d4b9e6 100644 --- a/src/app/app.component.scss +++ b/src/app/app.component.scss @@ -16,3 +16,11 @@ .main-content { flex: 1 0 auto; } + +h2.red { + color: red; +} + +h2.green { + color: green; +} \ No newline at end of file diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index 4541363548..d56be1a807 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -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; @@ -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] diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 993fdddd4c..b27fdd354b 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -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 ) { @@ -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() { diff --git a/src/app/core/data-services/collection-data.effects.ts b/src/app/core/data-services/collection-data.effects.ts index e1a294ea92..9586940def 100644 --- a/src/app/core/data-services/collection-data.effects.ts +++ b/src/app/core/data-services/collection-data.effects.ts @@ -14,13 +14,13 @@ import { GLOBAL_CONFIG, GlobalConfig } from '../../../config'; @Injectable() export class CollectionDataEffects extends DataEffects { 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 { diff --git a/src/app/core/data-services/item-data.effects.ts b/src/app/core/data-services/item-data.effects.ts index 12b57a375d..8c140c8398 100644 --- a/src/app/core/data-services/item-data.effects.ts +++ b/src/app/core/data-services/item-data.effects.ts @@ -14,13 +14,13 @@ import { GLOBAL_CONFIG, GlobalConfig } from '../../../config'; @Injectable() export class ItemDataEffects extends DataEffects { 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 { diff --git a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts index c9fc60d4cc..d8a21c6c9d 100644 --- a/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts +++ b/src/app/core/dspace-rest-v2/dspace-rest-v2.service.ts @@ -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 containing the response from the server */ get(relativeURL: string, options?: RequestOptionsArgs): Observable { - 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); diff --git a/src/app/core/url-combiner/rest-url-combiner.ts b/src/app/core/url-combiner/rest-url-combiner.ts index 16775af6b6..afbd25020a 100644 --- a/src/app/core/url-combiner/rest-url-combiner.ts +++ b/src/app/core/url-combiner/rest-url-combiner.ts @@ -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) { - super(config.rest.baseURL, config.rest.nameSpace, ...parts); + constructor(EnvConfig: GlobalConfig, ...parts: Array) { + super(EnvConfig.rest.baseUrl, EnvConfig.rest.nameSpace, ...parts); } } diff --git a/src/app/pagenotfound/pagenotfound.component.html b/src/app/pagenotfound/pagenotfound.component.html index cf6047e6df..fce500f376 100644 --- a/src/app/pagenotfound/pagenotfound.component.html +++ b/src/app/pagenotfound/pagenotfound.component.html @@ -1,9 +1,9 @@

404

{{"404.page-not-found" | translate}}

-
+

{{"404.help" | translate}}

-
+

{{"404.link.home-page" | translate}}

diff --git a/src/app/pagenotfound/pagenotfound.component.ts b/src/app/pagenotfound/pagenotfound.component.ts index f0e78be45b..e8537bc6b7 100644 --- a/src/app/pagenotfound/pagenotfound.component.ts +++ b/src/app/pagenotfound/pagenotfound.component.ts @@ -1,6 +1,5 @@ import { Component } from '@angular/core'; - @Component({ selector: 'ds-pagenotfound', styleUrls: ['./pagenotfound.component.css'], diff --git a/src/config.ts b/src/config.ts index 4736005cb5..78f6d11446 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 = 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 = 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 } diff --git a/src/platform/modules/browser.module.ts b/src/platform/modules/browser.module.ts index 9245b9b299..9cb2178fd9 100755 --- a/src/platform/modules/browser.module.ts +++ b/src/platform/modules/browser.module.ts @@ -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 }, diff --git a/src/platform/modules/node.module.ts b/src/platform/modules/node.module.ts index 28d4b3c33e..7148855d4f 100755 --- a/src/platform/modules/node.module.ts +++ b/src/platform/modules/node.module.ts @@ -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 }, diff --git a/src/server.aot.ts b/src/server.aot.ts index 11c2143f52..c8aa6a4c9a 100644 --- a/src/server.aot.ts +++ b/src/server.aot.ts @@ -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}`); }); diff --git a/src/server.ts b/src/server.ts index 94f84259f3..302462418e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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}`); });