mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-21 16:17:13 +00:00
config types split out
This commit is contained in:
parent
107edacc04
commit
4c0dced370
27
config/loadConfig.go
Normal file
27
config/loadConfig.go
Normal 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
|
||||
}
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
3
main.go
3
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 {
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user