config.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import path from "path";
  2. import os from 'os';
  3. import { LdapAuthenticationConfiguration } from 'craftlab-auth/dist'
  4. export interface Configuration {
  5. port: number;
  6. slave: boolean;
  7. slaves: Array<string>;
  8. masterPubKey: string;
  9. masterPrivateKey: string;
  10. hostname: string;
  11. apiKeys: string[];
  12. ldapAuthentication: LdapAuthenticationConfiguration|null
  13. }
  14. class ConfigurationManagerLoader {
  15. public static Load(fileName: string): Configuration {
  16. let config = require(fileName);
  17. config.slaves = config.slaves || [];
  18. if (config.slave && config.slaves?.length)
  19. throw new Error("Cannot have slaves while beeing slave");
  20. if (config.slave && !config.masterPubKey)
  21. throw new Error("Missing master public key for slave. Run with --generate-masterkey get get one");
  22. if (config.slaves.length && !config.masterPrivateKey)
  23. throw new Error("Missing master private key for slaves. Run with --generate-masterkey get get one");
  24. return {
  25. port: config.port || 9090,
  26. slave: config.slave || false,
  27. slaves: config.slaves || new Array(),
  28. masterPubKey: config.masterPubKey || "",
  29. masterPrivateKey: config.masterPrivateKey || "",
  30. apiKeys: config.apiKeys || [],
  31. hostname: os.hostname(),
  32. ldapAuthentication: config.ldapAuthentication ? <LdapAuthenticationConfiguration>{
  33. bindDnField: config.ldapAuthentication.bindDnField,
  34. bindBase: config.ldapAuthentication.bindBase,
  35. ldapUrl: config.ldapAuthentication.ldapUrl,
  36. usernameField: config.ldapAuthentication.usernameField,
  37. ldapFilter: config.ldapAuthentication.ldapFilter
  38. } : null
  39. };
  40. }
  41. }
  42. let ConfigurationManager = ConfigurationManagerLoader.Load(path.join(__dirname, "../config.json"));
  43. export default ConfigurationManager;