예제 #1
0
func TestParseURI(t *testing.T) {
	t.Parallel()

	tests := []struct {
		url        string
		connParams pgx.ConnConfig
	}{
		{
			url: "postgres://*****:*****@localhost:5432/mydb",
			connParams: pgx.ConnConfig{
				User:     "******",
				Password: "******",
				Host:     "localhost",
				Port:     5432,
				Database: "mydb",
			},
		},
		{
			url: "postgresql://*****:*****@localhost:5432/mydb",
			connParams: pgx.ConnConfig{
				User:     "******",
				Password: "******",
				Host:     "localhost",
				Port:     5432,
				Database: "mydb",
			},
		},
		{
			url: "postgres://jack@localhost:5432/mydb",
			connParams: pgx.ConnConfig{
				User:     "******",
				Host:     "localhost",
				Port:     5432,
				Database: "mydb",
			},
		},
		{
			url: "postgres://jack@localhost/mydb",
			connParams: pgx.ConnConfig{
				User:     "******",
				Host:     "localhost",
				Database: "mydb",
			},
		},
	}

	for i, tt := range tests {
		connParams, err := pgx.ParseURI(tt.url)
		if err != nil {
			t.Errorf("%d. Unexpected error from pgx.ParseURL(%q) => %v", i, tt.url, err)
			continue
		}

		if connParams != tt.connParams {
			t.Errorf("%d. expected %#v got %#v", i, tt.connParams, connParams)
		}
	}
}
예제 #2
0
func main() {
	var keys []*tufdata.Key
	if err := json.Unmarshal([]byte(os.Getenv("ROOT_KEYS")), &keys); err != nil {
		log.Fatal("missing or invalid ROOT_KEYS:", err)
	}
	opts := &tuf.HTTPRemoteOptions{
		UserAgent: "cli-redirect/v1",
	}
	remote, err := tuf.HTTPRemoteStore(os.Getenv("REPO_URL"), opts)
	if err != nil {
		log.Fatal("error initializing remote store:", err)
	}
	r := &redirector{
		RepoURL: os.Getenv("REPO_URL"),
		Client:  tuf.NewClient(tuf.MemoryLocalStore(), remote),
		refresh: make(chan struct{}, 1),
		notify:  make(chan struct{}, 1),
	}
	if err := r.Client.Init(keys, len(keys)); err != nil {
		log.Fatal("error initializing client:", err)
	}
	if _, err := r.Client.Update(); err != nil {
		log.Fatal("error running first update:", err)
	}
	targets, err := r.Client.Targets()
	if err != nil {
		log.Fatal("error getting targets:", err)
	}
	r.Targets.Store(targets)

	pgConf, err := pgx.ParseURI(os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal("error parsing DATABASE_URL:", err)
	}
	r.DB, err = pgx.NewConnPool(pgx.ConnPoolConfig{ConnConfig: pgConf})
	if err != nil {
		log.Fatal("error creating pgx pool:", err)
	}

	go r.pgListener()
	go r.pgNotifier()
	go r.tufLoader()

	log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), r))
}
예제 #3
0
func (d *Driver) Open(name string) (driver.Conn, error) {
	if d.Pool != nil {
		conn, err := d.Pool.Acquire()
		if err != nil {
			return nil, err
		}

		return &Conn{conn: conn, pool: d.Pool}, nil
	}

	connConfig, err := pgx.ParseURI(name)
	if err != nil {
		return nil, err
	}

	conn, err := pgx.Connect(connConfig)
	if err != nil {
		return nil, err
	}

	c := &Conn{conn: conn}
	return c, nil
}