Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Andy Day
@adayNU_twitter
that would be a bug in your handler/middleware code
Martti T.
@aldas
    e.GET("/", func(c echo.Context) error {
        err := c.String(http.StatusOK, c.Path())
        c.Response().Flush()
        time.Sleep(5 * time.Second)
        return err
    })
you can do this to flush you response to the client
but i would not recommend that. if your requirement is that telemetry should be reported async then run telemetry part in coroutine in that middleware after next(c) returns.
Andy Day
@adayNU_twitter
Screen Shot 2021-08-31 at 11.58.53 AM.png
that snippet your provided takes 5 seconds to respond to the client
even this takes 5 seconds to return a response to the client:
e.GET("/", func(c echo.Context) error {
        c.Response().Writer.Write([]byte("hello"))

        time.Sleep(5 * time.Second)
        return nil
})
Martti T.
@aldas

seems so. even if you write directly to response writer the connection stays open till chain ends and go http.server coroutine ends that ServerHTTP call I think.

if you need to do stuff without affecting the client then you need to run that slow stuff in coroutine

    e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
        return func(c echo.Context) error {
            // gather stats before
            err := next(c)
            // gather telemetry after
            go func() {
                time.Sleep(1 * time.Second) // do work, and report without affecting client
                fmt.Println("reporting telemetry")
            }()
            return err
        }
    })
something like that
Martti T.
@aldas
this middleware allows you to do reporting without affecting clients response time
but you need to recover panics in that coroutine or whole application will shutdown/crash on panic
Andy Day
@adayNU_twitter
ok i suppose the best route is a to use a go routine then, will do
Pablo
@pablodz
Hello there, how can I redirect "/" to "/api/v1/docs/index.html" with echo?
Pablo
@pablodz
:(
Maksimilian
@trendeeze_twitter
I am using AWS Cognito for user authentication to my API. Obviously the user can spoof GET/POST/etc params. So far I have created a middleware to validate the JWT token, which gives me access to the true user values, for example their account ID. What's the best practice for validating these? I think I'd like to store the user's true details somewhere so that I can validate it based on the resource they are calling, but not sure if that belongs in the context or somewhere else. Any ideas?
Martti T.
@aldas
in context with c.Set(). This is standard way to pass information from middleware to middleware and to handler. Ala JWT uses it store token in request context https://github.com/labstack/echo/blob/7f502b1ff10913aeab28abab64f2bf45952c768d/middleware/jwt.go#L234
Hemant Saikia
@hemant-saikia

Re: https://echo.labstack.com/middleware/rate-limiter/

How do I use this? I want a basic rate limiter and hence using the following:
e.Use(middleware.RateLimiter(middleware.NewRateLimiterMemoryStore(4)))

ON compilation:
./ari.go:55:8: undefined: middleware.RateLimiter
./ari.go:55:31: undefined: middleware.NewRateLimiterMemoryStore

How do i import the RateLimiter function?
@aldas @trendeeze_twitter @pablodz @adayNU_twitter
Martti T.
@aldas
Are you missing middleware package import?
import (
    "github.com/labstack/echo/v4"
    "github.com/labstack/echo/v4/middleware"
)
Kamol Mavlonov
@kamoljan
Is any reason why echo using Transfer-Encoding: chunked? I am trying to disable it, but no luck. Updated to v4 version still the same.
Martti T.
@aldas
are you using Gzip middleware?
Kamol Mavlonov
@kamoljan
nope
Only this
e := echo.New()
    e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Kamol Mavlonov
@kamoljan
I disabled all middleware and DisableHTTP2=true, but still this chunked response.
Martti T.
@aldas
there is nothing special related to chunked in echo code. This is probably related to request headers and some logic inside go http package.
does that request have Content-Length header?
4 replies
José María Martín
@jose-martin-m
Hi! I'm using echo with oapi validate middleware to check incoming request parameters against a regex in an OpenApi specification file. When a parameter does not match the regex a message it's shown with the correct regex for that parameter. The problem I'm having is that the slashes in the regex string are escaped multiple times. I think that when opai_validate generates the error message it prints the regex with %q so the single slash is escaped there. And then when echo prints the message the same thing happens and each slash is escaped again, making a single slash into four. Does anyone have an idea on how to deal with this?
Martti T.
@aldas
and maybe another breakpoint here https://github.com/labstack/echo/blob/c6f0c667f145b5e5347ba812c9de5a5a4280bac5/echo.go#L371 and you probably will see here things go south
If you do not have experience with debugger then maybe https://www.digitalocean.com/community/tutorials/debugging-go-code-with-visual-studio-code helps. assuming you are using vscode.
José María Martín
@jose-martin-m
Thank you for your response @aldas , will take a look when I can and report back
José María Martín
@jose-martin-m
Part of the problem seems to be Echo returns the string raw without processing its characters...? I'm not entirely sure. But modifying the error message and inserting tab characters \t makes those characters appear directly in the response instead of being processed, so I'm guessing something happens there and an escaped backslash \\ in the string is returned as-is
Martti T.
@aldas
Assuming that error ends up in global error handler. then it is sent to client by that line https://github.com/labstack/echo/blob/c6f0c667f145b5e5347ba812c9de5a5a4280bac5/echo.go#L400 if that message is already been escaped some characters will be double escaped to be valid json string value.
José María Martín
@jose-martin-m
ohh, I see, so the JSON response needs to have the backslash escaped and that's why it appears two times
Maksimilian
@trendeeze_twitter
I am using c.JSON to return a json response, however the inner item of the response is in base64. Why would that happen?
the field that is returning as base64 is a []byte in the struct
Maksimilian
@trendeeze_twitter
alright I see, it doesn't like that, works if I use a nested struct instead of []byte
Martti T.
@aldas

see https://pkg.go.dev/encoding/json#Marshal

Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.

David Desmarais-Michaud
@davidmdm
I can't seem to find the documentation but do paths support other directives other than : ?
Does it support regexp to some degree?
Martti T.
@aldas
only : is considered as start of path parameter. path param name will be everything till next / or end of path.
Note: any param * is a special case of : and you can access any path value in handler/middleware by c.Param("*")
baldr9
@baldr9
How to use a gRPC service with echo framework. I have the same question from: https://stackoverflow.com/questions/67558481/how-to-use-grpc-with-golang-echo-framework
Martti T.
@aldas
I do not think you are able to. GRPC is binary protocol and is completely different from HTTP. There are maybe server implementation that can sniff traffic and do routing of request to GRPC or HTTP handlers.
just implement layered architecture in our application and design HTTP and GRPC handlers in your transport layer that extract their data and pass it t your business service layer.
search for "Onion Architecture" / "Hexagonal Architecture"
Martti T.
@aldas
quick googling gives me for Go https://blog.kinark.app/post/onion-architecture/ which seems decent article
1 reply
Andrey Grehov
@andreygrehov_twitter
Hello. Does Echo have some sort of a retry mechanism on panics?
I noticed a paniced request flow is being retried 3 times, can't understand why would that happen
Andrey Grehov
@andreygrehov_twitter
nvm, it's chrome's weird behavior..
Maksimilian
@trendeeze_twitter
how can I access the requestId from the context?
ali naseri
@alinaserinet
Hi dears