aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'config.go')
-rw-r--r--config.go26
1 files changed, 24 insertions, 2 deletions
diff --git a/config.go b/config.go
index 6605262..d1c86a0 100644
--- a/config.go
+++ b/config.go
@@ -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()