Skip to content

Commit

Permalink
session: fix database connections with UNIX sockets (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Chen <jc@unknwon.io>
  • Loading branch information
NyCodeGHG and unknwon authored Jul 25, 2023
1 parent 95ab3bb commit b854a8b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
28 changes: 21 additions & 7 deletions cmd/pgrokd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,31 @@ func startWebServer(config *conf.Config, db *database.DB) {
f.Use(flamego.Logger())
f.Use(flamego.Recovery())
f.Use(flamego.Renderer())

var postgresDSN string
// Check if the host is a UNIX domain socket
if strings.HasPrefix(config.Database.Host, "/") {
postgresDSN = fmt.Sprintf("postgres://%s:%s@localhost:%d/%s?host=%s",
config.Database.User,
config.Database.Password,
config.Database.Port,
config.Database.Database,
config.Database.Host,
)
} else {
postgresDSN = fmt.Sprintf("postgres://%s:%s@%s:%d/%s",
config.Database.User,
config.Database.Password,
config.Database.Host,
config.Database.Port,
config.Database.Database,
)
}
f.Use(session.Sessioner(
session.Options{
Initer: postgres.Initer(),
Config: postgres.Config{
DSN: fmt.Sprintf("postgres://%s:%s@%s:%d/%s",
config.Database.User,
config.Database.Password,
config.Database.Host,
config.Database.Port,
config.Database.Database,
),
DSN: postgresDSN,
Table: "sessions",
InitTable: true,
},
Expand Down
2 changes: 1 addition & 1 deletion internal/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func New(logWriter io.Writer, config *conf.Database) (*DB, error) {
)

dsn := fmt.Sprintf(
"user='%s' password='%s' host='%s' port='%d' dbname='%s' search_path='public' application_name='pgrokd'",
"user='%s' password='%s' host='%s' port='%d' dbname='%s' search_path='public' application_name='pgrokd' client_encoding=UTF8",
config.User, config.Password, config.Host, config.Port, config.Database,
)
db, err := gorm.Open(
Expand Down

0 comments on commit b854a8b

Please sign in to comment.