From 3366cde38574eca30aa90c5534df3b7ade649714 Mon Sep 17 00:00:00 2001 From: Thomas Miceli <27960254+thomiceli@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:25:41 +0200 Subject: [PATCH] Sqlite journal mode (#54) --- README.md | 1 + config.yml | 4 ++++ internal/config/config.go | 9 +++++++++ internal/models/db.go | 13 +++++++++++-- internal/utils/slice.go | 10 ++++++++++ templates/pages/admin_config.html | 1 + 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 internal/utils/slice.go diff --git a/README.md b/README.md index 37a16e5..a407d68 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ You would only need to specify the configuration options you want to change — | external-url | OG_EXTERNAL_URL | none | Public URL for the Git HTTP/SSH connection. If not set, uses the URL from the request. | | opengist-home | OG_OPENGIST_HOME | home directory | Path to the directory where Opengist stores its data. | | db-filename | OG_DB_FILENAME | `opengist.db` | Name of the SQLite database file. | +| sqlite.journal-mode | OG_SQLITE_JOURNAL_MODE | `WAL` | Set the journal mode for SQLite. More info [here](https://www.sqlite.org/pragma.html#pragma_journal_mode) | | http.host | OG_HTTP_HOST | `0.0.0.0` | The host on which the HTTP server should bind. | | http.port | OG_HTTP_PORT | `6157` | The port on which the HTTP server should listen. | | http.git-enabled | OG_HTTP_GIT_ENABLED | `true` | Enable or disable git operations (clone, pull, push) via HTTP. (`true` or `false`) | diff --git a/config.yml b/config.yml index e84af6e..04b9889 100644 --- a/config.yml +++ b/config.yml @@ -11,6 +11,10 @@ opengist-home: # Name of the SQLite database file. Default: opengist.db db-filename: opengist.db +# Set the journal mode for SQLite. Default: WAL +# See https://www.sqlite.org/pragma.html#pragma_journal_mode +sqlite.journal-mode: WAL + # HTTP server configuration # Host to bind to. Default: 0.0.0.0 diff --git a/internal/config/config.go b/internal/config/config.go index 897722e..aab17da 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/rs/zerolog" "github.com/rs/zerolog/log" + "github.com/thomiceli/opengist/internal/utils" "gopkg.in/yaml.v3" "os" "path/filepath" @@ -24,6 +25,8 @@ type config struct { OpengistHome string `yaml:"opengist-home" env:"OG_OPENGIST_HOME"` DBFilename string `yaml:"db-filename" env:"OG_DB_FILENAME"` + SqliteJournalMode string `yaml:"sqlite.journal-mode" env:"OG_SQLITE_JOURNAL_MODE"` + HttpHost string `yaml:"http.host" env:"OG_HTTP_HOST"` HttpPort string `yaml:"http.port" env:"OG_HTTP_PORT"` HttpGit bool `yaml:"http.git-enabled" env:"OG_HTTP_GIT_ENABLED"` @@ -56,6 +59,8 @@ func configWithDefaults() (*config, error) { c.OpengistHome = filepath.Join(homeDir, ".opengist") c.DBFilename = "opengist.db" + c.SqliteJournalMode = "WAL" + c.HttpHost = "0.0.0.0" c.HttpPort = "6157" c.HttpGit = true @@ -108,6 +113,10 @@ func InitLog() { multi := zerolog.MultiLevelWriter(zerolog.NewConsoleWriter(), file) log.Logger = zerolog.New(multi).Level(level).With().Timestamp().Logger() + + if !utils.SliceContains([]string{"trace", "debug", "info", "warn", "error", "fatal", "panic"}, strings.ToLower(C.LogLevel)) { + log.Warn().Msg("Invalid log level: " + C.LogLevel) + } } func CheckGitVersion(version string) (bool, error) { diff --git a/internal/models/db.go b/internal/models/db.go index 4a417ca..a3fc124 100644 --- a/internal/models/db.go +++ b/internal/models/db.go @@ -3,17 +3,26 @@ package models import ( "errors" "github.com/mattn/go-sqlite3" + "github.com/rs/zerolog/log" + "github.com/thomiceli/opengist/internal/config" + "github.com/thomiceli/opengist/internal/utils" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" + "strings" ) var db *gorm.DB -func Setup(dbpath string) error { +func Setup(dbPath string) error { var err error + journalMode := strings.ToUpper(config.C.SqliteJournalMode) - if db, err = gorm.Open(sqlite.Open(dbpath+"?_fk=true"), &gorm.Config{ + if !utils.SliceContains([]string{"DELETE", "TRUNCATE", "PERSIST", "MEMORY", "WAL", "OFF"}, journalMode) { + log.Warn().Msg("Invalid SQLite journal mode: " + journalMode) + } + + if db, err = gorm.Open(sqlite.Open(dbPath+"?_fk=true&_journal_mode="+journalMode), &gorm.Config{ Logger: logger.Default.LogMode(logger.Silent), }); err != nil { return err diff --git a/internal/utils/slice.go b/internal/utils/slice.go new file mode 100644 index 0000000..d7d9d40 --- /dev/null +++ b/internal/utils/slice.go @@ -0,0 +1,10 @@ +package utils + +func SliceContains(slice []string, item string) bool { + for _, s := range slice { + if s == item { + return true + } + } + return false +} diff --git a/templates/pages/admin_config.html b/templates/pages/admin_config.html index 0cdb077..d04432b 100644 --- a/templates/pages/admin_config.html +++ b/templates/pages/admin_config.html @@ -17,6 +17,7 @@