RoadSign/pkg/navi/route.go

58 lines
1.5 KiB
Go
Raw Normal View History

2024-01-17 06:34:08 +00:00
package navi
2023-11-17 17:42:04 +00:00
import (
2024-01-30 16:57:47 +00:00
roadsign "code.smartsheep.studio/goatworks/roadsign/pkg"
2024-01-17 06:34:08 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/navi/transformers"
2024-01-30 16:57:47 +00:00
"fmt"
"github.com/spf13/viper"
2024-01-17 06:34:08 +00:00
"github.com/gofiber/fiber/v2"
2023-11-17 17:42:04 +00:00
)
2023-12-12 13:07:05 +00:00
type RoadApp struct {
2024-01-31 05:17:52 +00:00
Regions []*Region `json:"regions"`
Metrics *RoadMetrics `json:"metrics"`
2023-11-17 17:42:04 +00:00
}
2024-01-30 16:57:47 +00:00
func (v *RoadApp) Forward(c *fiber.Ctx, dest *Destination) error {
// Add reserve proxy headers
ip := c.IP()
scheme := c.Protocol()
protocol := string(c.Request().Header.Protocol())
c.Request().Header.Set(fiber.HeaderXForwardedFor, ip)
c.Request().Header.Set(fiber.HeaderXForwardedHost, ip)
c.Request().Header.Set(fiber.HeaderXForwardedProto, scheme)
c.Request().Header.Set(
fiber.HeaderVia,
fmt.Sprintf("%s %s", protocol, viper.GetString("central")),
)
c.Request().Header.Set(
fiber.HeaderForwarded,
fmt.Sprintf("by=%s; for=%s; host=%s; proto=%s", c.IP(), c.IP(), c.Get(fiber.HeaderHost), scheme),
)
// Response body
var err error
2024-01-24 16:09:39 +00:00
switch dest.GetType() {
case DestinationHypertext:
2024-01-30 16:57:47 +00:00
err = makeUnifiedResponse(c, dest)
2024-01-24 16:09:39 +00:00
case DestinationStaticFile:
2024-01-30 16:57:47 +00:00
err = makeFileResponse(c, dest)
2023-11-17 17:42:04 +00:00
default:
2024-01-30 16:57:47 +00:00
err = fiber.ErrBadGateway
}
// Apply helmet
if dest.Helmet != nil {
dest.Helmet.Apply(c)
2023-11-17 17:42:04 +00:00
}
2024-01-30 16:57:47 +00:00
// Apply watermark
c.Response().Header.Set(fiber.HeaderServer, "RoadSign")
c.Response().Header.Set(fiber.HeaderXPoweredBy, fmt.Sprintf("RoadSign %s", roadsign.AppVersion))
return err
2023-11-17 17:42:04 +00:00
}
2024-01-24 16:09:39 +00:00
type RequestTransformerConfig = transformers.TransformerConfig