Added Login and Create Account

This commit is contained in:
Gabe Farrell 2023-03-27 19:49:32 -04:00
parent 197afc4f59
commit 272e4e43b1
7 changed files with 334 additions and 0 deletions

40
db/db.go Normal file
View 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
}