cloudwego / hertz

Go HTTP framework with high-performance and strong-extensibility for building micro-services.
https://www.cloudwego.io
Apache License 2.0
5.12k stars 496 forks source link

[FR] Handle uri directly instead of redirecting #1081

Open nanakura opened 5 months ago

nanakura commented 5 months ago

Is your feature request related to a problem? Please describe.

[#818](https://github.com/cloudwego/hertz/issues/818) related In some case, redirection may result in failure to obtain the correct response.

package main

import (
    "context"
    "net/http"

    "github.com/cloudwego/hertz/pkg/app"
    "github.com/cloudwego/hertz/pkg/app/server"
)

func main() {
    h := server.Default(
        server.WithMaxRequestBodySize(8<<20),
        server.WithHostPorts("0.0.0.0:8080"),
    )
    s3 := h.Group("s3")
    {

        s3.GET("/:xxx/", func(ctx context.Context, c *app.RequestContext) {
            c.XML(http.StatusOK, "/xxx")
        })
        s3.GET("/:xxx/:ccc/*key", func(ctx context.Context, c *app.RequestContext) {
            xxx := c.Param("xxx")
            ccc := c.Param("ccc")
            key := c.Param("key")
            c.XML(http.StatusOK, xxx+ccc+key)
        })
        s3.PUT("/:xxx/:ccc/*key", func(ctx context.Context, c *app.RequestContext) {
            c.XML(http.StatusOK, "ok")
        })
    }

    h.Spin()
}

image

Describe the solution you'd like

Please directly let the redirect target's handler handle this uri Just like fiber does image

package main

import (
    "github.com/gofiber/fiber/v2"
    "net/http"
)

func main() {
    app := fiber.New()
    s3 := app.Group("/s3")
    {
        s3.Get("/:xxx/", func(c *fiber.Ctx) error {
            return c.Status(http.StatusOK).SendString("/xxx")
        })
        s3.Get("/:xxx/:ccc/*", func(c *fiber.Ctx) error {
            xxx := c.Params("xxx")
            ccc := c.Params("ccc")
            key := c.Params("*")
            return c.Status(http.StatusOK).SendString(xxx + ccc + key)
        })
        s3.Put("/:xxx/:ccc/*", func(c *fiber.Ctx) error {
            return c.SendStatus(http.StatusOK)
        })
    }

    app.Listen(":8080")
}

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

Additional context

Add any other context or screenshots about the feature request here.

github-actions[bot] commented 5 months ago

This issue has been marked as invalid question, please give more information by following the issue template. The issue will be closed in 1 days if no further activity occurs.

li-jin-gou commented 5 months ago

Hello, Can you submit a PR and add an option to support this behavior? @nanakura

nanakura commented 5 months ago

Hello, Can you submit a PR and add an option to support this behavior? @nanakura

What should the name of this option be called? I roughly completed this option in my fork