Browse Source

Added sqlite as database option

tags/v0.1
kolaente 1 year ago
parent
commit
9e6ddafe7e
Signed by: kolaente <konrad@kola-entertainments.de> GPG Key ID: F40E70337AB24C9B
3 changed files with 20 additions and 5 deletions
  1. 3
    0
      config.ini.sample
  2. 2
    0
      models/config.go
  3. 15
    5
      models/models.go

+ 3
- 0
config.ini.sample View File

@@ -4,11 +4,14 @@ JWTSecret = blablaGEHEMIN§)!§
4 4
 Interface = :8080
5 5
 
6 6
 [Database]
7
+Type = mysql
7 8
 User = root
8 9
 Password = supersecret
9 10
 Host = 127.0.0.1
10 11
 Database = library
11 12
 ShowQueries = false
13
+; When using sqlite, this is the path where to store the data
14
+; Path = ./library.db
12 15
 
13 16
 ; First user to be created, on every startup the program checks if he exists, if not it creates it
14 17
 [User]

+ 2
- 0
models/config.go View File

@@ -8,10 +8,12 @@ import (
8 8
 // ConfigStruct holds the config struct
9 9
 type ConfigStruct struct {
10 10
 	Database struct {
11
+		Type 		string
11 12
 		Host        string
12 13
 		User        string
13 14
 		Password    string
14 15
 		Database    string
16
+		Path 		string
15 17
 		ShowQueries bool
16 18
 	}
17 19
 

+ 15
- 5
models/models.go View File

@@ -2,7 +2,7 @@ package models
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	//_ "github.com/mattn/go-sqlite3" // Because.
5
+	_ "github.com/mattn/go-sqlite3" // Because.
6 6
 	_ "github.com/go-sql-driver/mysql" // Because.
7 7
 	"github.com/go-xorm/core"
8 8
 	"github.com/go-xorm/xorm"
@@ -11,10 +11,19 @@ import (
11 11
 var x *xorm.Engine
12 12
 
13 13
 func getEngine() (*xorm.Engine, error) {
14
-	connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
15
-		Config.Database.User, Config.Database.Password, Config.Database.Host, Config.Database.Database)
16
-	return xorm.NewEngine("mysql", connStr)
17
-	//return xorm.NewEngine("sqlite3", "./db.db")
14
+	// Use Mysql if set
15
+	if Config.Database.Type == "mysql" {
16
+		connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&parseTime=true",
17
+			Config.Database.User, Config.Database.Password, Config.Database.Host, Config.Database.Database)
18
+		return xorm.NewEngine("mysql", connStr)
19
+	} else {
20
+		// Otherwise use sqlite
21
+		path := Config.Database.Path
22
+		if path == "" {
23
+			path = "./db.db"
24
+		}
25
+		return xorm.NewEngine("sqlite3", path)
26
+	}
18 27
 }
19 28
 
20 29
 // SetEngine sets the xorm.Engine
@@ -40,6 +49,7 @@ func SetEngine() (err error) {
40 49
 	x.Sync(&Quantity{})
41 50
 	x.Sync(&quantityRelation{})
42 51
 	x.Sync(&Item{})
52
+	x.Sync(&UserLog{})
43 53
 
44 54
 	x.ShowSQL(Config.Database.ShowQueries)
45 55
 

Loading…
Cancel
Save