-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathetcd.module.ts
73 lines (66 loc) · 2.63 KB
/
etcd.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Module, DynamicModule, Global } from '@nestjs/common';
import { ETCD, IBoot, BOOT } from '@nestcloud/common';
import { ETCD_OPTIONS_PROVIDER } from './etcd.constants';
import { Etcd } from './etcd';
import { AsyncEtcdOptions, EtcdOptions } from './interfaces/etcd-options.interface';
import { Etcd3, IOptions } from 'etcd3';
import { DiscoveryModule } from '@nestjs/core';
import { EtcdMetadataAccessor } from './etcd-metadata.accessor';
import { EtcdOrchestrator } from './etcd.orchestrator';
import { EtcdExplorer } from './etcd.explorer';
@Global()
@Module({
imports: [DiscoveryModule],
providers: [EtcdMetadataAccessor, EtcdOrchestrator],
})
export class EtcdModule {
private static CONFIG_PREFIX = 'etcd';
public static forRoot(options: EtcdOptions): DynamicModule {
return this.register(options);
}
public static forRootAsync(options: AsyncEtcdOptions): DynamicModule {
return this.register(options);
}
private static register(options: EtcdOptions & AsyncEtcdOptions = { hosts: '127.0.0.1:2379' }): DynamicModule {
const inject = options.inject || [];
const etcdOptionsProvider = {
provide: ETCD_OPTIONS_PROVIDER,
useFactory: (...params: any[]) => {
const registerOptions = options;
const boot: IBoot = params[inject.indexOf(BOOT)];
if (boot) {
options = boot.get(this.CONFIG_PREFIX);
}
return Object.assign(registerOptions, options);
},
inject,
};
const etcdProvider = {
provide: ETCD,
useFactory: (options: EtcdOptions): Etcd => {
this.parseCertificate(options);
return new Etcd3(options as IOptions);
},
inject: [ETCD_OPTIONS_PROVIDER],
};
return {
module: EtcdModule,
providers: [etcdOptionsProvider, etcdProvider, EtcdExplorer],
exports: [etcdProvider],
};
}
private static parseCertificate(options: EtcdOptions): EtcdOptions {
if (options.credentials) {
if (options.credentials.rootCertificate) {
options.credentials.rootCertificate = new Buffer(options.credentials.rootCertificate);
}
if (options.credentials.privateKey) {
options.credentials.privateKey = new Buffer(options.credentials.privateKey);
}
if (options.credentials.certChain) {
options.credentials.certChain = new Buffer(options.credentials.certChain);
}
}
return options;
}
}