grain/relay/utils/loadConfig.go

63 lines
1.4 KiB
Go
Raw Normal View History

package utils
import (
2024-07-20 02:08:27 +00:00
"os"
"gopkg.in/yaml.v2"
)
2024-07-25 20:43:46 +00:00
type Config struct {
MongoDB struct {
URI string `yaml:"uri"`
Database string `yaml:"database"`
} `yaml:"mongodb"`
Server struct {
Port string `yaml:"port"`
} `yaml:"server"`
RateLimit RateLimitConfig `yaml:"rate_limit"`
}
type LimitBurst struct {
Limit float64 `yaml:"limit"`
Burst int `yaml:"burst"`
}
2024-07-25 03:04:26 +00:00
type RateLimitConfig struct {
WsLimit float64 `yaml:"ws_limit"`
WsBurst int `yaml:"ws_burst"`
EventLimit float64 `yaml:"event_limit"`
EventBurst int `yaml:"event_burst"`
2024-07-26 14:02:34 +00:00
ReqLimit float64 `yaml:"req_limit"`
ReqBurst int `yaml:"req_burst"`
2024-07-25 13:57:24 +00:00
CategoryLimits map[string]KindLimitConfig `yaml:"category_limits"`
2024-07-25 20:43:46 +00:00
KindLimits []KindLimitConfig `yaml:"kind_limits"`
2024-07-25 03:04:26 +00:00
}
type CategoryLimitConfig struct {
Regular LimitBurst `yaml:"regular"`
Replaceable LimitBurst `yaml:"replaceable"`
ParameterizedReplaceable LimitBurst `yaml:"parameterized_replaceable"`
Ephemeral LimitBurst `yaml:"ephemeral"`
}
2024-07-25 20:43:46 +00:00
type KindLimitConfig struct {
Kind int `yaml:"kind"`
Limit float64 `yaml:"limit"`
Burst int `yaml:"burst"`
}
func LoadConfig(filename string) (*Config, error) {
2024-07-20 02:08:27 +00:00
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}