TTime/backend/internal/database/db.go
2024-02-12 12:40:49 +01:00

32 lines
504 B
Go

package database
import (
"os"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
)
func DbConnect() *sqlx.DB {
// Check for the environment variable
dbpath := os.Getenv("SQLITE_DB_PATH")
// Default to something reasonable
if dbpath == "" {
dbpath = "./db.sqlite3"
}
// Open the database
// db, err := sqlx.Connect("sqlite3", ":memory:")
db, err := sqlx.Connect("sqlite3", dbpath)
if err != nil {
panic(err)
}
err = db.Ping()
if err != nil {
panic(err)
}
return db
}