From c452ca52b9c18303045db838c6cead62c1454172 Mon Sep 17 00:00:00 2001 From: xengineering Date: Wed, 27 Dec 2023 12:14:43 +0100 Subject: model: Implement database connection The already implemented storage folder should contain a sqlite database to store most parts of the Ceres user data. --- main.go | 3 +++ model/database.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 model/database.go diff --git a/main.go b/main.go index 57204af..cba4416 100644 --- a/main.go +++ b/main.go @@ -7,4 +7,7 @@ import ( func main() { model.InitStorage() defer model.RemoveStorage() + + model.InitDatabase() + defer model.CloseDatabase() } diff --git a/model/database.go b/model/database.go new file mode 100644 index 0000000..ed9a985 --- /dev/null +++ b/model/database.go @@ -0,0 +1,36 @@ +package model + +import ( + "database/sql" + "log" + "path/filepath" + + _ "github.com/mattn/go-sqlite3" +) + +var db *sql.DB + +func InitDatabase() { + var err error + + fn := filepath.Join(storage, "ceres.sqlite3") + + db, err = sql.Open("sqlite3", fn) + if err != nil { + log.Fatal(err) + } + + err = db.Ping() + if err != nil { + log.Fatal(err) + } +} + +func CloseDatabase() { + var err error = db.Close() + if err != nil { + log.Printf("Failed to close database: %v\n", err) + } else { + log.Println("Closed database") + } +} -- cgit v1.2.3-70-g09d2