config types split out

This commit is contained in:
0ceanSlim 2024-07-31 11:56:55 -04:00
parent 107edacc04
commit 4c0dced370
6 changed files with 47 additions and 36 deletions

27
config/loadConfig.go Normal file
View File

@ -0,0 +1,27 @@
package config
import (
"os"
config "grain/config/types"
"gopkg.in/yaml.v2"
)
func LoadConfig(filename string) (*config.ServerConfig, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var config config.ServerConfig
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, err
}
return &config, nil
}

View File

@ -2,6 +2,7 @@ package config
import (
"fmt"
config "grain/config/types"
"sync"
"golang.org/x/time/rate"
@ -31,7 +32,7 @@ type RateLimiter struct {
var rateLimiterInstance *RateLimiter
var once sync.Once
func SetupRateLimiter(cfg *Config) {
func SetupRateLimiter(cfg *config.ServerConfig) {
rateLimiter := NewRateLimiter(
rate.Limit(cfg.RateLimit.WsLimit),
cfg.RateLimit.WsBurst,

View File

@ -2,6 +2,8 @@ package config
import (
"sync"
config "grain/config/types"
)
type SizeLimiter struct {
@ -10,7 +12,7 @@ type SizeLimiter struct {
mu sync.RWMutex
}
func SetupSizeLimiter(cfg *Config) {
func SetupSizeLimiter(cfg *config.ServerConfig) {
sizeLimiter := NewSizeLimiter(cfg.RateLimit.MaxEventSize)
for _, kindSizeLimit := range cfg.RateLimit.KindSizeLimits {
sizeLimiter.AddKindSizeLimit(kindSizeLimit.Kind, kindSizeLimit.MaxSize)

View File

@ -1,12 +1,6 @@
package config
import (
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
type ServerConfig struct {
MongoDB struct {
URI string `yaml:"uri"`
Database string `yaml:"database"`
@ -28,16 +22,16 @@ type KindSizeLimitConfig struct {
}
type RateLimitConfig struct {
WsLimit float64 `yaml:"ws_limit"`
WsBurst int `yaml:"ws_burst"`
EventLimit float64 `yaml:"event_limit"`
EventBurst int `yaml:"event_burst"`
ReqLimit float64 `yaml:"req_limit"`
ReqBurst int `yaml:"req_burst"`
MaxEventSize int `yaml:"max_event_size"`
KindSizeLimits []KindSizeLimitConfig `yaml:"kind_size_limits"`
WsLimit float64 `yaml:"ws_limit"`
WsBurst int `yaml:"ws_burst"`
EventLimit float64 `yaml:"event_limit"`
EventBurst int `yaml:"event_burst"`
ReqLimit float64 `yaml:"req_limit"`
ReqBurst int `yaml:"req_burst"`
MaxEventSize int `yaml:"max_event_size"`
KindSizeLimits []KindSizeLimitConfig `yaml:"kind_size_limits"`
CategoryLimits map[string]KindLimitConfig `yaml:"category_limits"`
KindLimits []KindLimitConfig `yaml:"kind_limits"`
KindLimits []KindLimitConfig `yaml:"kind_limits"`
}
type CategoryLimitConfig struct {
@ -51,19 +45,4 @@ type KindLimitConfig struct {
Kind int `yaml:"kind"`
Limit float64 `yaml:"limit"`
Burst int `yaml:"burst"`
}
func LoadConfig(filename string) (*Config, error) {
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
}

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"grain/config"
configTypes "grain/config/types"
"grain/relay"
"grain/relay/db"
"grain/web"
@ -47,7 +48,7 @@ func setupRoutes() *http.ServeMux {
return mux
}
func startServer(config *config.Config, mux *http.ServeMux) {
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) {
fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
err := http.ListenAndServe(config.Server.Port, mux)
if err != nil {

View File

@ -3,7 +3,8 @@ package db
import (
"context"
"fmt"
"grain/config"
config "grain/config/types"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
@ -17,7 +18,7 @@ var collections = make(map[int]*mongo.Collection)
func GetClient() *mongo.Client {
return client
}
func InitDB(cfg *config.Config) (*mongo.Client, error) {
func InitDB(cfg *config.ServerConfig) (*mongo.Client, error) {
clientOptions := options.Client().ApplyURI(cfg.MongoDB.URI)
var err error
client, err = mongo.Connect(context.TODO(), clientOptions)