2024-10-20 16:05:40 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2024-10-22 15:23:58 +00:00
|
|
|
"fmt"
|
2024-10-20 16:05:40 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/proxy"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func forwardService(c *fiber.Ctx) error {
|
|
|
|
serviceType := c.Params("service")
|
|
|
|
ogKeyword := serviceType
|
|
|
|
|
|
|
|
aliasingMap := viper.GetStringMapString("services.aliases")
|
|
|
|
if val, ok := aliasingMap[serviceType]; ok {
|
|
|
|
serviceType = val
|
|
|
|
}
|
|
|
|
|
|
|
|
service := directory.GetServiceInstanceByType(serviceType)
|
|
|
|
|
|
|
|
if service == nil || service.HttpAddr == nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "service not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
url := c.OriginalURL()
|
|
|
|
url = strings.Replace(url, "/cgi/"+ogKeyword, "", 1)
|
|
|
|
url = *service.HttpAddr + url
|
|
|
|
|
2024-10-22 15:23:58 +00:00
|
|
|
if tk, ok := c.Locals("nex_token").(string); ok {
|
2024-10-30 15:26:39 +00:00
|
|
|
c.Request().Header.Set(fiber.HeaderAuthorization, fmt.Sprintf("Bearer %s", tk))
|
2024-10-22 15:23:58 +00:00
|
|
|
} else {
|
2024-10-30 15:26:39 +00:00
|
|
|
c.Request().Header.Del(fiber.HeaderAuthorization)
|
2024-10-22 15:23:58 +00:00
|
|
|
}
|
|
|
|
|
2024-10-20 16:05:40 +00:00
|
|
|
return proxy.Do(c, url)
|
|
|
|
|
|
|
|
}
|