@@ -20,8 +20,11 @@ package postgres
2020
2121import (
2222 "context"
23+ "flag"
2324 "fmt"
2425 "log/slog"
26+ "os"
27+ "strconv"
2528 "time"
2629
2730 "github.com/jackc/pgx/v5/pgxpool"
@@ -46,47 +49,6 @@ type PostgresClient struct {
4649 logger * slog.Logger
4750}
4851
49- // NewPostgresClientFromParams creates a new PostgreSQL client with the given connection parameters.
50- // This is a convenience function that constructs a PostgresConfig and calls NewPostgresClient.
51- func CreatePostgresClient (
52- ctx context.Context ,
53- logger * slog.Logger ,
54- host string ,
55- port int ,
56- database string ,
57- user string ,
58- password string ,
59- maxConns int32 ,
60- minConns int32 ,
61- maxConnLifetime time.Duration ,
62- sslMode string ,
63- ) (* PostgresClient , error ) {
64- config := PostgresConfig {
65- Host : host ,
66- Port : port ,
67- Database : database ,
68- User : user ,
69- Password : password ,
70- MaxConns : maxConns ,
71- MinConns : minConns ,
72- MaxConnLifetime : maxConnLifetime ,
73- SSLMode : sslMode ,
74- }
75-
76- client , err := NewPostgresClient (ctx , config , logger )
77- if err != nil {
78- return nil , err
79- }
80-
81- logger .Info ("postgres client initialized" ,
82- slog .String ("host" , host ),
83- slog .Int ("port" , port ),
84- slog .String ("database" , database ),
85- )
86-
87- return client , nil
88- }
89-
9052// NewPostgresClient creates a new PostgreSQL client with connection pooling
9153func NewPostgresClient (ctx context.Context , config PostgresConfig , logger * slog.Logger ) (* PostgresClient , error ) {
9254 // Build connection URL
@@ -126,7 +88,11 @@ func NewPostgresClient(ctx context.Context, config PostgresConfig, logger *slog.
12688 return nil , fmt .Errorf ("failed to ping database: %w" , err )
12789 }
12890
129- logger .Info ("postgres client connected successfully" )
91+ logger .Info ("postgres client connected successfully" ,
92+ slog .String ("host" , config .Host ),
93+ slog .Int ("port" , config .Port ),
94+ slog .String ("database" , config .Database ),
95+ )
13096
13197 return & PostgresClient {
13298 pool : pool ,
@@ -149,3 +115,88 @@ func (c *PostgresClient) Pool() *pgxpool.Pool {
149115func (c * PostgresClient ) Ping (ctx context.Context ) error {
150116 return c .pool .Ping (ctx )
151117}
118+
119+ // CreateClient creates a PostgreSQL client from PostgresConfig
120+ func (config * PostgresConfig ) CreateClient (logger * slog.Logger ) (* PostgresClient , error ) {
121+ return NewPostgresClient (context .Background (), * config , logger )
122+ }
123+
124+ // PostgresFlagPointers holds pointers to flag values for PostgreSQL configuration
125+ type PostgresFlagPointers struct {
126+ host * string
127+ port * int
128+ user * string
129+ password * string
130+ database * string
131+ maxConns * int
132+ minConns * int
133+ maxConnLifetimeMin * int
134+ sslMode * string
135+ }
136+
137+ // RegisterPostgresFlags registers PostgreSQL-related command-line flags
138+ // Returns a PostgresFlagPointers that should be converted to PostgresArgs
139+ // after flag.Parse() is called
140+ func RegisterPostgresFlags () * PostgresFlagPointers {
141+ return & PostgresFlagPointers {
142+ host : flag .String ("postgres-host" ,
143+ getEnv ("OSMO_POSTGRES_HOST" , "localhost" ),
144+ "PostgreSQL host" ),
145+ port : flag .Int ("postgres-port" ,
146+ getEnvInt ("OSMO_POSTGRES_PORT" , 5432 ),
147+ "PostgreSQL port" ),
148+ user : flag .String ("postgres-user" ,
149+ getEnv ("OSMO_POSTGRES_USER" , "postgres" ),
150+ "PostgreSQL user" ),
151+ password : flag .String ("postgres-password" ,
152+ getEnv ("OSMO_POSTGRES_PASSWORD" , "" ),
153+ "PostgreSQL password" ),
154+ database : flag .String ("postgres-database" ,
155+ getEnv ("OSMO_POSTGRES_DATABASE_NAME" , "osmo_db" ),
156+ "PostgreSQL database name" ),
157+ maxConns : flag .Int ("postgres-max-conns" ,
158+ getEnvInt ("OSMO_POSTGRES_MAX_CONNS" , 10 ),
159+ "PostgreSQL maximum connections in pool" ),
160+ minConns : flag .Int ("postgres-min-conns" ,
161+ getEnvInt ("OSMO_POSTGRES_MIN_CONNS" , 2 ),
162+ "PostgreSQL minimum connections in pool" ),
163+ maxConnLifetimeMin : flag .Int ("postgres-max-conn-lifetime" ,
164+ getEnvInt ("OSMO_POSTGRES_MAX_CONN_LIFETIME" , 5 ),
165+ "PostgreSQL maximum connection lifetime in minutes" ),
166+ sslMode : flag .String ("postgres-ssl-mode" ,
167+ getEnv ("OSMO_POSTGRES_SSL_MODE" , "disable" ),
168+ "PostgreSQL SSL mode (disable, require, verify-ca, verify-full)" ),
169+ }
170+ }
171+
172+ // ToPostgresConfig converts flag pointers to PostgresConfig
173+ // This should be called after flag.Parse()
174+ func (p * PostgresFlagPointers ) ToPostgresConfig () PostgresConfig {
175+ return PostgresConfig {
176+ Host : * p .host ,
177+ Port : * p .port ,
178+ Database : * p .database ,
179+ User : * p .user ,
180+ Password : * p .password ,
181+ MaxConns : int32 (* p .maxConns ),
182+ MinConns : int32 (* p .minConns ),
183+ MaxConnLifetime : time .Duration (* p .maxConnLifetimeMin ) * time .Minute ,
184+ SSLMode : * p .sslMode ,
185+ }
186+ }
187+
188+ func getEnv (key , defaultValue string ) string {
189+ if value := os .Getenv (key ); value != "" {
190+ return value
191+ }
192+ return defaultValue
193+ }
194+
195+ func getEnvInt (key string , defaultValue int ) int {
196+ if value := os .Getenv (key ); value != "" {
197+ if intValue , err := strconv .Atoi (value ); err == nil {
198+ return intValue
199+ }
200+ }
201+ return defaultValue
202+ }
0 commit comments