Forwarding requests

This commit is contained in:
LittleSheep 2024-07-14 22:41:14 +08:00
parent d371c4fbfa
commit 82dd04f8aa
3 changed files with 34 additions and 1 deletions

View File

@ -53,9 +53,13 @@ func (v *HyperConn) KeepRegisterService() error {
time.Sleep(5 * time.Second)
client := health.NewHealthClient(v.dealerConn)
if _, err := client.Check(context.Background(), &health.HealthCheckRequest{}); err != nil {
return v.KeepRegisterService()
if v.KeepRegisterService() == nil {
break
}
}
}
return nil
}
func (v *HyperConn) GetServiceGrpcConn(t string) (*grpc.ClientConn, error) {

View File

@ -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)
}

View File

@ -12,6 +12,8 @@ func MapAPIs(app *fiber.App) {
wellKnown.Get("/directory/services", listExistsService)
}
app.All("/srv/:service/*", forwardServiceRequest)
api := app.Group("/api").Name("API")
{
api.Use(func(c *fiber.Ctx) error {