Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MASTER MERGE #10

Merged
merged 3 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading