diff --git a/config/loadConfig.go b/config/loadConfig.go new file mode 100644 index 0000000..12fd2a9 --- /dev/null +++ b/config/loadConfig.go @@ -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 +} \ No newline at end of file diff --git a/config/rateLimiter.go b/config/rateLimiter.go index a8a6a14..8c39d97 100644 --- a/config/rateLimiter.go +++ b/config/rateLimiter.go @@ -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, diff --git a/config/sizeLimiter.go b/config/sizeLimiter.go index b50987b..48b9b62 100644 --- a/config/sizeLimiter.go +++ b/config/sizeLimiter.go @@ -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) diff --git a/config/load.go b/config/types/new.go similarity index 53% rename from config/load.go rename to config/types/new.go index 14920cc..c9c492a 100644 --- a/config/load.go +++ b/config/types/new.go @@ -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 } \ No newline at end of file diff --git a/main.go b/main.go index 317eb0c..0bb342b 100644 --- a/main.go +++ b/main.go @@ -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 { diff --git a/relay/db/db.go b/relay/db/db.go index 1b961ba..9709075 100644 --- a/relay/db/db.go +++ b/relay/db/db.go @@ -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)