Skip to content

Commit

Permalink
env fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajiv Geraev committed May 29, 2024
1 parent 68926b4 commit b685cfb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
12 changes: 9 additions & 3 deletions config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"context"
"log"
"os"
"time"

"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -14,17 +15,22 @@ var (
)

func ConnectDB() {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost:27017"))
mongoURL := os.Getenv("MONGO_URL")
if mongoURL == "" {
log.Fatal("MONGO_URL is not set in environment variables")
}

client, err := mongo.NewClient(options.Client().ApplyURI(mongoURL))
if err != nil {
log.Fatal(err)
log.Fatal("Error creating MongoDB client: ", err)
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
log.Fatal("Error connecting to MongoDB: ", err)
}

MongoDB = client.Database("testdb")
Expand Down
42 changes: 42 additions & 0 deletions invitation_seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"context"
"log"
"os"
"time"

"github.com/rajivgeraev/invitations/config"
"github.com/rajivgeraev/invitations/models"
"go.mongodb.org/mongo-driver/bson/primitive"
)

func main() {
// Подключение к базе данных
mongoURL := os.Getenv("MONGO_URL")
if mongoURL == "" {
log.Fatal("MONGO_URL is not set in environment variables")
}

config.ConnectDB()

collection := config.MongoDB.Collection("invitations")

invitations := []models.Invitation{
{ID: primitive.NewObjectID(), Code: "twitter-reg1", MaxUses: 100, UsedCount: 0, Emails: []string{}},
{ID: primitive.NewObjectID(), Code: "telegram-test", MaxUses: 100, UsedCount: 0, Emails: []string{}},
{ID: primitive.NewObjectID(), Code: "instagram-hello", MaxUses: 100, UsedCount: 0, Emails: []string{}},
}

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

for _, invitation := range invitations {
_, err := collection.InsertOne(ctx, invitation)
if err != nil {
log.Fatal("Error inserting invitation: ", err)
}
}

log.Println("Data seeded successfully!")
}
14 changes: 9 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ package main
import (
"log"
"net/http"
"os"

"github.com/gorilla/mux"
"github.com/joho/godotenv"

"github.com/rajivgeraev/invitations/config"
"github.com/rajivgeraev/invitations/routes"
)

func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
// Подключение к базе данных
mongoURL := os.Getenv("MONGO_URL")
if mongoURL == "" {
log.Fatal("MONGO_URL is not set in environment variables")
}

router := mux.NewRouter()
config.ConnectDB()

router := mux.NewRouter()
routes.InitializeRoutes(router)

log.Println("Server started on port 8080")
log.Fatal(http.ListenAndServe(":8080", router))
}

0 comments on commit b685cfb

Please sign in to comment.