Helo all,
In your documentation Rewriting target URLs in plugins, it mentions:
You can override the default target URL dynamically in a plugin by modifying these variables in your plugin code: req.targetHostname and req.targetPath.
I have a plugin that needs to override the target URL for one set in a custom configuration.
I made some tests using the version:
current nodejs version is v6.10.1
current edgemicro version is 2.3.3
In here I'm passing a fixed new URL, but the final one would be caught from configuration:
var EDGEMICRO_HEALTH = '/health'
if (req.url === EDGEMICRO_HEALTH) {
debug('%s endpoint was called with mode %s', EDGEMICRO_HEALTH, _healthMode)
debug('request ==>', Object.keys(req))
debug('port ==>', req.targetPort)
debug('targetHostname before==> %s', req.targetHostname)
req.targetHostname = 'localhost:3000'
debug('targetHostname after ==> %s', req.targetHostname)
next()
}
I have a local app running in localhost:3000 returning '200 OK' just for testing, if i call "http://localhost:8000/health", I get in the logs:
plugin:healthcheck /health endpoint was called with mode forward +6s
plugin:healthcheck request ==> [ '_readableState',
'readable',
'domain',
'_events',
'_eventsCount',
'_maxListeners',
'socket',
'connection',
'httpVersionMajor',
'httpVersionMinor',
'httpVersion',
'complete',
'headers',
'rawHeaders',
'trailers',
'rawTrailers',
'upgrade',
'url',
'method',
'statusCode',
'statusMessage',
'client',
'_consuming',
'_dumped',
'reqUrl',
'_overrideHeaders',
'_headersToUnset',
'setOverrideHeader',
'unsetHeader',
'targetPath',
'targetHostname' ] +0ms
plugin:healthcheck port ==> undefined +3ms
plugin:healthcheck targetHostname before==> localhost +1ms
plugin:healthcheck targetHostname after ==> localhost:3000 +0ms
So, as you can see, I'm able to change the targetHostname adding the port, although there is no property in request object regarding to port itself. I tried to force a req.targetPort
to see, but its undefined. And printing the properties in request
object, there is nothing close to what i need.
The thing is, the answer from EMG after the call is:
gateway:main targetRequest error +3ms dc6cc1c0-2c6a-11e7-afe7-bde5ed24fc46 Error: getaddrinfo ENOTFOUND localhost:3000 localhost:3000:8080
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
gateway:main plugin analytics does not provide handler function for error_request +2ms
gateway:main plugin healthcheck-plugin does not provide handler function for error_request +1ms
gateway:errors Error: getaddrinfo ENOTFOUND localhost:3000 localhost:3000:8080
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26) +1ms
gateway:main sourceRequest close +8ms
As it can be seen, EMG keeps the original port (which in this case was 8080 as defined in apigee cloud - target endpoint is http://localhost:8080), so it concatenate the changed target hostname with the original port, so localhost:3000:8080
, which obviously does not work.
Also, i believe it works the same for the HTTP scheme..it seems that there is no way to change from HTTP to HTTPS and HTTPS to HTTP as well.
I need to change the full target endpoint. If in apigee cloud the target is:
http://someurl:8080
I need to be able to change for instance to:
https://otherurl:9090
Is that possible somehow? Or even, is there a way for you to provide a change where its possible to override the full target endpoint?
Thanks
Ciro