Skip to content

Commit

Permalink
Sqlite journal mode (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomiceli committed Jun 9, 2023
1 parent b2a56fe commit 3366cde
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`) |
Expand Down
4 changes: 4 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 11 additions & 2 deletions internal/models/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

func SliceContains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
1 change: 1 addition & 0 deletions templates/pages/admin_config.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<dt>External URL</dt><dd>{{ .c.ExternalUrl }}</dd>
<dt>Opengist home</dt><dd>{{ .c.OpengistHome }}</dd>
<dt>DB filename</dt><dd>{{ .c.DBFilename }}</dd>
<dt>SQLite Journal Mode</dt><dd>{{ .c.SqliteJournalMode }}</dd>
<div class="relative col-span-3 mt-4">
<div class="absolute inset-0 flex items-center" aria-hidden="true">
<div class="w-full border-t border-gray-300"></div>
Expand Down

0 comments on commit 3366cde

Please sign in to comment.