Skip to content

Commit 14b2f7b

Browse files
committed
Added warning if Redis persistence is disabled and added documentation
1 parent 5a03054 commit 14b2f7b

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

docs/setup.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ During the first start, a new configuration file will be created and you will be
147147

148148
Database
149149
""""""""""""""
150-
By default, Gokapi stores its data in a database located in the ``data`` directory. You can specify a different database location in this menu. If no changes are needed, you can proceed as is. Redis is recommended for servers with a high frequency of downloads.
150+
By default, Gokapi stores its data in a SQLite database located in the ``data`` directory. You can specify a different database location in this menu. If you expect a high frequency of downloads or uploads or if your instance has a slow disk, using **Redis is strongly recommended instead of SQLite**.
151151

152152
You can configure the following settings:
153153

@@ -159,6 +159,10 @@ You can configure the following settings:
159159
- **Password (optional)** Enter the password for database connection.
160160
- **Use SSL** Select this option to establish an SSL connection.
161161

162+
163+
.. danger::
164+
When using Redis, make sure that you enable persistence (e.g. by setting ``save 1 1`` in the redis server configuration file). Otherwise all data will be lost after a restart!
165+
162166
.. warning::
163167
The Redis password will be stored in plain text and can be viewed when re-running the setup.
164168

internal/configuration/database/provider/redis/Redis.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package redis
33
import (
44
"errors"
55
"fmt"
6-
"github.com/forceu/gokapi/internal/helper"
7-
"github.com/forceu/gokapi/internal/models"
8-
redigo "github.com/gomodule/redigo/redis"
96
"os"
107
"strconv"
118
"strings"
129
"time"
10+
11+
"github.com/forceu/gokapi/internal/helper"
12+
"github.com/forceu/gokapi/internal/models"
13+
redigo "github.com/gomodule/redigo/redis"
1314
)
1415

1516
// DatabaseProvider contains the database instance
@@ -46,6 +47,15 @@ func (p DatabaseProvider) init(config models.DbConnection) (DatabaseProvider, er
4647
if err != nil {
4748
return DatabaseProvider{}, err
4849
}
50+
isPersistenceEnabled, err := p.isPersistenceEnabled()
51+
if err == nil {
52+
if !isPersistenceEnabled {
53+
fmt.Println("WARNING! Redis persistence is disabled. ALL DATA WILL BE LOST after a database restart.")
54+
}
55+
} else {
56+
fmt.Println("Unable to check if Redis has persistence enabled.")
57+
}
58+
4959
// If DB version is 0, the DB is new and therefore set version to latest one.
5060
// Otherwise, Upgrade() would be called after loading
5161
if p.GetDbVersion() == 0 {
@@ -54,6 +64,18 @@ func (p DatabaseProvider) init(config models.DbConnection) (DatabaseProvider, er
5464
return p, nil
5565
}
5666

67+
func (p DatabaseProvider) isPersistenceEnabled() (bool, error) {
68+
output, err := redigo.Values(p.getConfigRaw("save"))
69+
if err != nil {
70+
return false, err
71+
}
72+
if len(output) < 2 {
73+
return false, nil
74+
}
75+
saveVal, _ := redigo.String(output[1], nil)
76+
return len(saveVal) > 0, nil
77+
}
78+
5779
func getDialOptions(config models.DbConnection) []redigo.DialOption {
5880
dialOptions := []redigo.DialOption{redigo.DialClientName("gokapi")}
5981
if config.Username != "" {
@@ -196,6 +218,11 @@ func (p DatabaseProvider) getKeyRaw(id string) (any, error) {
196218
return conn.Do("GET", p.dbPrefix+id)
197219
}
198220

221+
func (p DatabaseProvider) getConfigRaw(id string) (any, error) {
222+
conn := p.pool.Get()
223+
defer conn.Close()
224+
return conn.Do("CONFIG", "GET", id)
225+
}
199226
func (p DatabaseProvider) getKeyString(id string) (string, bool) {
200227
result, err := redigo.String(p.getKeyRaw(id))
201228
if result == "" {

0 commit comments

Comments
 (0)