mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 01:05:32 +00:00
Start of creating app structure
This commit is contained in:
86
app/config.go
Normal file
86
app/config.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers"
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
// 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"
|
||||
)
|
||||
|
||||
type ConfigProvider struct {
|
||||
Files []string
|
||||
*Config
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
// At least one of these should be set
|
||||
}
|
||||
|
||||
func NewConfigProvider(files []string) *ConfigProvider {
|
||||
return &ConfigProvider{Files: files}
|
||||
}
|
||||
|
||||
func (c *ConfigProvider) Read() error {
|
||||
c.Config = &Config{}
|
||||
for _, file := range c.Files {
|
||||
c.Config = readSettingsFile(c.Config, file)
|
||||
}
|
||||
c.Config = readSettingsEnv(c.Config)
|
||||
c.Config = readDefaults(c.Config)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ConfigProvider) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func readSettingsFile(cfg *Config, path string) *Config {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
slog.Error("Error opening config file ", "path", path, "error", err)
|
||||
return cfg
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
dec := json.NewDecoder(f)
|
||||
err = dec.Decode(cfg)
|
||||
helpers.Assert(err, "Error decoding config file")
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func readSettingsEnv(cfg *Config) *Config {
|
||||
_ = envconfig.Process(ENV_PREFIX, cfg)
|
||||
return cfg
|
||||
}
|
||||
|
||||
func readDefaults(cfg *Config) *Config {
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// Implement stringer
|
||||
func (c *Config) String() string {
|
||||
json, err := json.MarshalIndent(c, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Sprintf("Error marshalling config: %v", err)
|
||||
}
|
||||
return string(json)
|
||||
}
|
||||
50
app/pb.go
Normal file
50
app/pb.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/mattn/go-sqlite3"
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase"
|
||||
)
|
||||
|
||||
// INFO: this is the main application that mainly is a pocketbase wrapper
|
||||
type App struct {
|
||||
PB *pocketbase.PocketBase
|
||||
MAConfig Config
|
||||
}
|
||||
|
||||
func init() {
|
||||
sql.Register("pb_sqlite3",
|
||||
&sqlite3.SQLiteDriver{
|
||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||
_, err := conn.Exec(`
|
||||
PRAGMA busy_timeout = 10000;
|
||||
PRAGMA journal_mode = WAL;
|
||||
PRAGMA journal_size_limit = 200000000;
|
||||
PRAGMA synchronous = NORMAL;
|
||||
PRAGMA foreign_keys = ON;
|
||||
PRAGMA temp_store = MEMORY;
|
||||
PRAGMA cache_size = -32768;
|
||||
`, nil)
|
||||
|
||||
return err
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
dbx.BuilderFuncMap["pb_sqlite3"] = dbx.BuilderFuncMap["sqlite3"]
|
||||
}
|
||||
|
||||
func New(config Config) App {
|
||||
app := pocketbase.NewWithConfig(pocketbase.Config{
|
||||
DBConnect: func(dbPath string) (*dbx.DB, error) {
|
||||
return dbx.Open("pb_sqlite3", dbPath)
|
||||
},
|
||||
})
|
||||
|
||||
return App{
|
||||
PB: app,
|
||||
MAConfig: config,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user