Skip to content

Commit

Permalink
Disable spatialite, WAL by default
Browse files Browse the repository at this point in the history
spatialite has proven to be a continual pain in the ass for something we
don't actually use in the app, so drop it for now. Can continue to use it
in the CLI for ad-hoc if we want.

Enable WAL mode by default. This ruins the 'one file' thing, but also is
what should be used in a serving environment
  • Loading branch information
lstoll committed May 30, 2022
1 parent 4b9aa49 commit 139a2a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
42 changes: 9 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/sha256"
"crypto/subtle"
"database/sql"
"encoding/json"
"errors"
"flag"
Expand All @@ -16,11 +15,9 @@ import (
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"time"

"github.com/mattn/go-sqlite3"
"github.com/oklog/run"
oidcm "github.com/pardot/oidc/middleware"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -29,10 +26,6 @@ import (
"golang.org/x/oauth2"
)

func init() {
registerSpatiaLite()
}

const (
mainDBFile = "wherewasi.db"
secretsFile = "secrets.json"
Expand Down Expand Up @@ -492,13 +485,15 @@ type baseCommand struct {
storage *Storage
smgr *secretsManager

dbPath string
dbPath string
disableWal bool

fs *flag.FlagSet
}

func (b *baseCommand) AddFlags(fs *flag.FlagSet) {
fs.StringVar(&b.dbPath, "db-path", "db", "directory for data storage")
fs.BoolVar(&b.disableWal, "disable-wal", false, "disable WAL mode for sqlite")
b.fs = fs
}

Expand All @@ -516,7 +511,12 @@ func (b *baseCommand) Parse(ctx context.Context, logger logger) {
os.Exit(1)
}

st, err := newStorage(ctx, logger, fmt.Sprintf("file:%s?cache=shared&_foreign_keys=on", filepath.Join(b.dbPath, mainDBFile)))
connStr := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_journal_mode=WAL&_foreign_keys=on", filepath.Join(b.dbPath, mainDBFile))
if b.disableWal {
connStr = fmt.Sprintf("file:%s?cache=shared&_foreign_keys=on", filepath.Join(b.dbPath, mainDBFile))
}

st, err := newStorage(ctx, logger, connStr)
if err != nil {
logger.Fatalf("creating storage: %v", err)
}
Expand All @@ -530,30 +530,6 @@ func (b *baseCommand) Parse(ctx context.Context, logger logger) {
}
}

func registerSpatiaLite() {
exts := map[string]string{}

if runtime.GOOS == "linux" {
exts["libspatialite.so.7"] = "spatialite_init_ex"
} else if runtime.GOOS == "darwin" {
exts["mod_spatialite"] = "sqlite3_modspatialite_init"
}

sql.Register("spatialite", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
if len(exts) > 0 {
for l, e := range exts {
if err := conn.LoadExtension(l, e); err == nil {
return nil
}
}
return fmt.Errorf("loading spatialite failed. make sure libraries are installed")
}
return nil
},
})
}

func wrapBasicAuth(username, password string, wrap http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
Expand Down
2 changes: 1 addition & 1 deletion sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ type Storage struct {
}

func newStorage(ctx context.Context, logger logger, connStr string) (*Storage, error) {
db, err := sql.Open("spatialite", connStr)
db, err := sql.Open("sqlite3", connStr)
if err != nil {
return nil, fmt.Errorf("opening DB: %v", err)
}
Expand Down

0 comments on commit 139a2a1

Please sign in to comment.