Skip to content

Commit

Permalink
don't prevalidate config
Browse files Browse the repository at this point in the history
  • Loading branch information
kimjoar committed May 27, 2017
1 parent 170926c commit 4dd5b74
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 121 deletions.
26 changes: 0 additions & 26 deletions platform/config/Config.ts

This file was deleted.

45 changes: 36 additions & 9 deletions platform/config/ConfigService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BehaviorSubject, Observable } from 'rxjs';
import { isEqual } from 'lodash';

import { Config } from './Config';
import { getRawConfig, applyArgv } from './readConfig';
import { Env } from '../env';
import { logger } from '../logger';
import { Setting, Any, TypeOf } from '../lib/schema';
import { ConfigWithSchema } from '../types';

const log = logger.get('settings');

Expand All @@ -16,26 +17,24 @@ export class ConfigService {
private readonly rawConfigFromFile$: BehaviorSubject<RawConfig | void> =
new BehaviorSubject(undefined)

private readonly config$: Observable<Config>;
private readonly rawConfig$: Observable<RawConfig>;

constructor(
private readonly argv: {[key: string]: any},
private readonly env: Env
) {
this.config$ = this.rawConfigFromFile$
this.rawConfig$ = this.rawConfigFromFile$
.filter(rawConfig => rawConfig !== undefined)
// we need to specify the type here, as we _know_ `RawConfig` no longer
// can be `undefined`.
.map<RawConfig, RawConfig>(rawConfig => applyArgv(argv, rawConfig))
// we only care about reloading the config if there are changes
.distinctUntilChanged((prev, next) => isEqual(prev, next))
.map(config => Config.create(config, env));
.distinctUntilChanged((prev, next) => isEqual(prev, next));
}

/**
* Reads the initial Kibana config
*/
// TODO inject schema?
start() {
this.loadConfig();
}
Expand All @@ -58,7 +57,35 @@ export class ConfigService {
this.rawConfigFromFile$.complete();
}

getConfig() {
return this.config$;
atPath<Schema extends Any, Config>(
path: string,
ConfigClass: ConfigWithSchema<Schema, Config>
) {
return this.getDistinctRawConfig(path)
.map(value => {
const config = ConfigClass.schema.validate(value);
return new ConfigClass(config, this.env);
});
}

optionalAtPath<Schema extends Any, Config>(
path: string,
ConfigClass: ConfigWithSchema<Schema, Config>
) {
return this.getDistinctRawConfig(path)
.map(value => {
if (value === undefined) {
return undefined;
}

const config = ConfigClass.schema.validate(value);
return new ConfigClass(config, this.env);
});
}

private getDistinctRawConfig(path: string) {
return this.rawConfig$
.map(config => config[path])
.distinctUntilChanged((prev, next) => isEqual(prev, next))
}
}
}
3 changes: 0 additions & 3 deletions platform/config/__tests__/__snapshots__/index.test.ts.snap

This file was deleted.

7 changes: 0 additions & 7 deletions platform/config/__tests__/index.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion platform/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { ConfigService } from './ConfigService';
export { Config } from './Config';
28 changes: 0 additions & 28 deletions platform/config/schema.ts

This file was deleted.

2 changes: 2 additions & 0 deletions platform/logger/LoggerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const loggerSchema = object({
export type LoggingSchema = TypeOf<typeof loggerSchema>;

export class LoggerConfig {
static schema = loggerSchema;

readonly dest: string;
private readonly silent: boolean;
private readonly quiet: boolean;
Expand Down
11 changes: 6 additions & 5 deletions platform/root/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Server } from '../server';
import { Env } from '../env';
import { ConfigService } from '../config';
import { loggerService, logger, LoggerConfig } from '../logger';
import { HttpModule } from '../server/http'

const log = logger.get('root');

Expand All @@ -23,16 +24,16 @@ export class Root {
start() {
this.configService.start();

const config$ = this.configService.getConfig();

const loggingConfig$ = config$
.map(config => new LoggerConfig(config.atPath('logging')));
const loggingConfig$ = this.configService.atPath(
'logging',
LoggerConfig
);

loggerService.upgrade(loggingConfig$);

log.info('starting the server');

this.server = new Server(config$);
this.server = new Server(this.configService);
this.server.start();
}

Expand Down
4 changes: 3 additions & 1 deletion platform/server/elasticsearch/ElasticsearchConfigs.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ElasticsearchConfig } from './ElasticsearchConfig';
import { ElasticsearchConfigsSchema } from './schema';
import { elasticsearchSchema, ElasticsearchConfigsSchema } from './schema';

import { Env } from '../../env';

export type ElasticsearchClusterType = 'data' | 'admin';

export class ElasticsearchConfigs {
static schema = elasticsearchSchema;

private readonly elasticsearchConfigs: {
data: ElasticsearchConfig,
admin: ElasticsearchConfig
Expand Down
11 changes: 4 additions & 7 deletions platform/server/elasticsearch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ import { ElasticsearchFacade } from './ElasticsearchFacade';
import { createElasticsearchRoutes } from './api';
import { RouterFactory } from '../http';
import { IsRouteable } from '../../types';
import { Config } from '../../config';
import { Schema } from '../../types';
import { ElasticsearchConfigs } from './ElasticsearchConfigs';

export { elasticsearchSchema } from './schema';
import { elasticsearchSchema } from './schema';
export { ElasticsearchService, ElasticsearchFacade, ElasticsearchConfigs };

export class ElasticsearchModule implements IsRouteable {
static config = ElasticsearchConfigs;

readonly service: ElasticsearchService;
readonly facade: ElasticsearchFacade;
readonly config$: Observable<ElasticsearchConfigs>;

constructor(config$: Observable<Config>) {
this.config$ = config$.map(config =>
new ElasticsearchConfigs(config.atPath('elasticsearch'), config.env)
);
constructor(readonly config$: Observable<ElasticsearchConfigs>) {
this.service = new ElasticsearchService(this.config$);
this.facade = new ElasticsearchFacade(this.service);
}
Expand Down
2 changes: 2 additions & 0 deletions platform/server/http/HttpConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export const httpSchema = object({
export type HttpSchema = TypeOf<typeof httpSchema>;

export class HttpConfig {
static schema = httpSchema;

host: string;
port: number;
maxPayload: ByteSizeValue;
Expand Down
10 changes: 4 additions & 6 deletions platform/server/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { Observable } from 'rxjs';

import { Config } from '../../config';
import { HttpService } from './HttpService';
import { HttpConfig } from './HttpConfig';
import { Routers } from './Routing';
import { httpSchema } from './HttpConfig';

export { httpSchema } from './HttpConfig';
export { KibanaRequest, KibanaResponse, RouterFactory, Router } from './Routing';
export { HttpService, Routers };

export class HttpModule {
static config = HttpConfig;

readonly service: HttpService;
readonly config$: Observable<HttpConfig>;

constructor(config$: Observable<Config>, routers: Routers) {
this.config$ = config$.map(config =>
new HttpConfig(config.atPath('server'), config.env)
);
constructor(readonly config$: Observable<HttpConfig>, routers: Routers) {
this.service = new HttpService(this.config$, routers.routers$);
}
}
78 changes: 72 additions & 6 deletions platform/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Observable, ReplaySubject } from 'rxjs';
import { isEqual } from 'lodash';

import { ConfigService } from '../config';
import { HttpModule, Routers } from './http';
import { PidModule } from './pid';
import { SavedObjectsModule } from './savedObjects';
import { ElasticsearchModule } from './elasticsearch';
import { KibanaModule } from './kibana';
import { logger } from '../logger';
import { Config } from '../config';
import * as schema from '../lib/schema';
import { KibanaRequestHelpers } from '../types';

const log = logger.get('server');


export class Server {
private readonly elasticsearch: ElasticsearchModule;
private readonly http: HttpModule;
Expand All @@ -21,11 +23,32 @@ export class Server {
private readonly routers: Routers;

constructor(
private readonly config$: Observable<Config>
private readonly configService: ConfigService
) {
this.pid = new PidModule(config$);
this.elasticsearch = new ElasticsearchModule(config$);
this.kibana = new KibanaModule(config$);
const pidConfig$ = configService.optionalAtPath(
'pid', PidModule.config
);

const elasticsearchConfigs$ = configService.atPath(
'elasticsearch', ElasticsearchModule.config
);

const kibanaConfig$ = configService.atPath(
'kibana', KibanaModule.config
);

const httpConfig$ = configService.atPath(
'server', HttpModule.config
);

// const configObservables = configService.getConfigFor({
// ...coreConfigs,
// pid: PidModule.config
// })

this.pid = new PidModule(pidConfig$);
this.elasticsearch = new ElasticsearchModule(elasticsearchConfigs$);
this.kibana = new KibanaModule(kibanaConfig$);
this.savedObjects = new SavedObjectsModule(
this.kibana.config$,
this.elasticsearch.service
Expand Down Expand Up @@ -53,8 +76,51 @@ export class Server {
savedObjects: this.savedObjects.facade
};

// const kibanaForPlugins: KibanaHelpers = {
// services: {
// elasticsearch: this.elasticsearch.service
// }
// }

// const plugins = [
// watcher,
// monitoring
// ]

// topologicalSort(plugins).map(plugin => plugin(kibanaForPlugins))
// type Monitoring = {
// key: string
// }

// type MonitoringHelper = {
// monitoring: Monitoring
// }

// type TestHelper = {
// test: {
// foo: number
// }
// }

// type MyHelper = {
// test?: TestHelper,
// monitoring: Monitoring
// }

// class KibanaPlugin<T> {
// constructor(obj: T) {
// }
// }

// class WatcherPlugin extends KibanaPlugin<MyHelper> {
// }

// new WatcherPlugin({
// monitoring
// })

this.routers = new Routers(kibana);
this.http = new HttpModule(config$, this.routers);
this.http = new HttpModule(httpConfig$, this.routers);
}

start() {
Expand Down
4 changes: 3 additions & 1 deletion platform/server/kibana/KibanaConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { KibanaConfigSchema } from './schema';
import { kibanaSchema, KibanaConfigSchema } from './schema';

export class KibanaConfig {
static schema = kibanaSchema;

readonly index: string;

constructor(config: KibanaConfigSchema) {
Expand Down
Loading

0 comments on commit 4dd5b74

Please sign in to comment.