diff options
author | Ben Morrison <ben@gbmor.dev> | 2019-06-06 01:23:44 -0400 |
---|---|---|
committer | Ben Morrison <ben@gbmor.dev> | 2019-06-06 01:23:44 -0400 |
commit | 439695f5521924833153852dc84040554e835f11 (patch) | |
tree | 415dcad14e5a231c59814579c022a75875527077 | |
parent | 695304555da1676bc0120e7e3cc0376411897b2b (diff) | |
download | getwtxt-439695f5521924833153852dc84040554e835f11.tar.gz |
moved initDatabase() to db.go
-rw-r--r-- | svc/db.go | 30 |
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 |