dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 91f8196fc38b03c7861f2d4c5bafdf771cdc3411
parent 20cd5de38268e40468e0ac03bd424c5467a11e42
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Thu,  9 Mar 2023 23:05:24 -0800

simplify code

Diffstat:
Mpkg/actions/actions.go | 4++--
Mpkg/database/database.go | 29+++++++++++------------------
2 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/pkg/actions/actions.go b/pkg/actions/actions.go @@ -47,7 +47,7 @@ func Start(c *cli.Context) error { ensureProjectHome() - db := database.DB() + db := database.NewDkfDB() runMigrations(db) @@ -278,7 +278,7 @@ func BuildProhibitedPasswords(c *cli.Context) error { rows = append(rows, database.ProhibitedPassword{Password: fileScanner.Text()}) } readFile.Close() - if err := gormbulk.BulkInsert(database.DB().DB(), rows, 10000); err != nil { + if err := gormbulk.BulkInsert(database.NewDkfDB().DB(), rows, 10000); err != nil { logrus.Error(err) } return nil diff --git a/pkg/database/database.go b/pkg/database/database.go @@ -9,7 +9,6 @@ import ( "net/url" "path/filepath" "strings" - "sync" "time" ) @@ -222,23 +221,17 @@ type IDkfDB interface { WhitelistUser(roomID RoomID, userID UserID) (out ChatRoomWhitelistedUser, err error) } -var once sync.Once -var inst *DkfDB - -func DB() *DkfDB { - once.Do(func() { - dbPath := filepath.Join(config.Global.ProjectPath(), config.DbFileName) - db, err := gorm.Open("sqlite3", dbPath) - if err != nil { - logrus.Fatal("Failed to open sqlite3 db : " + err.Error()) - } - db.DB().SetMaxIdleConns(1) // 10 - db.DB().SetMaxOpenConns(1) // 25 - db.LogMode(false) - db.Exec("PRAGMA foreign_keys=ON") - inst = &DkfDB{db: db} - }) - return inst +func NewDkfDB() *DkfDB { + dbPath := filepath.Join(config.Global.ProjectPath(), config.DbFileName) + db, err := gorm.Open("sqlite3", dbPath) + if err != nil { + logrus.Fatal("Failed to open sqlite3 db : " + err.Error()) + } + db.DB().SetMaxIdleConns(1) // 10 + db.DB().SetMaxOpenConns(1) // 25 + db.LogMode(false) + db.Exec("PRAGMA foreign_keys=ON") + return &DkfDB{db: db} } // DB2 is the SQL database.