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

View File

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

View File

@ -1,12 +1,6 @@
package config package config
import ( type ServerConfig struct {
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
MongoDB struct { MongoDB struct {
URI string `yaml:"uri"` URI string `yaml:"uri"`
Database string `yaml:"database"` Database string `yaml:"database"`
@ -28,16 +22,16 @@ type KindSizeLimitConfig struct {
} }
type RateLimitConfig struct { type RateLimitConfig struct {
WsLimit float64 `yaml:"ws_limit"` WsLimit float64 `yaml:"ws_limit"`
WsBurst int `yaml:"ws_burst"` WsBurst int `yaml:"ws_burst"`
EventLimit float64 `yaml:"event_limit"` EventLimit float64 `yaml:"event_limit"`
EventBurst int `yaml:"event_burst"` EventBurst int `yaml:"event_burst"`
ReqLimit float64 `yaml:"req_limit"` ReqLimit float64 `yaml:"req_limit"`
ReqBurst int `yaml:"req_burst"` ReqBurst int `yaml:"req_burst"`
MaxEventSize int `yaml:"max_event_size"` MaxEventSize int `yaml:"max_event_size"`
KindSizeLimits []KindSizeLimitConfig `yaml:"kind_size_limits"` KindSizeLimits []KindSizeLimitConfig `yaml:"kind_size_limits"`
CategoryLimits map[string]KindLimitConfig `yaml:"category_limits"` CategoryLimits map[string]KindLimitConfig `yaml:"category_limits"`
KindLimits []KindLimitConfig `yaml:"kind_limits"` KindLimits []KindLimitConfig `yaml:"kind_limits"`
} }
type CategoryLimitConfig struct { type CategoryLimitConfig struct {
@ -52,18 +46,3 @@ type KindLimitConfig struct {
Limit float64 `yaml:"limit"` Limit float64 `yaml:"limit"`
Burst int `yaml:"burst"` 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 ( import (
"fmt" "fmt"
"grain/config" "grain/config"
configTypes "grain/config/types"
"grain/relay" "grain/relay"
"grain/relay/db" "grain/relay/db"
"grain/web" "grain/web"
@ -47,7 +48,7 @@ func setupRoutes() *http.ServeMux {
return mux 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) fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
err := http.ListenAndServe(config.Server.Port, mux) err := http.ListenAndServe(config.Server.Port, mux)
if err != nil { if err != nil {

View File

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