app.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { ConfigSource } from '@growi/core/dist/interfaces';
  2. import loggerFactory from '~/utils/logger';
  3. import type Crowi from '../crowi';
  4. import S2sMessage from '../models/vo/s2s-message';
  5. import { configManager } from './config-manager';
  6. import type { S2sMessagingService } from './s2s-messaging/base';
  7. import type { S2sMessageHandlable } from './s2s-messaging/handlable';
  8. const logger = loggerFactory('growi:service:AppService');
  9. /**
  10. * the service class of AppService
  11. */
  12. export default class AppService implements S2sMessageHandlable {
  13. crowi: Crowi;
  14. s2sMessagingService: S2sMessagingService;
  15. constructor(crowi: Crowi) {
  16. this.crowi = crowi;
  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 = configManager.getConfig('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. } catch (e) {
  52. logger.error(
  53. 'Failed to publish post installation message with S2sMessagingService: ',
  54. e.message,
  55. );
  56. }
  57. }
  58. }
  59. getAppTitle() {
  60. return configManager.getConfig('app:title') ?? 'GROWI';
  61. }
  62. getTzoffset() {
  63. return -(configManager.getConfig('app:timezone') || 9) * 60;
  64. }
  65. getAppConfidential() {
  66. return configManager.getConfig('app:confidential');
  67. }
  68. async isDBInitialized(forceReload) {
  69. if (forceReload) {
  70. // load configs
  71. await configManager.loadConfigs();
  72. }
  73. return configManager.getConfig('app:installed', ConfigSource.db);
  74. }
  75. async setupAfterInstall(): Promise<void> {
  76. this.crowi.setupRoutesAtLast();
  77. this.crowi.setupGlobalErrorHandlers();
  78. }
  79. isMaintenanceMode(): boolean {
  80. return configManager.getConfig('app:isMaintenanceMode');
  81. }
  82. async startMaintenanceMode(): Promise<void> {
  83. await configManager.updateConfig('app:isMaintenanceMode', true);
  84. }
  85. async endMaintenanceMode(): Promise<void> {
  86. await configManager.updateConfig('app:isMaintenanceMode', false);
  87. }
  88. }