-
Notifications
You must be signed in to change notification settings - Fork 122
/
index.ts
81 lines (73 loc) · 2.37 KB
/
index.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
74
75
76
77
78
79
80
81
import 'clarify'; // clean the stacktraces
import filterRulesLoader, {
isUniversalFilters,
} from './common/filter/filter-rules-loading';
import { log as logger } from './logs/logger';
import {
CONFIGURATION,
getConfig,
loadBrokerConfig,
} from './common/config/config';
import { FiltersType } from './common/types/filter';
process.on('uncaughtExceptionMonitor', (error, origin) => {
logger.error({ error, origin }, 'found unhandled exception');
});
process.on('unhandledRejection', (reason: any) => {
logger.error(
{
reason: reason.stack,
},
'caught unhandledRejection',
);
});
export const app = async ({ port = 7341, client = false, config }) => {
// note: the config is loaded in the main function to allow us to mock in tests
if (process.env.JEST_WORKER_ID) {
delete require.cache[require.resolve('./common/config/config')];
}
const method = client ? 'client' : 'server';
if (process.env.UNIVERSAL_BROKER_ENABLED) {
// Pass custom/dev config.<SERVICE_ENV>.json, otherwise default universal
process.env.SERVICE_ENV = process.env.SERVICE_ENV || 'universal';
}
// loading it "manually" simplifies lot testing
await loadBrokerConfig();
const globalConfig = getConfig();
const localConfig = Object.assign({}, globalConfig, config) as Record<
string,
any
> as CONFIGURATION;
localConfig.brokerType = method;
const filters = filterRulesLoader(localConfig);
if (!filters) {
const error = new ReferenceError(
`No Filters found. A Broker requires filters to run. Shutting down.`,
);
error['code'] = 'MISSING_FILTERS';
logger.error({ error }, error.message);
throw error;
} else {
if (method == 'client') {
// if the localConfig has the broker server, then we must assume it's a client
return await (
await import('./client')
).main({ config: localConfig, port: localConfig.port || port, filters });
} else {
if (isUniversalFilters(filters)) {
throw new Error('Unexpected Universal Broker filters for server');
} else {
const classicFilters: FiltersType = filters as FiltersType;
return await (
await import('./server')
).main({
config: localConfig,
port: localConfig.port || port,
filters: classicFilters,
});
}
}
}
};
if (!module.parent) {
app({ config: {} }).then(() => {});
}