diff --git a/server/go.mod b/server/go.mod index 3fd48aab..e5d66a21 100644 --- a/server/go.mod +++ b/server/go.mod @@ -38,6 +38,7 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.6 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect @@ -68,6 +69,7 @@ require ( golang.org/x/arch v0.5.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/net v0.21.0 // indirect + golang.org/x/sync v0.5.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/protobuf v1.31.0 // indirect diff --git a/server/internal/command/command.go b/server/internal/command/command.go index abae328a..7cb60b1e 100644 --- a/server/internal/command/command.go +++ b/server/internal/command/command.go @@ -1,7 +1,6 @@ package command import ( - "context" "os" "path" @@ -77,7 +76,7 @@ func SetupCommand() { defer func() { if db != nil { logrus.Info("Closing datbase connection") - db.Close(context.Background()) + db.Close() } }() diff --git a/server/internal/handler_webdav/handler.go b/server/internal/handler_webdav/handler.go index 42d7d0b1..404986f1 100644 --- a/server/internal/handler_webdav/handler.go +++ b/server/internal/handler_webdav/handler.go @@ -31,24 +31,24 @@ func SetupHandler(r *gin.RouterGroup, fs *core.FileSystem, userManager *user.Man r.Use(func(c *gin.Context) { username, pass, ok := c.Request.BasicAuth() if !ok { - c.Header("WWW-Authenticate", "Basic realm=\"Authorization Required\"") + c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"") c.AbortWithStatus(http.StatusUnauthorized) return } user, err := userManager.FindUser(context.Background(), username) if err != nil { - c.Header("WWW-Authenticate", "Basic realm=\"Incorrect Username or Password\"") + c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"") c.AbortWithStatus(http.StatusUnauthorized) return } ok, err = cryptutil.VerifyPassword(pass, user.PasswordHash) if err != nil { - c.Header("WWW-Authenticate", "Basic realm=\"Incorrect Username or Password\"") + c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"") c.AbortWithStatus(http.StatusUnauthorized) return } if !ok { - c.Header("WWW-Authenticate", "Basic realm=\"Incorrect Username or Password\"") + c.Header("WWW-Authenticate", "Basic realm=\"Phylum WebDAV\"") c.AbortWithStatus(http.StatusUnauthorized) return } diff --git a/server/internal/migrations/migrations.go b/server/internal/migrations/migrations.go index a7609882..c9ea23ef 100644 --- a/server/internal/migrations/migrations.go +++ b/server/internal/migrations/migrations.go @@ -5,7 +5,7 @@ import ( "embed" "io/fs" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/tern/v2/migrate" ) @@ -18,9 +18,14 @@ type Migrator struct { //go:embed data/*.sql var migrationFiles embed.FS -func NewMigrator(conn *pgx.Conn) (Migrator, error) { +func NewMigrator(pool *pgxpool.Pool) (Migrator, error) { + conn, err := pool.Acquire(context.Background()) + if err != nil { + panic(err) + } + defer conn.Release() migrator, err := migrate.NewMigratorEx( - context.Background(), conn, versionTable, + context.Background(), conn.Conn(), versionTable, &migrate.MigratorOptions{ DisableTx: false, }) diff --git a/server/internal/sql/db_manager.go b/server/internal/sql/db_manager.go index 4b29d1fb..efa35969 100644 --- a/server/internal/sql/db_manager.go +++ b/server/internal/sql/db_manager.go @@ -6,33 +6,34 @@ import ( "fmt" "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/shroff/phylum/server/internal/migrations" "github.com/sirupsen/logrus" ) type DbHandler struct { - conn *pgx.Conn + pool *pgxpool.Pool queries *Queries } func NewDb(dsn string, trace, autoMigrate bool) (*DbHandler, error) { - config, err := pgx.ParseConfig(dsn) + config, err := pgxpool.ParseConfig(dsn) if err != nil { return nil, errors.New("Unable to parse DSN: " + err.Error()) } if trace { - config.Tracer = tracer{} + config.ConnConfig.Tracer = tracer{} } - conn, err := pgx.ConnectConfig(context.Background(), config) + pool, err := pgxpool.NewWithConfig(context.Background(), config) if err != nil { return nil, errors.New("Unable to connect to database: " + err.Error()) } - logrus.Info("Connected to " + config.Database + " at " + config.Host + ":" + fmt.Sprint(config.Port)) + logrus.Info("Connected to " + config.ConnConfig.Database + " at " + config.ConnConfig.Host + ":" + fmt.Sprint(config.ConnConfig.Port)) - if migrator, err := migrations.NewMigrator(conn); err != nil { + if migrator, err := migrations.NewMigrator(pool); err != nil { panic(err) } else { version, err := migrator.GetCurrentVersion() @@ -52,8 +53,8 @@ func NewDb(dsn string, trace, autoMigrate bool) (*DbHandler, error) { } return &DbHandler{ - conn: conn, - queries: &Queries{db: conn}, + pool: pool, + queries: &Queries{db: pool}, }, nil } @@ -62,12 +63,12 @@ func (d DbHandler) Queries() *Queries { } func (d DbHandler) RunInTx(ctx context.Context, fn func(*Queries) error) error { - return pgx.BeginFunc(ctx, d.conn, func(tx pgx.Tx) error { + return pgx.BeginFunc(ctx, d.pool, func(tx pgx.Tx) error { q := d.queries.WithTx(tx) return fn(q) }) } -func (d DbHandler) Close(ctx context.Context) { - d.conn.Close(ctx) +func (d DbHandler) Close() { + d.pool.Close() }