Coder Social home page Coder Social logo

Comments (7)

erikdubbelboer avatar erikdubbelboer commented on June 11, 2024 1

I don't think it happens very often that someone using a http handler will still also want to run fasthttp code. That's why you're the first one to have this issue. I'm afraid I don't have time to fix this now, but a pull request is always welcome.

from fasthttp.

erikdubbelboer avatar erikdubbelboer commented on June 11, 2024

The handler sets the status code which is captured by netHTTPResponseWriter and then line 65 propagates that to the Response.

Can you show an example of something going wrong?

from fasthttp.

luisgarciaalanis avatar luisgarciaalanis commented on June 11, 2024

The handler sets the status code which is captured by netHTTPResponseWriter and then line 65 propagates that to the Response.

Can you show an example of something going wrong?

netHTTPResponseWriter is missing the initialization of statusCode, so when it propagates it propagates a nil value.
its not setting the context status that comes from the caller.

from fasthttp.

erikdubbelboer avatar erikdubbelboer commented on June 11, 2024

But that's no problem, cause in net/http not setting a status code means 200 and that's exactly what it is doing here:

if w.statusCode == 0 {
return http.StatusOK
}

Or do you mean that you are setting a status code before you call the handler returned by NewFastHTTPHandler and want it to use that?

Like I said, show me an example of this going wrong.

from fasthttp.

luisgarciaalanis avatar luisgarciaalanis commented on June 11, 2024

But that's no problem, cause in net/http not setting a status code means 200 and that's exactly what it is doing here:

if w.statusCode == 0 {
return http.StatusOK
}

Or do you mean that you are setting a status code before you call the handler returned by NewFastHTTPHandler and want it to use that?

Like I said, show me an example of this going wrong.

Yes I mean to set the status code before calling the HTTPHandler. It needs to be set on line 59.

// For example imagine a Login handler that wants to set a status
// HTMX handles status to render differently, for example.
func LoginHandler(c *fiber.Ctx) error {
	
	// ........
	if isBadRequest {
		c.Status(http.StatusBadRequest)  <---- this will be converted to 200 always.
		return Render(c, dialog.BadRequest())
	}

	return Render(c, pages.Login())
}

func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error {
	componentHandler := templ.Handler(component)
	for _, o := range options {
		o(componentHandler)
	}
	return adaptor.HTTPHandler(componentHandler)(c)
}```

from fasthttp.

erikdubbelboer avatar erikdubbelboer commented on June 11, 2024

Interesting use case. But in that case can't you just turn it around and do this?

	if isBadRequest {
		Render(c, dialog.BadRequest())
		c.Status(http.StatusBadRequest)
	}

from fasthttp.

luisgarciaalanis avatar luisgarciaalanis commented on June 11, 2024

You might not know where the Status was set, if the HTTPHandler will reset it you need to get a copy beforehand to set it again afterwards. I found this issue while debugging Templ code using go.fiber, I don't control the adaptor, I just use it on my project.
https://github.com/a-h/templ/blob/main/examples/integration-gofiber/main.go

I think it should just respect the Status, the code above I just wrote it as an example.

from fasthttp.

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.