summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-06-06 01:23:44 -0400
committerBen Morrison <ben@gbmor.dev>2019-06-06 01:23:44 -0400
commit439695f5521924833153852dc84040554e835f11 (patch)
tree415dcad14e5a231c59814579c022a75875527077
parent695304555da1676bc0120e7e3cc0376411897b2b (diff)
downloadgetwtxt-439695f5521924833153852dc84040554e835f11.tar.gz
moved initDatabase() to db.go
-rw-r--r--svc/db.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/svc/db.go b/svc/db.go
index 8c3f278..10ea28d 100644
--- a/svc/db.go
+++ b/svc/db.go
@@ -1,6 +1,7 @@
 package svc // import "github.com/getwtxt/getwtxt/svc"
 
 import (
+	"database/sql"
 	"log"
 	"net"
 	"strings"
@@ -10,6 +11,35 @@ import (
 	"github.com/syndtr/goleveldb/leveldb"
 )
 
+// Pull DB data into cache, if available.
+func initDatabase() {
+	var db dbase
+	var err error
+
+	confObj.Mu.RLock()
+	switch confObj.DBType {
+
+	case "leveldb":
+		var lvl *leveldb.DB
+		lvl, err = leveldb.OpenFile(confObj.DBPath, nil)
+		db = &dbLevel{db: lvl}
+
+	case "sqlite":
+		var lite *sql.DB
+		db = &dbSqlite{db: lite}
+
+	}
+	confObj.Mu.RUnlock()
+
+	if err != nil {
+		log.Fatalf("%v\n", err.Error())
+	}
+
+	dbChan <- db
+
+	pullDatabase()
+}
+
 func checkDBtime() bool {
 	confObj.Mu.RLock()
 	answer := time.Since(confObj.LastPush) > confObj.DBInterval