RoadSign/pkg/configurator/router.go

42 lines
1.0 KiB
Go
Raw Normal View History

2023-11-17 16:23:40 +00:00
package configurator
import (
"math/rand"
)
type AppConfig struct {
Sites []SiteConfig `json:"sites"`
}
func (v *AppConfig) LoadBalance(site SiteConfig) *UpstreamConfig {
idx := rand.Intn(len(site.Upstreams))
switch site.Upstreams[idx].GetType() {
case UpstreamTypeHypertext:
return &site.Upstreams[idx]
case UpstreamTypeFile:
// TODO Make this into hypertext configuration
return &site.Upstreams[idx]
default:
// Give him the null value when this configuration is invalid.
// Then we can print a log in the console to warm him.
return nil
}
}
type SiteConfig struct {
ID string `json:"id"`
Name string `json:"name"`
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"`
}