From 982636b483004a64edbfbeb7dda75605d0c012ec Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 12 Jan 2025 17:16:11 +0800 Subject: Add PostgreSQL connection --- config.go | 26 ++++++++++++++++++++++++-- errors.go | 1 + go.mod | 10 ++++++++++ go.sum | 21 +++++++++++++++++++++ mta_recv.go | 1 - 5 files changed, 56 insertions(+), 3 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() diff --git a/errors.go b/errors.go index 247e507..da4d028 100644 --- a/errors.go +++ b/errors.go @@ -5,3 +5,4 @@ import ( ) var err_deliver_write = errors.New("unable to write to filesystem while attempting to deliver message") +var err_unsupported_database_type = errors.New("unsupported database type; only \"postgres\" is currently supported") diff --git a/go.mod b/go.mod index 314be8d..358240c 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,13 @@ module go.lindenii.runxiyu.org/maild go 1.23.4 require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250111202249-35d69905e2fc + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.7.2 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + golang.org/x/crypto v0.32.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/text v0.21.0 // indirect +) diff --git a/go.sum b/go.sum index 3e0ed5e..7d35297 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,23 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.2 h1:mLoDLV6sonKlvjIEsV56SkWNCnuNv531l94GaIzO+XI= +github.com/jackc/pgx/v5 v5.7.2/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250111202249-35d69905e2fc h1:jYjxxslcbZSKmXT96FhRW//t0dx9LN54J4PeD6oXTQk= go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250111202249-35d69905e2fc/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mta_recv.go b/mta_recv.go index 7042901..b67beb4 100644 --- a/mta_recv.go +++ b/mta_recv.go @@ -71,7 +71,6 @@ func handle_incoming_server_connection(buf_conn *bufio.ReadWriter, net_conn *net tls_conn = tls.Server(*net_conn, tls_config) buf_conn = bufio.NewReadWriter(bufio.NewReader(tls_conn), bufio.NewWriter(tls_conn)) server_state = server_state_begin - remote_server_name = "" current_mail_from = "" current_rcpt_to = []string{""} case "HELO": -- cgit v1.2.3