removed redundancies

This commit is contained in:
0ceanSlim 2024-07-31 11:28:41 -04:00
parent 8c2e4ed926
commit 107edacc04
3 changed files with 11 additions and 22 deletions

View File

@ -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
}
}

11
main.go
View File

@ -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)

View File

@ -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!")