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"` Burst int `yaml:"burst"`
} }
func LoadConfiguration() (*Config, error) {
return LoadConfig("config.yml")
}
func LoadConfig(filename string) (*Config, error) { func LoadConfig(filename string) (*Config, error) {
data, err := os.ReadFile(filename) data, err := os.ReadFile(filename)
if err != nil { if err != nil {
@ -70,4 +66,4 @@ func LoadConfig(filename string) (*Config, error) {
} }
return &config, nil return &config, nil
} }

11
main.go
View File

@ -2,28 +2,27 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http"
"grain/config" "grain/config"
"grain/relay" "grain/relay"
"grain/relay/db" "grain/relay/db"
"grain/web" "grain/web"
"log"
"net/http"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
func main() { func main() {
cfg, err := config.LoadConfiguration() cfg, err := config.LoadConfig("config.yml")
if err != nil { if err != nil {
log.Fatal("Error loading config: ", err) log.Fatal("Error loading config: ", err)
} }
err = db.InitializeDatabase(cfg) client, err := db.InitDB(cfg)
if err != nil { if err != nil {
log.Fatal("Error initializing database: ", err) log.Fatal("Error initializing database: ", err)
} }
defer db.DisconnectDB() defer db.DisconnectDB(client)
config.SetupRateLimiter(cfg) config.SetupRateLimiter(cfg)
config.SetupSizeLimiter(cfg) config.SetupSizeLimiter(cfg)

View File

@ -17,21 +17,14 @@ var collections = make(map[int]*mongo.Collection)
func GetClient() *mongo.Client { func GetClient() *mongo.Client {
return client return client
} }
func InitializeDatabase(config *config.Config) error { func InitDB(cfg *config.Config) (*mongo.Client, error) {
_, err := InitDB(config.MongoDB.URI, config.MongoDB.Database) clientOptions := options.Client().ApplyURI(cfg.MongoDB.URI)
return err
}
// Initialize MongoDB client
func InitDB(uri, database string) (*mongo.Client, error) {
clientOptions := options.Client().ApplyURI(uri)
var err error var err error
client, err = mongo.Connect(context.TODO(), clientOptions) client, err = mongo.Connect(context.TODO(), clientOptions)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Check the connection
err = client.Ping(context.TODO(), nil) err = client.Ping(context.TODO(), nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -61,8 +54,9 @@ func GetCollection(kind int) *mongo.Collection {
} }
// Disconnect from MongoDB // Disconnect from MongoDB
func DisconnectDB() { func DisconnectDB(client *mongo.Client) {
if err := client.Disconnect(context.TODO()); err != nil { err := client.Disconnect(context.TODO())
if err != nil {
fmt.Println("Error disconnecting from MongoDB:", err) fmt.Println("Error disconnecting from MongoDB:", err)
} }
fmt.Println("Disconnected from MongoDB!") fmt.Println("Disconnected from MongoDB!")