Charrette
Charrette

Reputation: 720

How to handle proxying to multiple services with golang and labstack echo

I'm new to Go and Micro-services development, but I hope my question will make sense:

Let's say I have a micro-service handling users actions such as creating, showing & updating an user, available at localhost:8081/users.

Beside that, I have a micro-service handlings events' creation, show & update as well, available at localhost:8082/events.

And, above that, there is a gateway, available at localhost:8080 which is suppose to act as a proxy to dispatch the incoming request to the right service.

I found this piece of code which is working well to redirect from my gateway to my user's service:

proxy := httputil.NewSingleHostReverseProxy(&url.URL{
    Scheme: "http",
    Host:   "localhost:8081",
})
http.ListenAndServe(":8080", proxy)

But two things are bothering me:

Upvotes: 6

Views: 3318

Answers (1)

Charrette
Charrette

Reputation: 720

Thanks to an answer on an issue I posted on labstack/echo github, here is the solution:

httputil.ReverseProxy implements http.Handler, so you can do something like:

e := echo.New()
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
    Scheme: "http",
    Host:   "localhost:8081",
})
e.Any("/users", echo.WrapHandler(proxy))
e.Start(":8080")

Upvotes: 7

Related Questions