mirror of
https://github.com/gabehf/BudgetBuddy.git
synced 2026-03-07 21:48:14 -08:00
Added Login and Create Account
This commit is contained in:
parent
197afc4f59
commit
272e4e43b1
7 changed files with 334 additions and 0 deletions
40
db/db.go
Normal file
40
db/db.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := godotenv.Load(".env")
|
||||
if err != nil {
|
||||
fmt.Println("* No .env file found")
|
||||
}
|
||||
Connect()
|
||||
}
|
||||
|
||||
var Client *mongo.Client
|
||||
|
||||
func Connect() {
|
||||
dbUsername := os.Getenv("DB_USERNAME")
|
||||
dbPassword := os.Getenv("DB_PASSWORD")
|
||||
|
||||
serverAPIOptions := options.ServerAPI(options.ServerAPIVersion1)
|
||||
clientOptions := options.Client().
|
||||
ApplyURI("mongodb+srv://" + dbUsername + ":" + dbPassword + "@budgetbuddy.3doyojf.mongodb.net/?retryWrites=true&w=majority").
|
||||
SetServerAPIOptions(serverAPIOptions)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
client, err := mongo.Connect(ctx, clientOptions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
Client = client
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue