Dealer/pkg/internal/server/api/directory.go

52 lines
1.2 KiB
Go
Raw Normal View History

2024-07-14 12:25:30 +00:00
package api
import (
"strings"
2024-07-14 12:25:30 +00:00
"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory"
"github.com/gofiber/fiber/v2"
2024-07-14 14:41:14 +00:00
"github.com/gofiber/fiber/v2/middleware/proxy"
"github.com/rs/zerolog/log"
2024-07-14 12:25:30 +00:00
"github.com/samber/lo"
)
func listExistsService(c *fiber.Ctx) error {
services := directory.ListServiceInstance()
return c.JSON(lo.Map(services, func(item *directory.ServiceInstance, index int) map[string]any {
return map[string]any{
"id": item.ID,
"type": item.Type,
"label": item.Label,
}
}))
}
2024-07-14 14:41:14 +00:00
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()
2024-07-26 17:10:26 +00:00
prevUrl := url
url = strings.Replace(url, "/srv/"+serviceType, "/api", 1)
2024-07-26 17:10:26 +00:00
if prevUrl == url {
url = strings.Replace(url, "/cgi/"+serviceType, "/api", 1)
}
2024-07-14 14:41:14 +00:00
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)
}