Coder Social home page Coder Social logo

Comments (13)

adriendomoison avatar adriendomoison commented on May 4, 2024 3

@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.

kpacha avatar kpacha commented on May 4, 2024 2

@adriendomoison , @bartoszgolek, please check this PR: #103

from lura.

bartoszgolek avatar bartoszgolek commented on May 4, 2024 1

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.

vahid4mm avatar vahid4mm commented on May 4, 2024 1

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.

riibeirogabriel avatar riibeirogabriel commented on May 4, 2024 1

@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.

kpacha avatar kpacha commented on May 4, 2024

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.

kpacha avatar kpacha commented on May 4, 2024

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.

adriendomoison avatar adriendomoison commented on May 4, 2024

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.

kpacha avatar kpacha commented on May 4, 2024

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.Responses 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.

bartoszgolek avatar bartoszgolek commented on May 4, 2024

I have created POC for this: bartoszgolek@630996f

It's not complete, just showing the way of creating ability to manage merging

from lura.

kpacha avatar kpacha commented on May 4, 2024

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.

vladkras avatar vladkras commented on May 4, 2024

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.

github-actions avatar github-actions commented on May 4, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.