Cleanup and minore refactor.

This commit is contained in:
William Welling
2017-03-24 22:36:15 -05:00
parent f51196b4a7
commit bf32f45298
7 changed files with 25 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
module.exports = {
// The REST API Location.
// The REST API server settings.
"rest": {
// NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
"nameSpace": "/api",
@@ -7,7 +7,7 @@ module.exports = {
"address": "localhost",
"port": 3000
},
// Protocol, path and port in use for this Angular2 UI
// Angular2 UI server settings.
"ui": {
"nameSpace": "/",
"protocol": "http",
@@ -16,10 +16,11 @@ module.exports = {
},
"cache": {
// how long should objects be cached for by default
"msToLive": 1 * 60 * 1000, // 1 minute
"msToLive": 15 * 60 * 1000, // 15 minute
"control": "max-age=60" // revalidate browser
},
"universal": {
// universal settings
// Angular Universal settings
"preboot": true,
"async": true
}

View File

@@ -1,5 +1,4 @@
// Sticky Footer
.outer-wrapper {
display: flex;
margin: 0;
@@ -23,4 +22,4 @@ h2.red {
h2.green {
color: green;
}
}

View File

@@ -21,7 +21,7 @@ export abstract class DataEffects<T extends CacheableObject> {
protected abstract getSerializer(): Serializer<T>;
constructor(
private config: GlobalConfig,
private EnvConfig: GlobalConfig,
private actions$: Actions,
private restApi: DSpaceRESTv2Service,
private objectCache: ObjectCacheService,
@@ -41,11 +41,11 @@ export abstract class DataEffects<T extends CacheableObject> {
if (hasNoValue(t) || hasNoValue(t.uuid)) {
throw new Error('The server returned an invalid object');
}
this.objectCache.add(t, this.config.cache.msToLive);
this.objectCache.add(t, this.EnvConfig.cache.msToLive);
});
})
.map((ts: Array<T>) => ts.map(t => t.uuid))
.map((ids: Array<string>) => new RequestCacheSuccessAction(action.payload.key, ids, new Date().getTime(), this.config.cache.msToLive))
.map((ids: Array<string>) => new RequestCacheSuccessAction(action.payload.key, ids, new Date().getTime(), this.EnvConfig.cache.msToLive))
.catch((error: Error) => Observable.of(new RequestCacheErrorAction(action.payload.key, error.message)));
});
@@ -59,9 +59,9 @@ export abstract class DataEffects<T extends CacheableObject> {
if (hasNoValue(t) || hasNoValue(t.uuid)) {
throw new Error('The server returned an invalid object');
}
this.objectCache.add(t, this.config.cache.msToLive);
this.objectCache.add(t, this.EnvConfig.cache.msToLive);
})
.map((t: T) => new RequestCacheSuccessAction(action.payload.key, [t.uuid], new Date().getTime(), this.config.cache.msToLive))
.map((t: T) => new RequestCacheSuccessAction(action.payload.key, [t.uuid], new Date().getTime(), this.EnvConfig.cache.msToLive))
.catch((error: Error) => Observable.of(new RequestCacheErrorAction(action.payload.key, error.message)));
});

View File

@@ -15,9 +15,9 @@ interface GlobalConfig {
"ui": ServerConfig,
"cache": {
"msToLive": number,
"control": string
},
"universal": {
"shouldRehydrate": boolean,
"preboot": boolean,
"async": boolean
}
@@ -36,7 +36,7 @@ switch (process.env.NODE_ENV) {
case 'prod':
case 'production':
production = true;
EnvConfigFile = './environment.prod.js';
EnvConfigFile = './environment.prod.js';
break;
case 'dev':
case 'development':
@@ -56,9 +56,9 @@ try {
}
// if EnvConfigFile set try to get configs
if(EnvConfigFile) {
if (EnvConfigFile) {
try {
EnvConfig = <GlobalConfig> Object.assign(EnvConfig, configContext(EnvConfigFile));
EnvConfig = <GlobalConfig>Object.assign(EnvConfig, configContext(EnvConfigFile));
} catch (e) {
console.warn("Cannot find file " + EnvConfigFile);
}

View File

@@ -23,7 +23,7 @@ import { effects } from '../../app/app.effects';
import { Meta } from '../angular2-meta';
import { RehydrateStoreAction } from "../../app/store.actions";
import { GLOBAL_CONFIG, EnvConfig } from '../../config';
import { GLOBAL_CONFIG, GlobalConfig, EnvConfig } from '../../config';
// import * as LRU from 'modern-lru';
@@ -73,7 +73,7 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
],
providers: [
{ provide: GLOBAL_CONFIG, useValue: EnvConfig },
{ provide: 'isBrowser', useValue: isBrowser },
{ provide: 'isNode', useValue: isNode },
@@ -88,7 +88,7 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
]
})
export class MainModule {
constructor(public store: Store<AppState>) {
constructor( @Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig, public store: Store<AppState>) {
// TODO(gdi2290): refactor into a lifecycle hook
this.doRehydrate();
}
@@ -96,7 +96,9 @@ export class MainModule {
doRehydrate() {
let defaultValue = {};
let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue);
this.store.dispatch(new RehydrateStoreAction(serverCache));
if (this.EnvConfig.universal.preboot) {
this.store.dispatch(new RehydrateStoreAction(serverCache));
}
}
_getCacheValue(key: string, defaultValue: any): any {

View File

@@ -64,7 +64,7 @@ app.use(morgan('common', {
function cacheControl(req, res, next) {
// instruct browser to revalidate in 60 seconds
res.header('Cache-Control', 'max-age=60');
res.header('Cache-Control', EnvConfig.cache.control || 'max-age=60');
next();
}
@@ -115,7 +115,7 @@ routes.forEach(route => {
app.get(`/${route}/*`, ngApp);
});
app.get('*', function(req, res) {
app.get('*', function (req, res) {
res.setHeader('Content-Type', 'application/json');
var pojo = { status: 404, message: 'No Content' };
var json = JSON.stringify(pojo, null, 2);

View File

@@ -57,7 +57,7 @@ app.use(morgan('dev'));
function cacheControl(req, res, next) {
// instruct browser to revalidate in 60 seconds
res.header('Cache-Control', 'max-age=60');
res.header('Cache-Control', EnvConfig.cache.control || 'max-age=60');
next();
}
@@ -107,7 +107,7 @@ routes.forEach(route => {
app.get(`/${route}/*`, ngApp);
});
app.get('*', function(req, res) {
app.get('*', function (req, res) {
res.setHeader('Content-Type', 'application/json');
var pojo = { status: 404, message: 'No Content' };
var json = JSON.stringify(pojo, null, 2);