diff options
Diffstat (limited to 'config.go')
-rw-r--r-- | config.go | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -2,11 +2,13 @@ package main import ( "bufio" + "context" "crypto/tls" "os" "sync" "go.lindenii.runxiyu.org/lindenii-common/scfg" + "github.com/jackc/pgx/v5/pgxpool" ) var config struct { @@ -16,10 +18,17 @@ var config struct { Cert string `scfg:"cert"` Key string `scfg:"key"` } `scfg:"tls"` + DB struct { + Type string `scfg:"type"` + Conn string `scfg:"conn"` + } `scfg:"db"` _tls_config *tls.Config } -var config_mutex sync.RWMutex +var config_mutex sync.RWMutex // covers things like the database too +var db *pgxpool.Pool +// load_config loads the configuration file and sets up global things according +// to the configuration directives. func load_config(path string) error { config_file, err := os.Open(path) if err != nil { @@ -33,6 +42,8 @@ func load_config(path string) error { if err != nil { return err } + + // TLS key loading and TLS config creation cer, err := tls.LoadX509KeyPair(config.TLS.Cert, config.TLS.Key) if err != nil { return err @@ -41,6 +52,17 @@ func load_config(path string) error { Certificates: []tls.Certificate{cer}, MinVersion: tls.VersionTLS13, } + + // Database setup + if config.DB.Type != "postgres" { + return err_unsupported_database_type + } + db, err = pgxpool.New(context.Background(), config.DB.Conn) + // BUG: Context-related leak: cancel context when the config is invalidated + if err != nil { + return err + } + return nil }() != nil { return err @@ -59,7 +81,7 @@ func config_fetch_one[T any](x *T) T { } // config_consistent_run runs the supplied function with a consistent snapshot -// of the configuration. +// of values covered by config_mutex. func config_consistent_run(f func()) { config_mutex.RLock() defer config_mutex.RUnlock() |