/* * @Description: 框架服务elasticsearch模块 * @version: 1.0.0 * @Company: saganlab * @Author: vcbear * @Date: 2022-08-24 19:01:06 * @LastEditors: vcbear * @LastEditTime: 2022-08-24 21:50:26 */ import elasticsearch from 'elasticsearch'; import { YiProcess } from '../types'; const dprocess = process as YiProcess; export default async (app) => { const esConfig = app.config?.elasticsearch || {}; // 如果esConfig为空,则不执行这里的代码 if (Object.keys(esConfig).length !== 0) { let client = new elasticsearch.Client(esConfig); const c = { cyan: '\x1b[36m', red: '\x1b[31m', end: '\x1b[39m' }; try { await client.ping({ requestTimeout: 1000 }) app.use((ctx, next) => { ctx.elasticsearch = client; return next() }) app.esConMsg = `elasticsearch connect success. host: ${c.cyan}${esConfig.host}${c.end}` } catch (error) { dprocess.emit('error', error); } } }