Sqlite journal mode (#54)

This commit is contained in:
Thomas Miceli
2023-06-09 15:25:41 +02:00
committed by GitHub
parent b2a56fe5a0
commit 3366cde385
6 changed files with 36 additions and 2 deletions

View File

@ -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) {

View File

@ -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

10
internal/utils/slice.go Normal file
View File

@ -0,0 +1,10 @@
package utils
func SliceContains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}