mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-29 17:16:31 +00:00
removed redundancies
This commit is contained in:
parent
8c2e4ed926
commit
107edacc04
@ -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
11
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)
|
||||
|
@ -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!")
|
||||
|
Loading…
Reference in New Issue
Block a user