config: use structs to group DB/MsgAuth

This commit is contained in:
Simon Ser 2022-09-11 13:50:34 +02:00
parent aee65cd269
commit d27880e03e
4 changed files with 28 additions and 19 deletions

View File

@ -86,7 +86,7 @@ func loadConfig() (*config.Server, *soju.Config, error) {
cfg := &soju.Config{
Hostname: raw.Hostname,
Title: raw.Title,
LogPath: raw.MsgStoreSource,
LogPath: raw.MsgStore.Source,
HTTPOrigins: raw.HTTPOrigins,
AcceptProxyIPs: raw.AcceptProxyIPs,
MaxUserNetworks: raw.MaxUserNetworks,
@ -118,7 +118,7 @@ func main() {
log.Printf("failed to bump max number of opened files: %v", err)
}
db, err := database.Open(cfg.SQLDriver, cfg.SQLSource)
db, err := database.Open(cfg.DB.Driver, cfg.DB.Source)
if err != nil {
log.Fatalf("failed to open database: %v", err)
}

View File

@ -45,7 +45,7 @@ func main() {
cfg = config.Defaults()
}
db, err := database.Open(cfg.SQLDriver, cfg.SQLSource)
db, err := database.Open(cfg.DB.Driver, cfg.DB.Source)
if err != nil {
log.Fatalf("failed to open database: %v", err)
}

View File

@ -36,6 +36,14 @@ type TLS struct {
CertPath, KeyPath string
}
type DB struct {
Driver, Source string
}
type MsgStore struct {
Driver, Source string
}
type Server struct {
Listen []string
TLS *TLS
@ -43,11 +51,8 @@ type Server struct {
Title string
MOTDPath string
SQLDriver string
SQLSource string
MsgStoreDriver string
MsgStoreSource string
DB DB
MsgStore MsgStore
HTTPOrigins []string
AcceptProxyIPs IPSet
@ -64,9 +69,13 @@ func Defaults() *Server {
}
return &Server{
Hostname: hostname,
SQLDriver: "sqlite3",
SQLSource: "soju.db",
MsgStoreDriver: "memory",
DB: DB{
Driver: "sqlite3",
Source: "soju.db",
},
MsgStore: MsgStore{
Driver: "memory",
},
MaxUserNetworks: -1,
MultiUpstream: true,
}
@ -109,22 +118,22 @@ func parse(cfg scfg.Block) (*Server, error) {
}
srv.TLS = tls
case "db":
if err := d.ParseParams(&srv.SQLDriver, &srv.SQLSource); err != nil {
if err := d.ParseParams(&srv.DB.Driver, &srv.DB.Source); err != nil {
return nil, err
}
case "message-store", "log":
if err := d.ParseParams(&srv.MsgStoreDriver); err != nil {
if err := d.ParseParams(&srv.MsgStore.Driver); err != nil {
return nil, err
}
switch srv.MsgStoreDriver {
switch srv.MsgStore.Driver {
case "memory":
srv.MsgStoreSource = ""
srv.MsgStore.Source = ""
case "fs":
if err := d.ParseParams(nil, &srv.MsgStoreSource); err != nil {
if err := d.ParseParams(nil, &srv.MsgStore.Source); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("directive %q: unknown driver %q", d.Name, srv.MsgStoreDriver)
return nil, fmt.Errorf("directive %q: unknown driver %q", d.Name, srv.MsgStore.Driver)
}
case "http-origin":
srv.HTTPOrigins = d.Params

View File

@ -64,7 +64,7 @@ func main() {
ctx := context.Background()
db, err := database.Open(cfg.SQLDriver, cfg.SQLSource)
db, err := database.Open(cfg.DB.Driver, cfg.DB.Source)
if err != nil {
log.Fatalf("failed to open database: %v", err)
}