Skip to content

Commit 84ac0b7

Browse files
Added PostgreSQL initialization function and module
1 parent 937f7f8 commit 84ac0b7

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed
Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
1-
package postgresql
1+
package postgresql
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
)
7+
8+
/*
9+
PostgreSQL database stores logs of all expired sessions and processed transactions results
10+
this is an archival database and is used to serve archived information to users and admin panel
11+
*/
12+
13+
/*
14+
TODO: when application shutsdown, all sessions in Redis are marked expired
15+
make sure to push those expired sessions to PostreSQL database for archival
16+
*/
17+
18+
/* PostgreSQL client */
19+
type PGClient struct {
20+
db *sql.DB
21+
}
22+
23+
/* create new PostgreSQL client */
24+
func NewPGClient(connStr string) (*PGClient, error) {
25+
26+
/* initiate PostgreSQL connection */
27+
db, err := sql.Open("postgres", connStr)
28+
if err != nil {
29+
return nil, fmt.Errorf("failed to open PostgreSQL connection: %w", err)
30+
}
31+
32+
/* ping PostgreSQL connection */
33+
if err := db.Ping(); err != nil {
34+
return nil, fmt.Errorf("failed to ping PostgreSQL: %w", err)
35+
}
36+
37+
/* return the PostgreSQL client */
38+
return &PGClient{db: db}, nil
39+
}

0 commit comments

Comments
 (0)