RoadSign/pkg/navi/route.go

50 lines
1.3 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 (
"errors"
"math/rand"
2024-01-17 06:34:08 +00:00
"code.smartsheep.studio/goatworks/roadsign/pkg/navi/transformers"
"github.com/gofiber/fiber/v2"
2023-11-17 17:42:04 +00:00
)
2023-12-12 13:07:05 +00:00
type RoadApp struct {
2023-12-10 03:30:31 +00:00
Sites []*SiteConfig `json:"sites"`
2023-11-17 17:42:04 +00:00
}
2023-12-12 13:07:05 +00:00
func (v *RoadApp) Forward(ctx *fiber.Ctx, site *SiteConfig) error {
2023-11-17 17:42:04 +00:00
if len(site.Upstreams) == 0 {
return errors.New("invalid configuration")
}
2023-12-10 03:30:31 +00:00
// Do forward
2023-11-17 17:42:04 +00:00
idx := rand.Intn(len(site.Upstreams))
2023-12-10 03:30:31 +00:00
upstream := site.Upstreams[idx]
2023-11-17 17:42:04 +00:00
switch upstream.GetType() {
case UpstreamTypeHypertext:
return makeHypertextResponse(ctx, upstream)
2023-11-17 17:42:04 +00:00
case UpstreamTypeFile:
return makeFileResponse(ctx, upstream)
2023-11-17 17:42:04 +00:00
default:
return fiber.ErrBadGateway
}
}
type RequestTransformerConfig = transformers.RequestTransformerConfig
2023-11-17 17:42:04 +00:00
type SiteConfig struct {
2023-12-10 03:30:31 +00:00
ID string `json:"id"`
2024-01-01 10:07:21 +00:00
Rules []*RouterRule `json:"rules" yaml:"rules"`
2023-12-10 03:30:31 +00:00
Transformers []*RequestTransformerConfig `json:"transformers" yaml:"transformers"`
2024-01-01 10:07:21 +00:00
Upstreams []*UpstreamInstance `json:"upstreams" yaml:"upstreams"`
2023-11-17 17:42:04 +00:00
}
2024-01-01 10:07:21 +00:00
type RouterRule struct {
2023-12-10 03:30:31 +00:00
Host []string `json:"host" yaml:"host"`
Path []string `json:"path" yaml:"path"`
Queries map[string]string `json:"queries" yaml:"queries"`
Headers map[string][]string `json:"headers" yaml:"headers"`
2023-11-17 17:42:04 +00:00
}