Start of creating app structure

This commit is contained in:
Simon Martens
2025-02-09 16:30:17 +01:00
parent a250d1b18e
commit 52239727d4
6 changed files with 155 additions and 26 deletions

86
app/config.go Normal file
View 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
View 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,
}
}

9
go.mod
View File

@@ -2,7 +2,13 @@ module github.com/Theodor-Springmann-Stiftung/musenalm
go 1.23.2
require github.com/pocketbase/pocketbase v0.25.0
require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/mattn/go-sqlite3 v1.14.24
github.com/pocketbase/dbx v1.11.0
github.com/pocketbase/pocketbase v0.25.0
github.com/yalue/merged_fs v1.3.0
)
require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
@@ -40,7 +46,6 @@ require (
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pocketbase/dbx v1.11.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/cobra v1.8.1 // indirect

6
go.sum
View File

@@ -137,6 +137,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -145,6 +147,8 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -175,6 +179,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yalue/merged_fs v1.3.0 h1:qCeh9tMPNy/i8cwDsQTJ5bLr6IRxbs6meakNE5O+wyY=
github.com/yalue/merged_fs v1.3.0/go.mod h1:WqqchfVYQyclV2tnR7wtRhBddzBvLVR83Cjw9BKQw0M=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.54.0 h1:r6I7RJCN86bpD/FQwedZ0vSixDpwuWREjW9oRMsmqDc=

View File

@@ -2,8 +2,6 @@ package helpers
import (
"os"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers/logging"
)
func Assert(err error, msg ...string) {
@@ -11,7 +9,6 @@ func Assert(err error, msg ...string) {
return
}
logging.Error(err, msg...)
os.Exit(1)
}
@@ -20,7 +17,6 @@ func AssertNonNil(obj interface{}, msg ...string) {
return
}
logging.Error(nil, msg...)
os.Exit(1)
}
@@ -29,7 +25,6 @@ func AssertNil(obj interface{}, msg ...string) {
return
}
logging.Error(nil, msg...)
os.Exit(1)
}
@@ -38,6 +33,5 @@ func AssertStr(str string, msg ...string) {
return
}
logging.Error(nil, msg...)
os.Exit(1)
}

View File

@@ -1,33 +1,21 @@
package main
import (
"log"
"os"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
"github.com/Theodor-Springmann-Stiftung/musenalm/app"
_ "github.com/Theodor-Springmann-Stiftung/musenalm/migrations"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
"log"
)
func main() {
app := pocketbase.New()
app := app.New(app.Config{})
migratecmd.MustRegister(app, app.RootCmd, migratecmd.Config{
migratecmd.MustRegister(app.PB, app.PB.RootCmd, migratecmd.Config{
Automigrate: false,
TemplateLang: migratecmd.TemplateLangGo,
})
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
// serves static files from the provided public dir (if exists)
se.Router.GET("/{path...}", apis.Static(os.DirFS("./pb_public"), false))
return se.Next()
})
if err := app.Start(); err != nil {
if err := app.PB.Start(); err != nil {
log.Fatal(err)
}
}