Skip to content

Commit

Permalink
Merge pull request #10 from voltgizerz/master
Browse files Browse the repository at this point in the history
MASTER MERGE
  • Loading branch information
voltgizerz authored Jul 9, 2023
2 parents e27310c + 5f99e64 commit 9cda1c1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 4 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ func main() {
config.LoadENV()

// initialize database
db := config.InitDB()

db, err := config.InitDB()
if err != nil {
logger.Log.Error(err)
}
// initialize repository
pokemonRepo := repository.NewPokemonRepository(db)
typeRepo := repository.NewTypeRepository(db)
Expand Down
17 changes: 11 additions & 6 deletions config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"database/sql"
"fmt"
"os"
"time"

Expand All @@ -17,18 +18,22 @@ type Database struct {
}

// InitDB - .
func InitDB() *Database {
db, err := sql.Open("mysql", os.Getenv("DATA_SOURCE"))
func InitDB() (*Database, error) {
dataSource := os.Getenv("DATA_SOURCE")
if dataSource == "" {
return nil, fmt.Errorf("DATA_SOURCE environment variable is empty or not set")
}

db, err := sql.Open("mysql", dataSource)
if err != nil {
logger.Log.Println(errors.Wrap(err, "database connection"))
panic(err)
return nil, fmt.Errorf("failed to open database connection: %w", err)
}

db.SetMaxOpenConns(10)
db.SetConnMaxIdleTime(time.Minute * 10)

logger.Log.Println("database connected...")
return &Database{DB: db}
fmt.Println("Database connected...")
return &Database{DB: db}, nil
}

// Close - close db
Expand Down
Binary file added server.exe
Binary file not shown.

0 comments on commit 9cda1c1

Please sign in to comment.