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) {
if tdb.ReadOnly {
if ReadOnly {
return
}

View File

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

View File

@@ -1,12 +1,14 @@
package settings
var (
tdb *TDB
Path string
tdb *TDB
Path string
ReadOnly bool
)
func InitSets(readOnly bool) {
tdb = NewTDB(readOnly)
ReadOnly = readOnly
tdb = NewTDB()
loadBTSets()
Migrate()
}
@@ -14,10 +16,3 @@ func InitSets(readOnly bool) {
func 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) {
if sets.ReadOnly {
return
}
bts.Disconnect()
sets.SetBTSets(set)
bts.Connect()

View File

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

File diff suppressed because one or more lines are too long