summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-12-27 12:14:43 +0100
committerxengineering <me@xengineering.eu>2023-12-27 12:27:53 +0100
commitc452ca52b9c18303045db838c6cead62c1454172 (patch)
tree7828c41b7aa7df33d4bfc49649b01087f685e15f
parentaaf2bad8d3ab2a4f825c2d3db3f2d17de68e08b4 (diff)
downloadceres-c452ca52b9c18303045db838c6cead62c1454172.tar
ceres-c452ca52b9c18303045db838c6cead62c1454172.tar.zst
ceres-c452ca52b9c18303045db838c6cead62c1454172.zip
model: Implement database connection
The already implemented storage folder should contain a sqlite database to store most parts of the Ceres user data.
-rw-r--r--main.go3
-rw-r--r--model/database.go36
2 files changed, 39 insertions, 0 deletions
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")
+ }
+}