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
|
// WARNING: this is not intended to be used in a multi-threaded environment
|
||||||
// Instatiate this once on startup before any goroutines are started
|
// Instatiate this once on startup before any goroutines are started
|
||||||
const (
|
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_PORT = "8080"
|
||||||
DEFAULT_ADDR = "localhost"
|
DEFAULT_ADDR = "localhost"
|
||||||
DEFAULT_HTTPS = false
|
DEFAULT_HTTPS = false
|
||||||
|
|
||||||
ENV_PREFIX = "KGPZ"
|
ENV_PREFIX = "MUSENALM"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigProvider struct {
|
type ConfigProvider struct {
|
||||||
Files []string
|
Files []string
|
||||||
|
DevFiles []string
|
||||||
*Config
|
*Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// At least one of these should be set
|
// 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 {
|
func NewConfigProvider(files []string, devfiles []string) *ConfigProvider {
|
||||||
return &ConfigProvider{Files: files}
|
return &ConfigProvider{Files: files, DevFiles: devfiles}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConfigProvider) Read() error {
|
func (c *ConfigProvider) Read() error {
|
||||||
c.Config = &Config{}
|
c.Config = &Config{}
|
||||||
|
|
||||||
for _, file := range c.Files {
|
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 = readSettingsEnv(c.Config)
|
||||||
c.Config = readDefaults(c.Config)
|
c.Config = readDefaults(c.Config)
|
||||||
@@ -51,11 +66,12 @@ func (c *ConfigProvider) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSettingsFile(cfg *Config, path string) *Config {
|
func readSettingsFile(path string) (*Config, error) {
|
||||||
|
cfg := &Config{}
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("Error opening config file ", "path", path, "error", err)
|
slog.Error("Error opening config file ", "path", path, "error", err)
|
||||||
return cfg
|
return cfg, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
@@ -63,7 +79,7 @@ func readSettingsFile(cfg *Config, path string) *Config {
|
|||||||
err = dec.Decode(cfg)
|
err = dec.Decode(cfg)
|
||||||
helpers.Assert(err, "Error decoding config file")
|
helpers.Assert(err, "Error decoding config file")
|
||||||
|
|
||||||
return cfg
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func readSettingsEnv(cfg *Config) *Config {
|
func readSettingsEnv(cfg *Config) *Config {
|
||||||
@@ -72,7 +88,6 @@ func readSettingsEnv(cfg *Config) *Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readDefaults(cfg *Config) *Config {
|
func readDefaults(cfg *Config) *Config {
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
38
app/pb.go
38
app/pb.go
@@ -2,10 +2,12 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
"github.com/pocketbase/dbx"
|
"github.com/pocketbase/dbx"
|
||||||
"github.com/pocketbase/pocketbase"
|
"github.com/pocketbase/pocketbase"
|
||||||
|
"github.com/pocketbase/pocketbase/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// INFO: this is the main application that mainly is a pocketbase wrapper
|
// INFO: this is the main application that mainly is a pocketbase wrapper
|
||||||
@@ -14,6 +16,11 @@ type App struct {
|
|||||||
MAConfig Config
|
MAConfig Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
TEST_SUPERUSER_MAIL = "test@test.de"
|
||||||
|
TEST_SUPERUSER_PASS = "passwort"
|
||||||
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sql.Register("pb_sqlite3",
|
sql.Register("pb_sqlite3",
|
||||||
&sqlite3.SQLiteDriver{
|
&sqlite3.SQLiteDriver{
|
||||||
@@ -41,6 +48,37 @@ func New(config Config) App {
|
|||||||
DBConnect: func(dbPath string) (*dbx.DB, error) {
|
DBConnect: func(dbPath string) (*dbx.DB, error) {
|
||||||
return dbx.Open("pb_sqlite3", dbPath)
|
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{
|
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
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -9,6 +10,10 @@ func Assert(err error, msg ...string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(err)
|
||||||
|
for msg := range msg {
|
||||||
|
fmt.Println(msg)
|
||||||
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,6 +22,9 @@ func AssertNonNil(obj interface{}, msg ...string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for msg := range msg {
|
||||||
|
fmt.Println(msg)
|
||||||
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,6 +33,9 @@ func AssertNil(obj interface{}, msg ...string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for msg := range msg {
|
||||||
|
fmt.Println(msg)
|
||||||
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,5 +44,9 @@ func AssertStr(str string, msg ...string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(str)
|
||||||
|
for msg := range msg {
|
||||||
|
fmt.Println(msg)
|
||||||
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,13 +96,13 @@ func handlePreferredTitleEntry(
|
|||||||
if band.Jahr == 0 {
|
if band.Jahr == 0 {
|
||||||
jahr = "[o. J.]"
|
jahr = "[o. J.]"
|
||||||
} else {
|
} else {
|
||||||
jahr = " (" + jahr + ")"
|
jahr = "(" + jahr + ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
bevti := slices.IndexFunc(rels, func(r xmlmodels.Relation_Band_Reihe) bool { return r.Relation == "1" })
|
bevti := slices.IndexFunc(rels, func(r xmlmodels.Relation_Band_Reihe) bool { return r.Relation == "1" })
|
||||||
if bevti != -1 {
|
if bevti != -1 {
|
||||||
bevt := rmap[rels[bevti].Reihe]
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
17
musenalm.go
17
musenalm.go
@@ -1,14 +1,27 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
|
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
|
||||||
|
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers"
|
||||||
_ "github.com/Theodor-Springmann-Stiftung/musenalm/migrations"
|
_ "github.com/Theodor-Springmann-Stiftung/musenalm/migrations"
|
||||||
"github.com/pocketbase/pocketbase/plugins/migratecmd"
|
"github.com/pocketbase/pocketbase/plugins/migratecmd"
|
||||||
"log"
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DEV_CONFIG = "config.dev.json"
|
||||||
|
DEFAULT_CONFIG = "config.json"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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{
|
migratecmd.MustRegister(app.PB, app.PB.RootCmd, migratecmd.Config{
|
||||||
Automigrate: false,
|
Automigrate: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user