app.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { pathUtils } from '@growi/core';
  2. import loggerFactory from '~/utils/logger';
  3. import { generateConfigsForInstalling } from '../models/config';
  4. import S2sMessage from '../models/vo/s2s-message';
  5. import { S2sMessageHandlable } from './s2s-messaging/handlable';
  6. import { S2sMessagingService } from './s2s-messaging/base';
  7. const logger = loggerFactory('growi:service:AppService');
  8. /**
  9. * the service class of AppService
  10. */
  11. class AppService implements S2sMessageHandlable {
  12. crowi!: any;
  13. configManager: any;
  14. s2sMessagingService: S2sMessagingService;
  15. constructor(crowi) {
  16. this.crowi = crowi;
  17. this.configManager = crowi.configManager;
  18. this.s2sMessagingService = crowi.s2sMessagingService;
  19. }
  20. /**
  21. * @inheritdoc
  22. */
  23. shouldHandleS2sMessage(s2sMessage) {
  24. const { eventName } = s2sMessage;
  25. if (eventName !== 'systemInstalled') {
  26. return false;
  27. }
  28. const isInstalled = this.crowi.configManager.getConfig('crowi', 'app:installed');
  29. return !isInstalled;
  30. }
  31. /**
  32. * @inheritdoc
  33. */
  34. async handleS2sMessage(s2sMessage) {
  35. logger.info('Invoke post installation process by pubsub notification');
  36. const isDBInitialized = await this.isDBInitialized(true);
  37. if (isDBInitialized) {
  38. this.setupAfterInstall();
  39. }
  40. }
  41. async publishPostInstallationMessage() {
  42. const { s2sMessagingService } = this;
  43. if (s2sMessagingService != null) {
  44. const s2sMessage = new S2sMessage('systemInstalled');
  45. try {
  46. await s2sMessagingService.publish(s2sMessage);
  47. }
  48. catch (e) {
  49. logger.error('Failed to publish post installation message with S2sMessagingService: ', e.message);
  50. }
  51. }
  52. }
  53. getAppTitle() {
  54. return this.configManager.getConfig('crowi', 'app:title') || 'GROWI';
  55. }
  56. /**
  57. * get the site url
  58. *
  59. * If the config for the site url is not set, this returns a message "[The site URL is not set. Please set it!]".
  60. *
  61. * With version 3.2.3 and below, there is no config for the site URL, so the system always uses auto-generated site URL.
  62. * With version 3.2.4 to 3.3.4, the system uses the auto-generated site URL only if the config is not set.
  63. * With version 3.3.5 and above, the system use only a value from the config.
  64. */
  65. /* eslint-disable no-else-return */
  66. getSiteUrl() {
  67. const siteUrl = this.configManager.getConfig('crowi', 'app:siteUrl');
  68. if (siteUrl != null) {
  69. return pathUtils.removeTrailingSlash(siteUrl);
  70. }
  71. else {
  72. return '[The site URL is not set. Please set it!]';
  73. }
  74. }
  75. /* eslint-enable no-else-return */
  76. getTzoffset() {
  77. return -(this.configManager.getConfig('crowi', 'app:timezone') || 9) * 60;
  78. }
  79. getAppConfidential() {
  80. return this.configManager.getConfig('crowi', 'app:confidential');
  81. }
  82. /**
  83. * Execute only once for installing application
  84. */
  85. async initDB(globalLang) {
  86. const initialConfig = generateConfigsForInstalling();
  87. initialConfig['app:globalLang'] = globalLang;
  88. await this.configManager.updateConfigsInTheSameNamespace('crowi', initialConfig, true);
  89. }
  90. async isDBInitialized(forceReload) {
  91. if (forceReload) {
  92. // load configs
  93. await this.configManager.loadConfigs();
  94. }
  95. return this.configManager.getConfigFromDB('crowi', 'app:installed');
  96. }
  97. async setupAfterInstall() {
  98. await this.crowi.pluginService.autoDetectAndLoadPlugins();
  99. this.crowi.setupRoutesAtLast();
  100. this.crowi.setupGlobalErrorHandlers();
  101. // remove message handler
  102. const { s2sMessagingService } = this;
  103. if (s2sMessagingService != null) {
  104. this.s2sMessagingService.removeMessageHandler(this);
  105. }
  106. }
  107. }
  108. module.exports = AppService;