Nexus/pkg/internal/http/api/forward.go

40 lines
929 B
Go
Raw Normal View History

2024-10-20 16:05:40 +00:00
package api
import (
"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
if tk, ok := c.Locals("nex_token").(string); ok {
c.Set(fiber.HeaderAuthorization, fmt.Sprintf("Bearer %s", tk))
} else {
c.Set(fiber.HeaderAuthorization, "")
}
2024-10-20 16:05:40 +00:00
return proxy.Do(c, url)
}