✨ Forwarding requests
This commit is contained in:
@ -3,7 +3,10 @@ package api
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/proxy"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/samber/lo"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func listExistsService(c *fiber.Ctx) error {
|
||||
@ -17,3 +20,27 @@ func listExistsService(c *fiber.Ctx) error {
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
func forwardServiceRequest(c *fiber.Ctx) error {
|
||||
serviceType := c.Params("service")
|
||||
|
||||
service := directory.GetServiceInstanceByType(serviceType)
|
||||
|
||||
if service == nil || service.HttpAddr == nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, "service not found")
|
||||
}
|
||||
|
||||
ogUrl := c.Request().URI().String()
|
||||
url := c.OriginalURL()
|
||||
url = strings.Replace(url, "/srv/"+serviceType, "", 1)
|
||||
url = "http://" + *service.HttpAddr + url
|
||||
|
||||
log.Debug().
|
||||
Str("from", ogUrl).
|
||||
Str("to", url).
|
||||
Str("service", serviceType).
|
||||
Str("id", service.ID).
|
||||
Msg("Forwarding request for service...")
|
||||
|
||||
return proxy.Do(c, url)
|
||||
}
|
||||
|
Reference in New Issue
Block a user