From d2ba2cdf796b6757d055e1082a6bfdef836a5457 Mon Sep 17 00:00:00 2001 From: tosuke <13393900+tosuke@users.noreply.github.com> Date: Sat, 11 Jan 2025 04:24:24 +0900 Subject: [PATCH 1/2] refactor: parse DSN --- backend/cmd/scoreserver/config.go | 17 ++++++----------- backend/scoreserver/config.go | 4 +++- backend/scoreserver/scoreserver.go | 8 +------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/backend/cmd/scoreserver/config.go b/backend/cmd/scoreserver/config.go index 2c7fd4a0..55ebbc66 100644 --- a/backend/cmd/scoreserver/config.go +++ b/backend/cmd/scoreserver/config.go @@ -7,23 +7,18 @@ import ( "github.com/cockroachdb/errors" "github.com/ictsc/ictsc-regalia/backend/scoreserver" + "github.com/jackc/pgx/v5" ) func newConfig() (*scoreserver.Config, error) { - var errs []error - adminHTTPAddr, err := netip.ParseAddrPort(*flagAdminHTTPAddr) if err != nil { - errs = append(errs, errors.Wrap(err, "invalid admin HTTP address")) - } - - dsn := os.Getenv("DB_DSN") - if dsn == "" { - errs = append(errs, errors.New("DB_DSN must be set")) + return nil, errors.Wrap(err, "invalid admin HTTP address") } - if len(errs) > 0 { - return nil, errors.Join(errs...) + cfg, err := pgx.ParseConfig(os.Getenv("DB_DSN")) + if err != nil { + return nil, errors.Wrap(err, "failed to parse DB_DSN") } return &scoreserver.Config{ @@ -34,6 +29,6 @@ func newConfig() (*scoreserver.Config, error) { ContestantHTTPAddress: netip.AddrPort{}, ContestantBaseURLs: []url.URL{}, - DBDSN: dsn, + PgConfig: *cfg, }, nil } diff --git a/backend/scoreserver/config.go b/backend/scoreserver/config.go index 76eec4e4..039c2f1d 100644 --- a/backend/scoreserver/config.go +++ b/backend/scoreserver/config.go @@ -3,6 +3,8 @@ package scoreserver import ( "net/netip" "net/url" + + "github.com/jackc/pgx/v5" ) type Config struct { @@ -11,7 +13,7 @@ type Config struct { ContestantHTTPAddress netip.AddrPort ContestantBaseURLs []url.URL - DBDSN string + PgConfig pgx.ConnConfig } type AdminAPIConfig struct { diff --git a/backend/scoreserver/scoreserver.go b/backend/scoreserver/scoreserver.go index 594f18e5..5e368841 100644 --- a/backend/scoreserver/scoreserver.go +++ b/backend/scoreserver/scoreserver.go @@ -13,7 +13,6 @@ import ( "github.com/ictsc/ictsc-regalia/backend/pkg/pgxutil" "github.com/ictsc/ictsc-regalia/backend/pkg/proto/admin/v1/adminv1connect" "github.com/ictsc/ictsc-regalia/backend/scoreserver/admin" - "github.com/jackc/pgx/v5" "github.com/jmoiron/sqlx" "golang.org/x/net/http2" "golang.org/x/net/http2/h2c" @@ -31,12 +30,7 @@ type ScoreServer struct { } func New(cfg *Config) (*ScoreServer, error) { - pgcfg, err := pgx.ParseConfig(cfg.DBDSN) - if err != nil { - return nil, errors.Wrap(err, "failed to parse DB URL") - } - - db := pgxutil.NewDBx(*pgcfg, pgxutil.WithOTel(true)) + db := pgxutil.NewDBx(cfg.PgConfig, pgxutil.WithOTel(true)) adminServer := cfg.AdminAPI.new(db) From 5651b1c6bd48d1c5571f4d5603f3593acc88bac5 Mon Sep 17 00:00:00 2001 From: tosuke <13393900+tosuke@users.noreply.github.com> Date: Sat, 11 Jan 2025 04:25:11 +0900 Subject: [PATCH 2/2] feat: use PG* envvars --- backend/scripts/local-exec | 12 ++++++------ backend/scripts/migrate | 4 ++-- backend/scripts/psqlw | 4 ---- 3 files changed, 8 insertions(+), 12 deletions(-) delete mode 100755 backend/scripts/psqlw diff --git a/backend/scripts/local-exec b/backend/scripts/local-exec index 89e87b71..927d61af 100755 --- a/backend/scripts/local-exec +++ b/backend/scripts/local-exec @@ -3,13 +3,13 @@ compose_file="$(dirname "$0")/../compose.yaml" -export DB_HOST=127.0.0.1 -DB_PORT="$(docker compose -f "$compose_file" port postgres 5432 | cut -d: -f2)" -export DB_PORT -export DB_USER=ictsc +export PGHOST=127.0.0.1 +PGPORT="$(docker compose -f "$compose_file" port postgres 5432 | cut -d: -f2)" +export PGPORT +export PGUSER=ictsc export PGPASSWORD=password -export DB_NAME=ictscore -export DB_DSN="host=$DB_HOST port=$DB_PORT user=$DB_USER password=$PGPASSWORD dbname=$DB_NAME sslmode=disable" +export PGDATABASE=ictscore +export PGSSLMODE=disable export OTEL_METRICS_EXPORTER=${OTEL_METRICS_EXPORTER:-prometheus} diff --git a/backend/scripts/migrate b/backend/scripts/migrate index 2315bdbe..b4287d42 100755 --- a/backend/scripts/migrate +++ b/backend/scripts/migrate @@ -2,10 +2,10 @@ # vim: ft=sh : PSQLDEF="go run github.com/sqldef/sqldef/cmd/psqldef" -if which psqldef > /dev/null; then +if which psqldef 2>/dev/null; then PSQLDEF=psqldef fi schema_file=$(dirname "$0")/../schema.sql -exec $PSQLDEF --host="$DB_HOST" --port="$DB_PORT" --user="$DB_USER" "$DB_NAME" < "$schema_file" "$@" +exec $PSQLDEF --host="${PGHOST:-localhost}" --port="${PGPORT:-5432}" --user="${PGUSER:-ictsc}" "${PGDATABASE:-ictscore}" < "$schema_file" "$@" diff --git a/backend/scripts/psqlw b/backend/scripts/psqlw deleted file mode 100755 index e765b4ad..00000000 --- a/backend/scripts/psqlw +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -# vim: ft=sh : - -psql --host="$DB_HOST" --port="$DB_PORT" --user="$DB_USER" "$DB_NAME" "$@"