Compare commits

...

2 commits

Author SHA1 Message Date
b8fc873850 ♻️ Refactor main to load toml
Temporary until we set up actually loading from a specific file
2024-09-14 23:58:05 -05:00
9ddaa98ff4 Load config from TOML 2024-09-14 23:07:28 -05:00
6 changed files with 78 additions and 20 deletions

View file

@ -4,6 +4,7 @@ package main
import ( import (
"fmt" "fmt"
"os"
"codeberg.org/danjones000/lenore" "codeberg.org/danjones000/lenore"
"codeberg.org/danjones000/lenore/config" "codeberg.org/danjones000/lenore/config"
@ -11,23 +12,15 @@ import (
) )
func main() { func main() {
conf := config.Config{ conf, err := config.LoadFromToml(getTomlFile())
BaseURL: "http://localhost:4523/", quitErr(err)
Conn: config.ConnSettings{ fmt.Printf("%+v\n", conf)
Store: "sqlite",
DSN: "storage",
},
}
db, err := store.MakeStore(conf.Conn.Store, conf) db, err := store.MakeStore(conf.Conn.Store, conf)
if err != nil { quitErr(err)
panic(err)
}
app, err := lenore.NewApp(config.Version, conf, db) app, err := lenore.NewApp(config.Version, conf, db)
if err != nil { quitErr(err)
panic(err)
}
fmt.Println(app) fmt.Println(app)
serv := app.Service() serv := app.Service()
@ -35,3 +28,28 @@ func main() {
fmt.Println(string(out)) fmt.Println(string(out))
fmt.Println(serv.ID) fmt.Println(serv.ID)
} }
func quitErr(err error) {
if err != nil {
panic(err)
}
}
func getTomlFile() string {
tmp, err := os.CreateTemp("", "*.toml")
if err != nil {
panic(err)
}
defer tmp.Close()
p := tmp.Name()
fmt.Fprintln(tmp, confStr)
return p
}
var confStr = `
base_url = "http://localhost:4523/"
[conn]
store = "sqlite"
dsn = "store"
`

View file

@ -1,16 +1,16 @@
package config package config
type Config struct { type Config struct {
Name string Name string `toml:"name"`
Env Env Env Env `toml:"env"`
BaseURL string BaseURL string `toml:"base_url"`
Conn ConnSettings Conn ConnSettings `toml:"conn"`
} }
type ConnSettings struct { type ConnSettings struct {
Store string Store string `toml:"store"`
DSN string DSN string `toml:"dsn"`
AdditionalSettings map[string]any Settings map[string]any `toml:"settings"`
} }
func (c Config) Environment() Env { func (c Config) Environment() Env {

8
config/load.go Normal file
View file

@ -0,0 +1,8 @@
package config
import "github.com/BurntSushi/toml"
func LoadFromToml(path string) (c Config, err error) {
_, err = toml.DecodeFile(path, &c)
return
}

29
config/load_test.go Normal file
View file

@ -0,0 +1,29 @@
package config
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLoadTomlMissing(t *testing.T) {
_, e := LoadFromToml("not-a-real-filee")
assert.Error(t, e)
}
func TestLoadTomlGood(t *testing.T) {
tmp, _ := os.CreateTemp("", "*.toml")
defer tmp.Close()
fmt.Fprintln(tmp, `name = "Cool"`)
fmt.Fprintln(tmp, "[conn]")
fmt.Fprintln(tmp, `store = "sqlite"`)
fmt.Fprintln(tmp, "[conn.settings]")
fmt.Fprintln(tmp, `num = 42`)
c, e := LoadFromToml(tmp.Name())
assert.NoError(t, e)
assert.Equal(t, "Cool", c.Name)
assert.Equal(t, "sqlite", c.Conn.Store)
assert.Equal(t, int64(42), c.Conn.Settings["num"])
}

1
go.mod
View file

@ -3,6 +3,7 @@ module codeberg.org/danjones000/lenore
go 1.23.1 go 1.23.1
require ( require (
github.com/BurntSushi/toml v1.4.0
github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c github.com/go-ap/activitypub v0.0.0-20240910141749-b4b8c8aa484c
github.com/go-ap/client v0.0.0-20240910141951-13a4f3c4fd53 github.com/go-ap/client v0.0.0-20240910141951-13a4f3c4fd53
github.com/go-ap/fedbox v0.0.0-20240910163620-7bcedb2eb399 github.com/go-ap/fedbox v0.0.0-20240910163620-7bcedb2eb399

2
go.sum
View file

@ -7,6 +7,8 @@ git.sr.ht/~mariusor/lw v0.0.0-20240906100438-00d2184b2120 h1:OLxL9lel79BV3EHu/AM
git.sr.ht/~mariusor/lw v0.0.0-20240906100438-00d2184b2120/go.mod h1:kXJ4JsgGBu7IVBKlrVvGjSLJmpsAGqZwq/JU/kTUaLw= git.sr.ht/~mariusor/lw v0.0.0-20240906100438-00d2184b2120/go.mod h1:kXJ4JsgGBu7IVBKlrVvGjSLJmpsAGqZwq/JU/kTUaLw=
git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7 h1:bCGvett+MiEDc5L+T9jByp671KnKRG/iJCCm0fc+21s= git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7 h1:bCGvett+MiEDc5L+T9jByp671KnKRG/iJCCm0fc+21s=
git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7/go.mod h1:VApG24PG5Ij+tw5zpN5O61FSQU9gJK/cYQwFYM+kkwA= git.sr.ht/~mariusor/ssm v0.0.0-20240811085540-34f24cac52b7/go.mod h1:VApG24PG5Ij+tw5zpN5O61FSQU9gJK/cYQwFYM+kkwA=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/carlmjohnson/be v0.23.2 h1:1QjPnPJhwGUjsD9+7h98EQlKsxnG5TV+nnEvk0wnkls= github.com/carlmjohnson/be v0.23.2 h1:1QjPnPJhwGUjsD9+7h98EQlKsxnG5TV+nnEvk0wnkls=
github.com/carlmjohnson/be v0.23.2/go.mod h1:KAgPUh0HpzWYZZI+IABdo80wTgY43YhbdsiLYAaSI/Q= github.com/carlmjohnson/be v0.23.2/go.mod h1:KAgPUh0HpzWYZZI+IABdo80wTgY43YhbdsiLYAaSI/Q=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=