app.ts 3.3 KB

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