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{ cfg := &soju.Config{
Hostname: raw.Hostname, Hostname: raw.Hostname,
Title: raw.Title, Title: raw.Title,
LogPath: raw.MsgStoreSource, LogPath: raw.MsgStore.Source,
HTTPOrigins: raw.HTTPOrigins, HTTPOrigins: raw.HTTPOrigins,
AcceptProxyIPs: raw.AcceptProxyIPs, AcceptProxyIPs: raw.AcceptProxyIPs,
MaxUserNetworks: raw.MaxUserNetworks, MaxUserNetworks: raw.MaxUserNetworks,
@ -118,7 +118,7 @@ func main() {
log.Printf("failed to bump max number of opened files: %v", err) 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 { if err != nil {
log.Fatalf("failed to open database: %v", err) log.Fatalf("failed to open database: %v", err)
} }

View File

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

View File

@ -36,6 +36,14 @@ type TLS struct {
CertPath, KeyPath string CertPath, KeyPath string
} }
type DB struct {
Driver, Source string
}
type MsgStore struct {
Driver, Source string
}
type Server struct { type Server struct {
Listen []string Listen []string
TLS *TLS TLS *TLS
@ -43,11 +51,8 @@ type Server struct {
Title string Title string
MOTDPath string MOTDPath string
SQLDriver string DB DB
SQLSource string MsgStore MsgStore
MsgStoreDriver string
MsgStoreSource string
HTTPOrigins []string HTTPOrigins []string
AcceptProxyIPs IPSet AcceptProxyIPs IPSet
@ -63,10 +68,14 @@ func Defaults() *Server {
hostname = "localhost" hostname = "localhost"
} }
return &Server{ return &Server{
Hostname: hostname, Hostname: hostname,
SQLDriver: "sqlite3", DB: DB{
SQLSource: "soju.db", Driver: "sqlite3",
MsgStoreDriver: "memory", Source: "soju.db",
},
MsgStore: MsgStore{
Driver: "memory",
},
MaxUserNetworks: -1, MaxUserNetworks: -1,
MultiUpstream: true, MultiUpstream: true,
} }
@ -109,22 +118,22 @@ func parse(cfg scfg.Block) (*Server, error) {
} }
srv.TLS = tls srv.TLS = tls
case "db": 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 return nil, err
} }
case "message-store", "log": case "message-store", "log":
if err := d.ParseParams(&srv.MsgStoreDriver); err != nil { if err := d.ParseParams(&srv.MsgStore.Driver); err != nil {
return nil, err return nil, err
} }
switch srv.MsgStoreDriver { switch srv.MsgStore.Driver {
case "memory": case "memory":
srv.MsgStoreSource = "" srv.MsgStore.Source = ""
case "fs": case "fs":
if err := d.ParseParams(nil, &srv.MsgStoreSource); err != nil { if err := d.ParseParams(nil, &srv.MsgStore.Source); err != nil {
return nil, err return nil, err
} }
default: 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": case "http-origin":
srv.HTTPOrigins = d.Params srv.HTTPOrigins = d.Params

View File

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