{"size": 2578, "ext": "js", "max_stars_repo_path": "mchat-msgcenter/utils/commonFun.js", "max_stars_repo_name": "Thinker-Mars/mChat-backend", "max_stars_repo_stars_event_min_datetime": "2021-03-24T08:44:22.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-24T08:44:22.000Z", "max_issues_repo_path": "mchat-msgcenter/utils/commonFun.js", "max_issues_repo_name": "Thinker-Mars/mChat-backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "mchat-msgcenter/utils/commonFun.js", "max_forks_repo_name": "Thinker-Mars/mChat-backend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 18.2836879433, "max_line_length": 91, "alphanum_fraction": 0.6109387122}
const os = require('os');
const moment = require('moment');
const Request = require('./request');
const {
	ServiceID, ServiceConfigAddr, ServicePort, ServiceWeight,
	ApiKey, ServiceDesc, ConsumerKey, ConsumerConfigAddr
} = require('../config/serviceConfig');

/**
 * 获取IP
 */
function getIP() {
	const interfaces = os.networkInterfaces();
	for (const type in interfaces) {
		const netInfos = interfaces[type];
		for (let i = 0; i < netInfos.length; i++) {
			const netInfo = netInfos[i];
			if (netInfo.family === 'IPv4' && netInfo.address !== '127.0.0.1' && !netInfo.internal) {
				return netInfo.address;
			}
		}
	}
}

/**
 * 获取服务信息
 */
function getService() {
	const serviceUrl = `${ServiceConfigAddr}/${ServiceID}?api_key=${ApiKey}`;
	return Request.get(serviceUrl);
}

/**
 * 更新服务
 */
function updateService() {
	/**
	 * 此服务的地址：ip:port
	 */
	const serviceAddress = `${getIP()}:${ServicePort}`;

	const serviceReqUrl = `${ServiceConfigAddr}/${ServiceID}?api_key=${ApiKey}`;

	/**
	 * 当前时间
	 */
	const time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');

	const nodes = {};
	nodes[serviceAddress] = ServiceWeight;
	const data = {
		upstream: {
			nodes
		}
	};

	Request.patch(serviceReqUrl, data).then(
		() => {
			console.log(`[消息中心服务]注册成功: ${serviceAddress} ${time}`);
		},
		(err) => {
			console.log(err, `[消息中心服务]注册失败 ${time}`);
		}
	);
}

/**
 * 新增服务
 */
function addService() {
	/**
	 * 此服务的地址：ip:port
	 */
	const serviceAddress = `${getIP()}:${ServicePort}`;

	const serviceReqUrl = `${ServiceConfigAddr}/${ServiceID}?api_key=${ApiKey}`;

	const consumerReqUrl = `${ConsumerConfigAddr}/?api_key=${ApiKey}`;

	/**
		* 生成当前时间
		*/
	const time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');

	const nodes = {};
	nodes[serviceAddress] = 1;
	const serviceData = {
		plugins: {
			'key-auth': {}
		},
		desc: ServiceDesc,
		upstream: {
			type: 'roundrobin',
			nodes
		}
	};

	const consumerData = {
		username: ConsumerKey,
		plugins: {
			'key-auth': {
				'key': ConsumerKey
			}
		}
	};

	// 注册consumer
	Request.put(consumerReqUrl, consumerData).then(
		(res) => {
			// 注册service
			Request.put(serviceReqUrl, serviceData).then(
				() => {
					console.log(`[消息中心服务]注册成功: ${serviceAddress} ${time}`);
				},
				err => {
					console.log(err, `[消息中心服务]注册失败 ${time}`);
				}
			);
		}
	);
}

/**
 * 服务启动之后，向网关注册服务信息
 */
function registerService() {
	// 先判断是否已有相关服务存在
	getService().then(
		(res) => {
			// 已有服务注册，更新服务注册信息
			updateService();
		},
		(err) => {
			// 目前尚无服务注册
			addService();
		}
	);
}

module.exports.registerService = registerService;
