Skip to content
Snippets Groups Projects
Commit 415a52f0 authored by William Welling's avatar William Welling
Browse files

Bring config into server.

parent 99f936ff
No related branches found
No related tags found
No related merge requests found
......@@ -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
}
};
......@@ -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]
})
......
......@@ -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() {
......
// 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 }
......@@ -45,7 +45,6 @@ export function getResponse() {
return {};
}
export const UNIVERSAL_KEY = 'UNIVERSAL_CACHE';
@NgModule({
......@@ -89,17 +88,15 @@ 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));
}
let defaultValue = {};
let serverCache = this._getCacheValue(NGRX_CACHE_KEY, defaultValue);
this.store.dispatch(new RehydrateStoreAction(serverCache));
}
_getCacheValue(key: string, defaultValue: any): any {
......
......@@ -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();
......
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment