summary refs log tree commit diff stats
path: root/storage/storage.go
blob: 9aa1a57c69016ab69466af37c92eeac7073c37db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package storage

import (
	"database/sql"
	"fmt"
	"log"
	"sync"
)

// DB holds the database connection, mutex & path.
type DB struct {
	Path string
	Mu   *sync.RWMutex
	Conn *sql.DB
}

// Init initializes the database.
func Init() *DB {
	db := DB{
		Mu: new(sync.RWMutex),
	}

	initDB(&db)
	return &db
}

// InitConn initializes database connection.
func InitConn() *DB {
	var err error
	db := DB{
		Mu: new(sync.RWMutex),
	}

	db.Path = fmt.Sprintf("%s/grus.db", GetDir())

	db.Conn, err = sql.Open("sqlite3", db.Path)
	if err != nil {
		log.Printf("storage/init.go: %s\n",
			"Failed to open database connection")
		initErr(&db, err)
	}

	return &db
}