Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use PG* envvars #1029

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions backend/cmd/scoreserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -34,6 +29,6 @@ func newConfig() (*scoreserver.Config, error) {
ContestantHTTPAddress: netip.AddrPort{},
ContestantBaseURLs: []url.URL{},

DBDSN: dsn,
PgConfig: *cfg,
}, nil
}
4 changes: 3 additions & 1 deletion backend/scoreserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package scoreserver
import (
"net/netip"
"net/url"

"github.com/jackc/pgx/v5"
)

type Config struct {
Expand All @@ -11,7 +13,7 @@ type Config struct {
ContestantHTTPAddress netip.AddrPort
ContestantBaseURLs []url.URL

DBDSN string
PgConfig pgx.ConnConfig
}

type AdminAPIConfig struct {
Expand Down
8 changes: 1 addition & 7 deletions backend/scoreserver/scoreserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down
12 changes: 6 additions & 6 deletions backend/scripts/local-exec
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
4 changes: 2 additions & 2 deletions backend/scripts/migrate
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$@"
4 changes: 0 additions & 4 deletions backend/scripts/psqlw

This file was deleted.

Loading