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": { "rest": {
// NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript // NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
"nameSpace": "/api", "nameSpace": "/api",
"baseURL": "http://localhost:3000" "protocol": "http",
"address": "localhost",
"port": 3000
}, },
// Path and Port in use for this Angular2 UI // Path and Port in use for this Angular2 UI
"ui": { "ui": {
"nameSpace": "/", "nameSpace": "/",
"baseURL": "http://localhost:3000" "protocol": "http",
"address": "localhost",
"port": 3000
}, },
"cache": { "cache": {
// how long should objects be cached for by default // how long should objects be cached for by default
@@ -17,6 +21,8 @@ module.exports = {
}, },
"universal": { "universal": {
//on the client: start with the state on the server //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 { HostWindowResizeAction } from "./shared/host-window.actions";
import { MockTranslateLoader } from "./shared/testing/mock-translate-loader"; import { MockTranslateLoader } from "./shared/testing/mock-translate-loader";
import { globalConfig } from '../config';
let comp: AppComponent; let comp: AppComponent;
let fixture: ComponentFixture<AppComponent>; let fixture: ComponentFixture<AppComponent>;
let de: DebugElement; let de: DebugElement;
@@ -37,7 +39,8 @@ describe('App component', () => {
})], })],
declarations: [AppComponent], // declare the test component declarations: [AppComponent], // declare the test component
providers: [ providers: [
AppComponent AppComponent,
globalConfig
], ],
schemas: [CUSTOM_ELEMENTS_SCHEMA] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}) })

View File

@@ -45,6 +45,8 @@ export class AppComponent implements OnDestroy, OnInit {
translate.setDefaultLang('en'); translate.setDefaultLang('en');
// the lang to use, if the lang isn't available, it will use the current loader to get them // the lang to use, if the lang isn't available, it will use the current loader to get them
translate.use('en'); translate.use('en');
console.log(this.config);
} }
ngOnInit() { ngOnInit() {

View File

@@ -1,7 +1,7 @@
// Look in ./config folder for config // Look in ./config folder for config
import { OpaqueToken } from '@angular/core'; import { OpaqueToken } from '@angular/core';
import path from 'path'; import * as path from 'path';
let configContext = require.context("../config", false, /js$/); let configContext = require.context("../config", false, /js$/);
@@ -33,27 +33,43 @@ try {
const GLOBAL_CONFIG = new OpaqueToken('config'); const GLOBAL_CONFIG = new OpaqueToken('config');
interface ServerConfig {
"nameSpace": string,
"protocol": string,
"address": string,
"port": number,
"baseURL": string
}
interface GlobalConfig { interface GlobalConfig {
"production": string, "production": string,
"rest": { "rest": ServerConfig,
"nameSpace": string, "ui": ServerConfig,
"baseURL": string
},
"ui": {
"nameSpace": string,
"baseURL": string
},
"cache": { "cache": {
"msToLive": number, "msToLive": number,
}, },
"universal": { "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 = { const globalConfig = {
provide: GLOBAL_CONFIG, 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 {}; return {};
} }
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE'; export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
@NgModule({ @NgModule({
@@ -89,17 +88,15 @@ export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
] ]
}) })
export class MainModule { 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 // TODO(gdi2290): refactor into a lifecycle hook
this.doRehydrate(); this.doRehydrate();
} }
doRehydrate() { doRehydrate() {
if (this.config.universal.shouldRehydrate) { let defaultValue = {};
let defaultValue = {}; let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue);
let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue); this.store.dispatch(new RehydrateStoreAction(serverCache));
this.store.dispatch(new RehydrateStoreAction(serverCache));
}
} }
_getCacheValue(key: string, defaultValue: any): any { _getCacheValue(key: string, defaultValue: any): any {

View File

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

View File

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