2024-07-14 12:25:30 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-08-17 15:43:21 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-07-16 10:07:48 +00:00
|
|
|
"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")
|
|
|
|
|
2024-08-17 15:43:21 +00:00
|
|
|
aliasingMap := viper.GetStringMapString("services.alias")
|
|
|
|
if val, ok := aliasingMap[serviceType]; ok {
|
|
|
|
serviceType = val
|
|
|
|
}
|
|
|
|
|
2024-07-14 14:41:14 +00:00
|
|
|
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
|
2024-07-16 10:07:48 +00:00
|
|
|
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)
|
|
|
|
}
|