add read-only db work mode

This commit is contained in:
nikk gitanes
2020-11-03 01:24:32 +03:00
parent 06d9a364d1
commit 045a0914ac
4 changed files with 18 additions and 4 deletions

View File

@@ -18,6 +18,7 @@ type args struct {
Port string `arg:"-p" help:"web server port"` Port string `arg:"-p" help:"web server port"`
Path string `arg:"-d" help:"database path"` Path string `arg:"-d" help:"database path"`
Add string `arg:"-a" help:"add torrent link and exit"` Add string `arg:"-a" help:"add torrent link and exit"`
RDB bool `arg:"-r" help:"start in read-only DB mode"`
Kill bool `arg:"-k" help:"dont kill program on signal"` Kill bool `arg:"-k" help:"dont kill program on signal"`
} }
@@ -45,7 +46,11 @@ func main() {
Preconfig(params.Kill) Preconfig(params.Kill)
server.Start(params.Path, params.Port) server.Start(params.Path, params.Port)
settings.SaveSettings() if (params.RDB) {
settings.SetRDB()
} else {
settings.SaveSettings()
}
fmt.Println(server.WaitServer()) fmt.Println(server.WaitServer())
time.Sleep(time.Second * 3) time.Sleep(time.Second * 3)
os.Exit(0) os.Exit(0)

View File

@@ -22,9 +22,10 @@ func openDB() error {
} }
var err error var err error
db, err = bolt.Open(filepath.Join(Path, "torrserver.db"), 0666, nil) var ro = Get().ReadOnlyMode
db, err = bolt.Open(filepath.Join(Path, "torrserver.db"), 0666, &bolt.Options{ReadOnly: ro})
if err != nil { if err != nil {
fmt.Print(err) fmt.Println(err)
return err return err
} }

View File

@@ -37,6 +37,7 @@ type Settings struct {
DisableUPNP bool DisableUPNP bool
DisableDHT bool DisableDHT bool
DisableUpload bool DisableUpload bool
ReadOnlyMode bool
Encryption int // 0 - Enable, 1 - disable, 2 - force Encryption int // 0 - Enable, 1 - disable, 2 - force
DownloadRateLimit int // in kb, 0 - inf DownloadRateLimit int // in kb, 0 - inf
UploadRateLimit int // in kb, 0 - inf UploadRateLimit int // in kb, 0 - inf
@@ -113,3 +114,10 @@ func SaveSettings() error {
return setsDB.Put([]byte("json"), []byte(buf)) return setsDB.Put([]byte("json"), []byte(buf))
}) })
} }
func SetRDB() {
SaveSettings()
fmt.Println("Enable Read-only DB mode")
CloseDB()
sets.ReadOnlyMode = true
}

View File

@@ -182,7 +182,7 @@ func torrentGet(c echo.Context) error {
tor, err := settings.LoadTorrentDB(jreq.Hash) tor, err := settings.LoadTorrentDB(jreq.Hash)
if err != nil { if err != nil {
fmt.Println("Error get torrent:", jreq.Hash, err) fmt.Println("Error get torrent:", jreq.Hash, err)
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) // return echo.NewHTTPError(http.StatusBadRequest, err.Error()) // Handle R/O DB
} }
torrStatus := torr.TorrentAdded torrStatus := torr.TorrentAdded