Просмотр исходного кода

refactor initializing es client

Yuki Takei 6 лет назад
Родитель
Сommit
7220e538d9
3 измененных файлов с 62 добавлено и 40 удалено
  1. 8 7
      src/server/crowi/index.js
  2. 12 6
      src/server/service/config-loader.js
  3. 42 27
      src/server/util/search.js

+ 8 - 7
src/server/crowi/index.js

@@ -330,19 +330,20 @@ Crowi.prototype.setupPassport = async function() {
 };
 
 Crowi.prototype.setupSearcher = async function() {
-  const self = this;
-  const searcherUri = this.env.ELASTICSEARCH_URI
-    || this.env.SEARCHBOX_SSL_URL
-    || null;
+  const esUri = this.configManager.getConfig('crowi', 'app:elasticsearchUri');
+  const sbUrl = this.configManager.getConfig('crowi', 'app:searchboxSslUrl');
+
+  const searcherUri = esUri || sbUrl;
 
   if (searcherUri) {
+    const SearchClient = require('@server/util/search');
     try {
-      self.searcher = new (require(path.join(self.libDir, 'util', 'search')))(self, searcherUri);
-      self.searcher.initIndices();
+      this.searcher = new SearchClient(this, searcherUri);
+      this.searcher.initIndices();
     }
     catch (e) {
       logger.error('Error on setup searcher', e);
-      self.searcher = null;
+      this.searcher = null;
     }
   }
 };

+ 12 - 6
src/server/service/config-loader.js

@@ -22,12 +22,6 @@ const TYPES = {
  *  So, parameters of these are under consideration.
  */
 const ENV_VAR_NAME_TO_CONFIG_INFO = {
-  // ELASTICSEARCH_URI: {
-  //   ns:      ,
-  //   key:     ,
-  //   type:    ,
-  //   default:
-  // },
   // FILE_UPLOAD: {
   //   ns:      ,
   //   key:     ,
@@ -136,6 +130,18 @@ const ENV_VAR_NAME_TO_CONFIG_INFO = {
     type:    TYPES.NUMBER,
     default: Infinity,
   },
+  ELASTICSEARCH_URI: {
+    ns:      'crowi',
+    key:     'app:elasticsearchUri',
+    type:    TYPES.STRING,
+    default: null,
+  },
+  SEARCHBOX_SSL_URL: {
+    ns:      'crowi',
+    key:     'app:searchboxSslUrl',
+    type:    TYPES.STRING,
+    default: null,
+  },
   MONGO_GRIDFS_TOTAL_LIMIT: {
     ns:      'crowi',
     key:     'gridfs:totalLimit',

+ 42 - 27
src/server/util/search.js

@@ -6,6 +6,8 @@ const elasticsearch = require('elasticsearch');
 const debug = require('debug')('growi:lib:search');
 const logger = require('@alias/logger')('growi:lib:search');
 
+const { URL } = require('url');
+
 const {
   Writable, Transform,
 } = require('stream');
@@ -50,21 +52,52 @@ function SearchClient(crowi, esUri) {
     },
   };
 
-  const uri = this.parseUri(this.esUri);
-  this.host = uri.host;
-  this.indexName = uri.indexName;
-  this.aliasName = `${this.indexName}-alias`;
+  this.client = null;
+  this.indexName = null;
+  this.aliasName = null;
+
+  this.mappingFile = `${crowi.resourceDir}search/mappings.json`;
+
+  try {
+    this.initClient();
+    this.registerUpdateEvent();
+  }
+  catch (err) {
+    logger.error(err);
+  }
+
+}
+
+SearchClient.prototype.initClient = function() {
+  let indexName = 'crowi';
+  let host = this.esUri;
+  let httpAuth = '';
+
+  const isSearchboxSsl = this.configManager.getConfig('crowi', 'app:searchboxSslUrl') != null;
+
+  const url = new URL(this.esUri);
+  if (url.pathname !== '/') {
+    host = isSearchboxSsl
+      ? `${url.protocol}//${url.hostname}:443` // use 443 when Searchbox
+      : `${url.protocol}//${url.host}`;
+
+    indexName = url.pathname.substring(1); // omit heading slash
+
+    if (url.auth != null) {
+      httpAuth = url.auth;
+    }
+  }
 
   this.client = new elasticsearch.Client({
-    host: this.host,
+    host,
+    httpAuth,
     requestTimeout: 5000,
     // log: 'debug',
   });
 
-  this.registerUpdateEvent();
-
-  this.mappingFile = `${crowi.resourceDir}search/mappings.json`;
-}
+  this.indexName = indexName;
+  this.aliasName = `${this.indexName}-alias`;
+};
 
 SearchClient.prototype.getInfo = function() {
   return this.client.info({});
@@ -109,24 +142,6 @@ SearchClient.prototype.shouldIndexed = function(page) {
   return page.creator != null && page.revision != null && page.redirectTo == null;
 };
 
-SearchClient.prototype.parseUri = function(uri) {
-  let indexName = 'crowi';
-  let host = uri;
-
-  // https://regex101.com/r/eKT4Db/4
-  const match = uri.match(/^(https?:\/\/[^/]+)\/?(.+)?$/);
-
-  if (match) {
-    host = match[1];
-    indexName = match[2] || 'growi';
-  }
-
-  return {
-    host,
-    indexName,
-  };
-};
-
 SearchClient.prototype.initIndices = async function() {
   await this.checkESVersion();