File size: 3,061 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * Copyright (c) 2023 MERCENARIES.AI PTE. LTD.
 * All rights reserved.
 */

// ---------------------------------------------------------------------------------------------
// loadConfig.ts
//  Purpose: Loads the server configuration from the default and local files
// ---------------------------------------------------------------------------------------------
import { existsSync, readFileSync } from 'fs';
import yaml from 'js-yaml';

import { type IAmqpServiceConfig } from './services/AmqpService.js';
import { type ICredentialServiceConfig } from './services/CredentialsService/CredentialService.js';
import { type FastifyServerServiceConfig } from './services/FastifyServerService.js';
import { type IJobControllerServiceConfig } from './services/JobController/JobControllerService.js';
import { type IMessagingServerServiceConfig } from './services/MessagingService.js';
import { type IDBServerServiceConfig } from './services/DBService.js';

interface IServerConfig {
  server: {
    kvStorage?: {
      dbPath: string;
    };
    version: string;
    integrations?: any;
    network: {
      interface: string;
      host: string;
      port: number;
      protocol: string;
      public_url: string;
      rateLimit: {
        global: boolean;
        max: number;
        timeWindow: number;
      };
    };

    session: {
      secret: string;
      cookie: {
        secure: boolean;
        httpOnly: boolean;
        maxAge: number;
      };
    };
    logger: { level: number };
    services: {
      langchain?: {
        opts?: any;
      };
      messaging: IMessagingServerServiceConfig;
      credentials: ICredentialServiceConfig;
      jobs: IJobControllerServiceConfig;
      // registry: IRegistryServiceConfig
      // componentService: IComponentServiceConfig
      amqp?: IAmqpServiceConfig;
      httpd?: FastifyServerServiceConfig;
      db: IDBServerServiceConfig;
      influx?: {
        influxUrl: string;
        influxToken: string;
        influxOrg: string;
      };
      rest_consumer?: {
        endpoint: string;
        username: string;
        password: string;
        exchange: {
          name: string;
          type: string;
          options: { durable: boolean; autoDelete?: boolean; internal?: boolean; arguments?: any };
        };
        retry: {
          disabled: boolean;
          delay: number;
          maxRetries: number;
        };
        useKeystore: boolean;
        disabled: boolean;
      };
    };
  };

  credentials?: {
    seaweed?: {
      headers: any;
    };
  };
}

const loadServerConfig = (defaultFile: string) => {
  let defaultConfig: any = {};

  if (existsSync(defaultFile)) {
    defaultConfig = yaml.load(readFileSync(defaultFile, 'utf8')) as IServerConfig;
    omnilog.info('Importing ', defaultFile, ' configuration');
    return defaultConfig;
  } else {
    // TODO: We need to generate a config for building the production version
    throw new Error('No ' + defaultFile + ' found at repository root');
  }
};

export { loadServerConfig, type IServerConfig };