1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | var debug = require('debug')('plugin:eurekeclient');
|
8 |
|
9 | var os = require('os');
|
10 |
|
11 | const port = process.env.PORT || 8000;
|
12 | const Eureka = require('eureka-js-client').Eureka;
|
13 |
|
14 | const CONSOLE_LOG_TAG_COMP = 'microgateway-plugins eurekaclient';
|
15 | const LOG_TAG_COMP = 'eurekaclient';
|
16 |
|
17 | module.exports.init = function (config , logger /*, stats */) {
|
18 |
|
19 |
|
20 |
|
21 | config.instance.hostName = os.hostname();
|
22 | debug('local hostName: ' + config.instance.hostName);
|
23 | config.instance.ipAddr = getIPAddr();
|
24 | config.instance.port = {};
|
25 | config.instance.port["$"] = port;
|
26 | config.instance.port["@enabled"] = true;
|
27 | config.instance.dataCenterInfo["@class"] = "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo";
|
28 |
|
29 | const client = new Eureka(config);
|
30 |
|
31 | try {
|
32 | client.start();
|
33 | } catch (err) {
|
34 | logger.consoleLog('error',{component: CONSOLE_LOG_TAG_COMP}, err);
|
35 | client.stop();
|
36 | }
|
37 |
|
38 | function getIPAddr() {
|
39 | var interfaces = os.networkInterfaces();
|
40 | var addresses = [];
|
41 | for (var k in interfaces) {
|
42 | for (var k2 in interfaces[k]) {
|
43 | var address = interfaces[k][k2];
|
44 | if (address.family === 'IPv4' && !address.internal) {
|
45 | addresses.push(address.address);
|
46 | }
|
47 | }
|
48 | }
|
49 | debug ('localhost ip: ' + addresses[0]);
|
50 | return addresses[0];
|
51 | }
|
52 |
|
53 | function getAppName(url) {
|
54 | for (var index in config.lookup) {
|
55 | if (url.includes(config.lookup[index].uri) || url === config.lookup[index].uri) {
|
56 | return {
|
57 | app: config.lookup[index].app,
|
58 | secure: config.lookup[index].secure
|
59 | };
|
60 | }
|
61 | }
|
62 | return "";
|
63 | }
|
64 |
|
65 | function getTarget(app, secure) {
|
66 | var instances = client.getInstancesByAppId(app);
|
67 |
|
68 | for (var index in instances) {
|
69 | if (instances[index].status === "UP") {
|
70 | return (secure === true) ? {"hostName": instances[index].hostName, "port": instances[index].securePort["$"]} : {"hostName": instances[index].hostName, "port":instances[index].port["$"]};
|
71 | }
|
72 | }
|
73 | return "";
|
74 | }
|
75 |
|
76 | return {
|
77 | onrequest: function(req, res, next) {
|
78 | var appInfo = getAppName(req.url);
|
79 | var endpoint = getTarget(appInfo.app, appInfo.secure);
|
80 |
|
81 | if (endpoint.hostName) {
|
82 | debug("target hostname: " + endpoint.hostName);
|
83 | req.targetHostname = endpoint.hostName;
|
84 | debug("target port: " + endpoint.port);
|
85 | req.targetPort = endpoint.port;
|
86 | req.targetPath = req.url;
|
87 | if (appInfo.secure) {
|
88 | req.targetSecure = true;
|
89 | } else {
|
90 | req.targetSecure = false;
|
91 | }
|
92 | } else {
|
93 | logger.eventLog({level:'warn', req: req, res: res, err:null, component:LOG_TAG_COMP }, "Target endpoint from Eureka not found");
|
94 | }
|
95 | next();
|
96 | }
|
97 | };
|
98 | }
|