2
0

app.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { ConfigSource } from '@growi/core/dist/interfaces';
  2. import loggerFactory from '~/utils/logger';
  3. import S2sMessage from '../models/vo/s2s-message';
  4. import { configManager } from './config-manager';
  5. import type { S2sMessagingService } from './s2s-messaging/base';
  6. import type { S2sMessageHandlable } from './s2s-messaging/handlable';
  7. const logger = loggerFactory('growi:service:AppService');
  8. /**
  9. * the service class of AppService
  10. */
  11. export default class AppService implements S2sMessageHandlable {
  12. crowi!: any;
  13. s2sMessagingService: S2sMessagingService;
  14. constructor(crowi) {
  15. this.crowi = crowi;
  16. this.s2sMessagingService = crowi.s2sMessagingService;
  17. }
  18. /**
  19. * @inheritdoc
  20. */
  21. shouldHandleS2sMessage(s2sMessage) {
  22. const { eventName } = s2sMessage;
  23. if (eventName !== 'systemInstalled') {
  24. return false;
  25. }
  26. const isInstalled = configManager.getConfig('app:installed');
  27. return !isInstalled;
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. async handleS2sMessage(s2sMessage) {
  33. logger.info('Invoke post installation process by pubsub notification');
  34. const isDBInitialized = await this.isDBInitialized(true);
  35. if (isDBInitialized) {
  36. this.setupAfterInstall();
  37. // remove message handler
  38. const { s2sMessagingService } = this;
  39. if (s2sMessagingService != null) {
  40. this.s2sMessagingService.removeMessageHandler(this);
  41. }
  42. }
  43. }
  44. async publishPostInstallationMessage() {
  45. const { s2sMessagingService } = this;
  46. if (s2sMessagingService != null) {
  47. const s2sMessage = new S2sMessage('systemInstalled');
  48. try {
  49. await s2sMessagingService.publish(s2sMessage);
  50. }
  51. catch (e) {
  52. logger.error('Failed to publish post installation message with S2sMessagingService: ', e.message);
  53. }
  54. }
  55. }
  56. getAppTitle() {
  57. return configManager.getConfig('app:title') ?? 'GROWI';
  58. }
  59. getTzoffset() {
  60. return -(configManager.getConfig('app:timezone') || 9) * 60;
  61. }
  62. getAppConfidential() {
  63. return configManager.getConfig('app:confidential');
  64. }
  65. async isDBInitialized(forceReload) {
  66. if (forceReload) {
  67. // load configs
  68. await configManager.loadConfigs();
  69. }
  70. return configManager.getConfig('app:installed', ConfigSource.db);
  71. }
  72. async setupAfterInstall(): Promise<void> {
  73. this.crowi.setupRoutesAtLast();
  74. this.crowi.setupGlobalErrorHandlers();
  75. }
  76. isMaintenanceMode(): boolean {
  77. return configManager.getConfig('app:isMaintenanceMode');
  78. }
  79. async startMaintenanceMode(): Promise<void> {
  80. await configManager.updateConfig('app:isMaintenanceMode', true);
  81. }
  82. async endMaintenanceMode(): Promise<void> {
  83. await configManager.updateConfig('app:isMaintenanceMode', false);
  84. }
  85. }