Bring config into server.

This commit is contained in:
William Welling
2017-03-24 16:48:43 -05:00
parent 99f936ff45
commit 415a52f011
7 changed files with 51 additions and 23 deletions

View File

@@ -4,12 +4,16 @@ module.exports = {
"rest": {
// NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
"nameSpace": "/api",
"baseURL": "http://localhost:3000"
"protocol": "http",
"address": "localhost",
"port": 3000
},
// Path and Port in use for this Angular2 UI
"ui": {
"nameSpace": "/",
"baseURL": "http://localhost:3000"
"protocol": "http",
"address": "localhost",
"port": 3000
},
"cache": {
// how long should objects be cached for by default
@@ -17,6 +21,8 @@ module.exports = {
},
"universal": {
//on the client: start with the state on the server
"shouldRehydrate": true
"shouldRehydrate": true,
"preboot": true,
"async": true
}
};

View File

@@ -21,6 +21,8 @@ import { HostWindowState } from "./shared/host-window.reducer";
import { HostWindowResizeAction } from "./shared/host-window.actions";
import { MockTranslateLoader } from "./shared/testing/mock-translate-loader";
import { globalConfig } from '../config';
let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let de: DebugElement;
@@ -37,7 +39,8 @@ describe('App component', () => {
})],
declarations: [AppComponent], // declare the test component
providers: [
AppComponent
AppComponent,
globalConfig
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})

View File

@@ -45,6 +45,8 @@ 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() {

View File

@@ -1,7 +1,7 @@
// Look in ./config folder for config
import { OpaqueToken } from '@angular/core';
import path from 'path';
import * as path from 'path';
let configContext = require.context("../config", false, /js$/);
@@ -33,27 +33,43 @@ try {
const GLOBAL_CONFIG = new OpaqueToken('config');
interface ServerConfig {
"nameSpace": string,
"protocol": string,
"address": string,
"port": number,
"baseURL": string
}
interface GlobalConfig {
"production": string,
"rest": {
"nameSpace": string,
"baseURL": string
},
"ui": {
"nameSpace": string,
"baseURL": string
},
"rest": ServerConfig,
"ui": ServerConfig,
"cache": {
"msToLive": number,
},
"universal": {
"shouldRehydrate": boolean
"shouldRehydrate": boolean,
"preboot": boolean,
"async": boolean
}
}
const config: GlobalConfig = <GlobalConfig>Object.assign(DefaultConfig, EnvConfig);
function buildURL(server: ServerConfig) {
return [server.protocol, '://', server.address, (server.port !== 80) ? ':' + server.port : ''].join('');
}
for (let key in config) {
if (config[key].protocol && config[key].address && config[key].port) {
config[key].baseURL = buildURL(config[key]);
}
}
const globalConfig = {
provide: GLOBAL_CONFIG,
useValue: <GlobalConfig>Object.assign(DefaultConfig, EnvConfig)
useValue: config
};
export { GLOBAL_CONFIG, GlobalConfig, globalConfig}
export { GLOBAL_CONFIG, GlobalConfig, globalConfig, config }

View File

@@ -45,7 +45,6 @@ export function getResponse() {
return {};
}
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
@NgModule({
@@ -89,18 +88,16 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
]
})
export class MainModule {
constructor(public store: Store<AppState>, @Inject(GLOBAL_CONFIG) private config: GlobalConfig, ) {
constructor(public store: Store<AppState>) {
// TODO(gdi2290): refactor into a lifecycle hook
this.doRehydrate();
}
doRehydrate() {
if (this.config.universal.shouldRehydrate) {
let defaultValue = {};
let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue);
this.store.dispatch(new RehydrateStoreAction(serverCache));
}
}
_getCacheValue(key: string, defaultValue: any): any {
// browser

View File

@@ -25,6 +25,8 @@ import { MainModuleNgFactory } from './platform/modules/node.module.ngfactory';
// Routes
import { routes } from './server.routes';
import { config } from './config';
// enable prod for faster renders
enableProdMode();

View File

@@ -24,6 +24,8 @@ import { MainModule } from './platform/modules/node.module';
// Routes
import { routes } from './server.routes';
import { config } from './config';
// enable prod for faster renders
enableProdMode();