Comments (13)
@kpacha do you have an example or a documentation for that? I read the code and it did not look very trivial to inject my own HTTPStatusHandler
.
I would need to use NewHTTPProxyDetailed(remote *config.Backend, requestExecutor HTTPRequestExecutor, ch HTTPStatusHandler, rp HTTPResponseParser) Proxy
right? Then what is the correct way to generate the config.Backend, the HTTPRequestExecutor and the HTTPResponseParser?
And then will I be able to just get the generated proxy and do routerFactory := gin.DefaultFactory(proxy.DefaultFactory(logger), logger)
Thanks!
from lura.
@adriendomoison , @bartoszgolek, please check this PR: #103
from lura.
Checkout github.com/devopsfaith/krakend/proxy/merging.go:33
and github.com/devopsfaith/krakend/proxy/merging.go:80
, here is the logic of merging responses. I would sugest to extract combineData to interface and provide it trough parameter, then You would be able to provide StatusHandler not throwing error and here provide "merger" which merge responses and statuses.
If You would like to wait till monday, I would like to prepare pull request for this.
from lura.
Hi, so long story short, if I want to send the response the service is providing to the user through the krakend gateway, I need to inject code?
from lura.
@kpacha I can use the no-op
only to backend responses? reading the docs about (https://www.krakend.io/docs/endpoints/no-op/) sounds that no-op
needs be setted in both ways (in requests and responses), i set the no-op
in backend thinking work only in responses but this not work, there is a way to use only in backend response, not in request?
from lura.
The status 0 is a known 'feature' of the cache package included in the gin example (https://github.com/devopsfaith/krakend/blob/master/examples/gin/main.go#L68)
Regarding the bigger than 200 status: by default, the gateway will always send a HTTP status 200 if the backend returns a 200 or a 201, and a 500 for any status above 400
https://github.com/devopsfaith/krakend/blob/master/proxy/http_status.go
You can inject your own HTTPStatusHandler
implementation if you need a different behaviour.
cheers!
from lura.
check out the tests and the krakend-ce repo for examples of how to build the proxy stack.
if you are not using the default factories, you must go with the 'explicit' constructors... something like this:
customStatusHandler := ...
backendFactory := func(backendCfg *config.Backend) proxy.Proxy {
// the default request executor
re := proxy.DefaultHTTPRequestExecutor(proxy. NewHTTPClient)
// default entity formatter for the given backend
ef := proxy.NewEntityFormatter(backendCfg.Target, backendCfg.Whitelist, backendCfg.Blacklist, backendCfg.Group, backendCfg.Mapping)
// the default response parser with the required config
rp := proxy.DefaultHTTPResponseParserFactory(proxy.HTTPResponseParserConfig{backendCfg.Decoder, ef})
// build and return the new backend proxy
return proxy.NewHTTPProxyDetailed(backend, re, customStatusHandler, rp)
}
// build the pipes on top of the custom backend factory
proxyFactory := proxy.NewDefaultFactory(backendFactory, logger)
// create the router factory with the custom proxy factory
routerFactory := gin.DefaultFactory(proxyFactory, logger)
The backendCfg
(of type *config.Backend
) argument is automatically generated from the config file and injected into the factories when calling routerFactory.NewWithContext(ctx).Run(cfg)
from lura.
Yuhu! Getting there! One last question @kpacha : is there a way to have access to the original headers of the responses (generated by my services) when injecting a HandlerFactory: func(configuration *config.EndpointConfig, proxy proxy.Proxy) gin.HandlerFunc
?
I was able to figure out that this was the main handler I had to work on and inject to get the result I need thanks to your help 👍
My goal here is still to be able to decide what kind of status I want to return to the client (e.g. to be able to return a 201 when something was created as much as a 409 if there were a conflict). So far, without access to the list of headers from the requests, the only thing I could do is to modify all the responses from the services and let them return some additional information about what they did and what happened, then decide what status to set and after blacklist this additional data.
I am curious, how did you imagined and designed Krakend to be used for this kind of scenario? I would really like to follow the idea and the design :)
from lura.
I have some minor remarks here:
1- the proxy.HTTPStatusHandler
is responsible of dealing with the status code of the backend response. And it has full access to the http response and its headers for extra verifications.
2- as @bartoszgolek commented, the proxy.Response
s from all the backends in a pipe are merged into a single one. If your endpoint is dealing with multiple backends, the headers will get lost using the default response combiner/merger. But if your endpoint consumes just a single backend, the headers will be passed through the proxy pipe to your router.EndpointHandler
. There is a issue for adding a 'combiner register and factory' so they can be defined per endpoint in the config file (#78 )
3- even when your backend headers arrive to the router.EndpointHandler
, the default one won't use them to build the final response. You must create a custom one and inject it in your stack.
As a final note, we created the KrakenD to be used as a pure, state-less API gateway (http://www.krakend.io/blog/what-is-an-api-gateway/). So I'm not sure if your use case fits in that classification. But this doesn't mean you shouldn't do it.
from lura.
I have created POC for this: bartoszgolek@630996f
It's not complete, just showing the way of creating ability to manage merging
from lura.
here you have the complete PoC I was working on two months ago: https://github.com/devopsfaith/krakend/blob/raw_encoding/proxy/merging.go
from lura.
you can use return_error_details
(https://www.krakend.io/docs/backends/detailed-errors/) to get status code and message in additional object, but krakend still sends only 200 or 500
or you can use no-op
feature
from lura.
This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.
from lura.
Related Issues (20)
- Control Plane/ Service Mesh Feature Request HOT 2
- GraphQL backend to support variables in the POST body for Queries HOT 2
- Probably me as newbie but I can't get the GraphQL proxy config to work HOT 3
- Race condition in shadowproxy HOT 1
- Default router behavior changing when the `router` configuration is present HOT 2
- Inconsistent interpolation behavior in various parts of the configuration HOT 1
- Metrics for HTTP-Client plugin not reported HOT 3
- Configurable SD protocol HOT 1
- Proposal: Ability to filter input_headers at the backend level HOT 1
- Log which endpoint failed to register HOT 2
- Remove restriction on multiple POSTS or other unsafe methods HOT 1
- Default router behavior changing when the router configuration is present HOT 1
- Where did the examples go? HOT 3
- Pass request `ctx` to modifier plugins HOT 1
- Gofiber Support HOT 2
- Plugin for running async operations HOT 1
- Unexpected Content-Type when setting return_error_code and return_error_msg HOT 5
- Host List Causing Addition of 'http://' in Front of Address and DNS Resolution Leading to 'No Such Host' Error HOT 1
- logger.Fatal received too many arguments HOT 1
- Mixed paths when performing multiple backend calls in parallel for a given Endpoint HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lura.