2023-11-17 17:42:04 +00:00
|
|
|
package sign
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math/rand"
|
2023-11-25 15:00:34 +00:00
|
|
|
|
|
|
|
"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:
|
2023-11-25 15:00:34 +00:00
|
|
|
return makeHypertextResponse(ctx, upstream)
|
2023-11-17 17:42:04 +00:00
|
|
|
case UpstreamTypeFile:
|
2023-11-25 15:00:34 +00:00
|
|
|
return makeFileResponse(ctx, upstream)
|
2023-11-17 17:42:04 +00:00
|
|
|
default:
|
|
|
|
return fiber.ErrBadGateway
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SiteConfig struct {
|
2023-11-25 15:00:34 +00:00
|
|
|
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"`
|
|
|
|
}
|