app.js 3.5 KB

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