Skip to content

Commit

Permalink
use env config
Browse files Browse the repository at this point in the history
  • Loading branch information
VikashChauhan51 committed Jul 28, 2024
1 parent 4aa82d8 commit 5ff5669
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
16 changes: 5 additions & 11 deletions .env
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
PORT=:50051
DB_URL="host=localhost user=postgres dbname=booknow port=5432 password=testing"
JWT_SECRET_KEY=testing


DBHOST="localhost"
DBNAME="booknow"
DBUSER="postgres"
DBPORT=5432
DBPASSWORD=testing
SECRET_KEY="testing"
DB_USER=Sa
DB_PASSWORD=Welcome@123
DB_HOST=localhost
DB_PORT=1433
DB_NAME=bookstore
17 changes: 16 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@ package main

import (
"fmt"
"os"
"time"

"github.com/VikashChauhan51/go-sample-api/cmd/api/routes"
docs "github.com/VikashChauhan51/go-sample-api/docs"
"github.com/VikashChauhan51/go-sample-api/internal/core/entities"
"github.com/VikashChauhan51/go-sample-api/internal/infra/databases"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
swaggerfiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)

func loadEnv() {
if err := godotenv.Load(); err != nil {
fmt.Printf("No .env file found")
}
}

func main() {

loadEnv()
// Connect to the database
dsn := "sqlserver://Sa:Welcome@123@localhost:1433?database=bookstore"
dbUser := os.Getenv("DB_USER")
dbPassword := os.Getenv("DB_PASSWORD")
dbHost := os.Getenv("DB_HOST")
dbPort := os.Getenv("DB_PORT")
dbName := os.Getenv("DB_NAME")

dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%s?database=%s", dbUser, dbPassword, dbHost, dbPort, dbName)
db, err := databases.NewDBConnection(dsn)
if err != nil {
fmt.Printf("failed to connect to database: %v \n", err)
Expand Down

0 comments on commit 5ff5669

Please sign in to comment.