RoadSign/pkg/sign/router.go

45 lines
1.0 KiB
Go
Raw Normal View History

2023-11-17 17:42:04 +00:00
package sign
import (
"errors"
"math/rand"
"github.com/gofiber/fiber/v2"
2023-11-17 17:42:04 +00:00
)
type AppConfig struct {
Sites []SiteConfig `json:"sites"`
}
func (v *AppConfig) Forward(ctx *fiber.Ctx, site SiteConfig) error {
if len(site.Upstreams) == 0 {
return errors.New("invalid configuration")
}
idx := rand.Intn(len(site.Upstreams))
upstream := &site.Upstreams[idx]
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 SiteConfig struct {
ID string `json:"id"`
2023-11-17 17:42:04 +00:00
Rules []RouterRuleConfig `json:"rules"`
Transformers []RequestTransformerConfig `json:"transformers"`
Upstreams []UpstreamConfig `json:"upstreams"`
}
type RouterRuleConfig struct {
Host []string `json:"host"`
Path []string `json:"path"`
Queries map[string]string `json:"query"`
Headers map[string][]string `json:"headers"`
}