2023-05-05 20:27:33 +08:00
|
|
|
package infrastructure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This function is used to connect to MariaDB.
|
|
|
|
func ConnectToMariaDB() (*sql.DB, error) {
|
|
|
|
// Connect to MariaDB.
|
2023-05-12 21:46:48 +08:00
|
|
|
db, err := sql.Open("mysql", "root:@tcp(mariadb:3306)/bill")
|
2023-05-05 20:27:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up important parts as was told by the documentation.
|
|
|
|
db.SetConnMaxLifetime(time.Minute * 3)
|
|
|
|
db.SetMaxOpenConns(10)
|
|
|
|
db.SetMaxIdleConns(10)
|
|
|
|
|
|
|
|
// Return our database instance.
|
|
|
|
return db, nil
|
|
|
|
}
|