refactor read only sets

This commit is contained in:
YouROK
2021-01-25 15:20:48 +03:00
parent 3bbc761f90
commit 9f2e0c3661
6 changed files with 16 additions and 20 deletions

View File

@@ -47,7 +47,7 @@ var (
) )
func SetBTSets(sets *BTSets) { func SetBTSets(sets *BTSets) {
if tdb.ReadOnly { if ReadOnly {
return return
} }

View File

@@ -10,11 +10,10 @@ import (
type TDB struct { type TDB struct {
Path string Path string
ReadOnly bool
db *bolt.DB db *bolt.DB
} }
func NewTDB(readOnly bool) *TDB { func NewTDB() *TDB {
db, err := bolt.Open(filepath.Join(Path, "config.db"), 0666, nil) db, err := bolt.Open(filepath.Join(Path, "config.db"), 0666, nil)
if err != nil { if err != nil {
log.TLogln(err) log.TLogln(err)
@@ -24,7 +23,6 @@ func NewTDB(readOnly bool) *TDB {
tdb := new(TDB) tdb := new(TDB)
tdb.db = db tdb.db = db
tdb.Path = Path tdb.Path = Path
tdb.ReadOnly = readOnly
return tdb return tdb
} }
@@ -69,7 +67,7 @@ func (v *TDB) Get(xpath, name string) []byte {
} }
func (v *TDB) Set(xpath, name string, value []byte) { func (v *TDB) Set(xpath, name string, value []byte) {
if v.ReadOnly { if ReadOnly {
return return
} }
@@ -144,7 +142,7 @@ func (v *TDB) List(xpath string) []string {
} }
func (v *TDB) Rem(xpath, name string) { func (v *TDB) Rem(xpath, name string) {
if v.ReadOnly { if ReadOnly {
return return
} }

View File

@@ -3,10 +3,12 @@ package settings
var ( var (
tdb *TDB tdb *TDB
Path string Path string
ReadOnly bool
) )
func InitSets(readOnly bool) { func InitSets(readOnly bool) {
tdb = NewTDB(readOnly) ReadOnly = readOnly
tdb = NewTDB()
loadBTSets() loadBTSets()
Migrate() Migrate()
} }
@@ -14,10 +16,3 @@ func InitSets(readOnly bool) {
func CloseDB() { func CloseDB() {
tdb.CloseDB() tdb.CloseDB()
} }
func IsReadOnly() bool {
if tdb == nil || tdb.ReadOnly {
return true
}
return false
}

View File

@@ -119,6 +119,9 @@ func DropTorrent(hashHex string) {
} }
func SetSettings(set *sets.BTSets) { func SetSettings(set *sets.BTSets) {
if sets.ReadOnly {
return
}
bts.Disconnect() bts.Disconnect()
sets.SetBTSets(set) sets.SetBTSets(set)
bts.Connect() bts.Connect()

View File

@@ -43,7 +43,7 @@ func echo(c *gin.Context) {
} }
func shutdown(c *gin.Context) { func shutdown(c *gin.Context) {
if sets.IsReadOnly() { if sets.ReadOnly {
c.Status(http.StatusForbidden) c.Status(http.StatusForbidden)
return return
} }

File diff suppressed because one or more lines are too long