From 107edacc046afb99dae47a94034638d93a70e191 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Wed, 31 Jul 2024 11:28:41 -0400 Subject: [PATCH] removed redundancies --- config/load.go | 6 +----- main.go | 11 +++++------ relay/db/db.go | 16 +++++----------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/config/load.go b/config/load.go index f94c098..14920cc 100644 --- a/config/load.go +++ b/config/load.go @@ -53,10 +53,6 @@ type KindLimitConfig struct { Burst int `yaml:"burst"` } -func LoadConfiguration() (*Config, error) { - return LoadConfig("config.yml") -} - func LoadConfig(filename string) (*Config, error) { data, err := os.ReadFile(filename) if err != nil { @@ -70,4 +66,4 @@ func LoadConfig(filename string) (*Config, error) { } return &config, nil -} +} \ No newline at end of file diff --git a/main.go b/main.go index e58d257..317eb0c 100644 --- a/main.go +++ b/main.go @@ -2,28 +2,27 @@ package main import ( "fmt" - "log" - "net/http" - "grain/config" "grain/relay" "grain/relay/db" "grain/web" + "log" + "net/http" "golang.org/x/net/websocket" ) func main() { - cfg, err := config.LoadConfiguration() + cfg, err := config.LoadConfig("config.yml") if err != nil { log.Fatal("Error loading config: ", err) } - err = db.InitializeDatabase(cfg) + client, err := db.InitDB(cfg) if err != nil { log.Fatal("Error initializing database: ", err) } - defer db.DisconnectDB() + defer db.DisconnectDB(client) config.SetupRateLimiter(cfg) config.SetupSizeLimiter(cfg) diff --git a/relay/db/db.go b/relay/db/db.go index eb4927b..1b961ba 100644 --- a/relay/db/db.go +++ b/relay/db/db.go @@ -17,21 +17,14 @@ var collections = make(map[int]*mongo.Collection) func GetClient() *mongo.Client { return client } -func InitializeDatabase(config *config.Config) error { - _, err := InitDB(config.MongoDB.URI, config.MongoDB.Database) - return err -} - -// Initialize MongoDB client -func InitDB(uri, database string) (*mongo.Client, error) { - clientOptions := options.Client().ApplyURI(uri) +func InitDB(cfg *config.Config) (*mongo.Client, error) { + clientOptions := options.Client().ApplyURI(cfg.MongoDB.URI) var err error client, err = mongo.Connect(context.TODO(), clientOptions) if err != nil { return nil, err } - // Check the connection err = client.Ping(context.TODO(), nil) if err != nil { return nil, err @@ -61,8 +54,9 @@ func GetCollection(kind int) *mongo.Collection { } // Disconnect from MongoDB -func DisconnectDB() { - if err := client.Disconnect(context.TODO()); err != nil { +func DisconnectDB(client *mongo.Client) { + err := client.Disconnect(context.TODO()) + if err != nil { fmt.Println("Error disconnecting from MongoDB:", err) } fmt.Println("Disconnected from MongoDB!")