mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-28 16:55:32 +00:00
Allow for setting a testuser via settings
This commit is contained in:
@@ -12,35 +12,50 @@ import (
|
||||
// WARNING: this is not intended to be used in a multi-threaded environment
|
||||
// Instatiate this once on startup before any goroutines are started
|
||||
const (
|
||||
DEFAULT_GIT_DIR = "data_git"
|
||||
DEFAULT_GND_DIR = "cache_gnd"
|
||||
DEFAULT_GEO_DIR = "cache_geo"
|
||||
DEFAULT_IMG_DIR = "data_bilder"
|
||||
|
||||
DEFAULT_PORT = "8080"
|
||||
DEFAULT_ADDR = "localhost"
|
||||
DEFAULT_HTTPS = false
|
||||
|
||||
ENV_PREFIX = "KGPZ"
|
||||
ENV_PREFIX = "MUSENALM"
|
||||
)
|
||||
|
||||
type ConfigProvider struct {
|
||||
Files []string
|
||||
DevFiles []string
|
||||
*Config
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
// At least one of these should be set
|
||||
Debug bool `json:"debug,omitempty" envconfig:"DEBUG"`
|
||||
AllowTestLogin bool `json:"allow_test_login,omitempty" envconfig:"ALLOW_TEST_LOGIN"`
|
||||
}
|
||||
|
||||
func NewConfigProvider(files []string) *ConfigProvider {
|
||||
return &ConfigProvider{Files: files}
|
||||
func NewConfigProvider(files []string, devfiles []string) *ConfigProvider {
|
||||
return &ConfigProvider{Files: files, DevFiles: devfiles}
|
||||
}
|
||||
|
||||
func (c *ConfigProvider) Read() error {
|
||||
c.Config = &Config{}
|
||||
|
||||
for _, file := range c.Files {
|
||||
c.Config = readSettingsFile(c.Config, file)
|
||||
conf, err := readSettingsFile(file)
|
||||
if err == nil {
|
||||
c.Config = conf
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, file := range c.DevFiles {
|
||||
conf, err := readSettingsFile(file)
|
||||
if c.Debug {
|
||||
if err == nil {
|
||||
c.Config = conf
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
c.Config = readSettingsEnv(c.Config)
|
||||
c.Config = readDefaults(c.Config)
|
||||
@@ -51,11 +66,12 @@ func (c *ConfigProvider) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readSettingsFile(cfg *Config, path string) *Config {
|
||||
func readSettingsFile(path string) (*Config, error) {
|
||||
cfg := &Config{}
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
slog.Error("Error opening config file ", "path", path, "error", err)
|
||||
return cfg
|
||||
return cfg, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
@@ -63,7 +79,7 @@ func readSettingsFile(cfg *Config, path string) *Config {
|
||||
err = dec.Decode(cfg)
|
||||
helpers.Assert(err, "Error decoding config file")
|
||||
|
||||
return cfg
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func readSettingsEnv(cfg *Config) *Config {
|
||||
@@ -72,7 +88,6 @@ func readSettingsEnv(cfg *Config) *Config {
|
||||
}
|
||||
|
||||
func readDefaults(cfg *Config) *Config {
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
|
||||
38
app/pb.go
38
app/pb.go
@@ -2,10 +2,12 @@ package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
// INFO: this is the main application that mainly is a pocketbase wrapper
|
||||
@@ -14,6 +16,11 @@ type App struct {
|
||||
MAConfig Config
|
||||
}
|
||||
|
||||
const (
|
||||
TEST_SUPERUSER_MAIL = "test@test.de"
|
||||
TEST_SUPERUSER_PASS = "passwort"
|
||||
)
|
||||
|
||||
func init() {
|
||||
sql.Register("pb_sqlite3",
|
||||
&sqlite3.SQLiteDriver{
|
||||
@@ -41,6 +48,37 @@ func New(config Config) App {
|
||||
DBConnect: func(dbPath string) (*dbx.DB, error) {
|
||||
return dbx.Open("pb_sqlite3", dbPath)
|
||||
},
|
||||
DefaultDev: config.Debug,
|
||||
})
|
||||
|
||||
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
|
||||
superusersCol, err := e.App.FindCachedCollectionByNameOrId(core.CollectionNameSuperusers)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to fetch %q collection: %w.", core.CollectionNameSuperusers, err)
|
||||
}
|
||||
|
||||
superuser, err := e.App.FindAuthRecordByEmail(superusersCol, TEST_SUPERUSER_MAIL)
|
||||
if err != nil {
|
||||
superuser = core.NewRecord(superusersCol)
|
||||
} else if !config.AllowTestLogin {
|
||||
// INFO: we to it as a raw query here since PB does not support deleting the last superuser
|
||||
_, err = e.App.DB().
|
||||
NewQuery("DELETE FROM " + superusersCol.Name + " WHERE id = '" + superuser.Id + "'").
|
||||
Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to delete superuser account: %w.", err)
|
||||
}
|
||||
return e.Next()
|
||||
}
|
||||
|
||||
superuser.SetEmail(TEST_SUPERUSER_MAIL)
|
||||
superuser.SetPassword(TEST_SUPERUSER_PASS)
|
||||
|
||||
if err := app.Save(superuser); err != nil {
|
||||
return fmt.Errorf("Failed to upsert superuser account: %w.", err)
|
||||
}
|
||||
|
||||
return e.Next()
|
||||
})
|
||||
|
||||
return App{
|
||||
|
||||
4
config.dev.json
Normal file
4
config.dev.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"debug": true,
|
||||
"allow_test_login": false
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
@@ -9,6 +10,10 @@ func Assert(err error, msg ...string) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(err)
|
||||
for msg := range msg {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -17,6 +22,9 @@ func AssertNonNil(obj interface{}, msg ...string) {
|
||||
return
|
||||
}
|
||||
|
||||
for msg := range msg {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -25,6 +33,9 @@ func AssertNil(obj interface{}, msg ...string) {
|
||||
return
|
||||
}
|
||||
|
||||
for msg := range msg {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -33,5 +44,9 @@ func AssertStr(str string, msg ...string) {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(str)
|
||||
for msg := range msg {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -96,13 +96,13 @@ func handlePreferredTitleEntry(
|
||||
if band.Jahr == 0 {
|
||||
jahr = "[o. J.]"
|
||||
} else {
|
||||
jahr = " (" + jahr + ")"
|
||||
jahr = "(" + jahr + ")"
|
||||
}
|
||||
|
||||
bevti := slices.IndexFunc(rels, func(r xmlmodels.Relation_Band_Reihe) bool { return r.Relation == "1" })
|
||||
if bevti != -1 {
|
||||
bevt := rmap[rels[bevti].Reihe]
|
||||
record.Set(dbmodels.PREFERRED_TITLE_FIELD, NormalizeString(bevt.Titel)+jahr)
|
||||
record.Set(dbmodels.PREFERRED_TITLE_FIELD, NormalizeString(bevt.Titel)+" "+jahr)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
17
musenalm.go
17
musenalm.go
@@ -1,14 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers"
|
||||
_ "github.com/Theodor-Springmann-Stiftung/musenalm/migrations"
|
||||
"github.com/pocketbase/pocketbase/plugins/migratecmd"
|
||||
"log"
|
||||
)
|
||||
|
||||
const (
|
||||
DEV_CONFIG = "config.dev.json"
|
||||
DEFAULT_CONFIG = "config.json"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := app.New(app.Config{})
|
||||
|
||||
cfg := app.NewConfigProvider([]string{DEFAULT_CONFIG}, []string{DEV_CONFIG})
|
||||
if err := cfg.Read(); err != nil {
|
||||
helpers.Assert(err, "Error reading config")
|
||||
}
|
||||
|
||||
app := app.New(*cfg.Config)
|
||||
|
||||
migratecmd.MustRegister(app.PB, app.PB.RootCmd, migratecmd.Config{
|
||||
Automigrate: false,
|
||||
|
||||
Reference in New Issue
Block a user