diff --git a/server/course/courseParticipation/main.go b/server/course/courseParticipation/main.go index 01b9dfb..8538ba2 100644 --- a/server/course/courseParticipation/main.go +++ b/server/course/courseParticipation/main.go @@ -2,11 +2,11 @@ package courseParticipation import ( "github.com/gin-gonic/gin" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" ) -func InitCourseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) { +func InitCourseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) { setupCourseParticipationRouter(routerGroup) CourseParticipationServiceSingleton = &CourseParticipationService{ queries: queries, diff --git a/server/course/courseParticipation/service.go b/server/course/courseParticipation/service.go index 8512ef1..42b3899 100644 --- a/server/course/courseParticipation/service.go +++ b/server/course/courseParticipation/service.go @@ -4,14 +4,14 @@ import ( "context" "github.com/google/uuid" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/niclasheun/prompt2.0/course/courseParticipation/courseParticipationDTO" db "github.com/niclasheun/prompt2.0/db/sqlc" ) type CourseParticipationService struct { queries db.Queries - conn *pgx.Conn + conn *pgxpool.Pool } var CourseParticipationServiceSingleton *CourseParticipationService diff --git a/server/course/main.go b/server/course/main.go index e9c9fb8..ff78158 100644 --- a/server/course/main.go +++ b/server/course/main.go @@ -2,13 +2,13 @@ package course import ( "github.com/gin-gonic/gin" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" "github.com/niclasheun/prompt2.0/keycloak" "github.com/niclasheun/prompt2.0/permissionValidation" ) -func InitCourseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) { +func InitCourseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) { setupCourseRouter(routerGroup, keycloak.KeycloakMiddleware, permissionValidation.CheckAccessControlByRole, checkAccessControlByIDWrapper) CourseServiceSingleton = &CourseService{ diff --git a/server/course/service.go b/server/course/service.go index ebc5bff..7175236 100644 --- a/server/course/service.go +++ b/server/course/service.go @@ -5,7 +5,7 @@ import ( "fmt" "github.com/google/uuid" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/niclasheun/prompt2.0/course/courseDTO" "github.com/niclasheun/prompt2.0/coursePhase/coursePhaseDTO" db "github.com/niclasheun/prompt2.0/db/sqlc" @@ -14,7 +14,7 @@ import ( type CourseService struct { queries db.Queries - conn *pgx.Conn + conn *pgxpool.Pool // use dependency injection for keycloak to allow mocking createCourseGroupsAndRoles func(ctx context.Context, courseName, iterationName string) error addUserToGroup func(ctx context.Context, userID, groupName string) error diff --git a/server/coursePhase/coursePhaseParticipation/main.go b/server/coursePhase/coursePhaseParticipation/main.go index cb2fb53..a90a3a6 100644 --- a/server/coursePhase/coursePhaseParticipation/main.go +++ b/server/coursePhase/coursePhaseParticipation/main.go @@ -2,11 +2,11 @@ package coursePhaseParticipation import ( "github.com/gin-gonic/gin" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" ) -func InitCoursePhaseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) { +func InitCoursePhaseParticipationModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) { setupCoursePhaseParticipationRouter(routerGroup) CoursePhaseParticipationServiceSingleton = &CoursePhaseParticipationService{ queries: queries, diff --git a/server/coursePhase/coursePhaseParticipation/service.go b/server/coursePhase/coursePhaseParticipation/service.go index f9cb0f5..c8b6564 100644 --- a/server/coursePhase/coursePhaseParticipation/service.go +++ b/server/coursePhase/coursePhaseParticipation/service.go @@ -4,14 +4,14 @@ import ( "context" "github.com/google/uuid" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/niclasheun/prompt2.0/coursePhase/coursePhaseParticipation/coursePhaseParticipationDTO" db "github.com/niclasheun/prompt2.0/db/sqlc" ) type CoursePhaseParticipationService struct { queries db.Queries - conn *pgx.Conn + conn *pgxpool.Pool } var CoursePhaseParticipationServiceSingleton *CoursePhaseParticipationService diff --git a/server/coursePhase/main.go b/server/coursePhase/main.go index 3a60a75..9d9ccda 100644 --- a/server/coursePhase/main.go +++ b/server/coursePhase/main.go @@ -2,11 +2,11 @@ package coursePhase import ( "github.com/gin-gonic/gin" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" ) -func InitCoursePhaseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) { +func InitCoursePhaseModule(routerGroup *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) { setupCoursePhaseRouter(routerGroup) CoursePhaseServiceSingleton = &CoursePhaseService{ diff --git a/server/coursePhase/service.go b/server/coursePhase/service.go index 707ae2a..bac5234 100644 --- a/server/coursePhase/service.go +++ b/server/coursePhase/service.go @@ -4,14 +4,14 @@ import ( "context" "github.com/google/uuid" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/niclasheun/prompt2.0/coursePhase/coursePhaseDTO" db "github.com/niclasheun/prompt2.0/db/sqlc" ) type CoursePhaseService struct { queries db.Queries - conn *pgx.Conn + conn *pgxpool.Pool } var CoursePhaseServiceSingleton *CoursePhaseService diff --git a/server/go.mod b/server/go.mod index 521ced1..142a43a 100644 --- a/server/go.mod +++ b/server/go.mod @@ -49,6 +49,7 @@ require ( github.com/golang-jwt/jwt/v5 v5.2.1 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.17.4 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect @@ -87,6 +88,7 @@ require ( golang.org/x/crypto v0.27.0 // indirect golang.org/x/net v0.27.0 // indirect golang.org/x/oauth2 v0.21.0 // indirect + golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.25.0 // indirect golang.org/x/text v0.18.0 // indirect google.golang.org/protobuf v1.34.1 // indirect diff --git a/server/main.go b/server/main.go index f9a8aed..d1d040b 100644 --- a/server/main.go +++ b/server/main.go @@ -7,7 +7,7 @@ import ( "os/exec" "github.com/gin-gonic/gin" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" "github.com/niclasheun/prompt2.0/course" "github.com/niclasheun/prompt2.0/course/courseParticipation" "github.com/niclasheun/prompt2.0/coursePhase" @@ -62,12 +62,12 @@ func main() { runMigrations(databaseURL) // establish db connection - conn, err := pgx.Connect(context.Background(), databaseURL) + conn, err := pgxpool.New(context.Background(), databaseURL) if err != nil { - log.Fatalf("Unable to connect to database: %v\n", err) + log.Fatalf("Unable to create connection pool: %v\n", err) os.Exit(1) } - defer conn.Close(context.Background()) + defer conn.Close() query := db.New(conn) diff --git a/server/permissionValidation/main.go b/server/permissionValidation/main.go index 2645b19..f005107 100644 --- a/server/permissionValidation/main.go +++ b/server/permissionValidation/main.go @@ -1,18 +1,18 @@ package permissionValidation import ( - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" ) type ValidationService struct { queries db.Queries - conn *pgx.Conn + conn *pgxpool.Pool } var ValidationServiceSingleton *ValidationService -func InitValidationService(queries db.Queries, conn *pgx.Conn) { +func InitValidationService(queries db.Queries, conn *pgxpool.Pool) { ValidationServiceSingleton = &ValidationService{ queries: queries, conn: conn, diff --git a/server/student/main.go b/server/student/main.go index 20ee59e..f4aef26 100644 --- a/server/student/main.go +++ b/server/student/main.go @@ -2,11 +2,11 @@ package student import ( "github.com/gin-gonic/gin" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" ) -func InitStudentModule(api *gin.RouterGroup, queries db.Queries, conn *pgx.Conn) { +func InitStudentModule(api *gin.RouterGroup, queries db.Queries, conn *pgxpool.Pool) { setupStudentRouter(api) StudentServiceSingleton = &StudentService{ diff --git a/server/student/service.go b/server/student/service.go index 92cba1a..3c95595 100644 --- a/server/student/service.go +++ b/server/student/service.go @@ -4,14 +4,14 @@ import ( "context" "github.com/google/uuid" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" "github.com/niclasheun/prompt2.0/student/studentDTO" ) type StudentService struct { queries db.Queries - conn *pgx.Conn + conn *pgxpool.Pool } var StudentServiceSingleton *StudentService diff --git a/server/testutils/db_setup.go b/server/testutils/db_setup.go index 3c3753f..c94db72 100644 --- a/server/testutils/db_setup.go +++ b/server/testutils/db_setup.go @@ -6,14 +6,14 @@ import ( "os" "time" - "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" db "github.com/niclasheun/prompt2.0/db/sqlc" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) type TestDB struct { - Conn *pgx.Conn + Conn *pgxpool.Pool Queries *db.Queries } @@ -41,10 +41,10 @@ func SetupTestDB(ctx context.Context, sqlDumpPath string) (*TestDB, func(), erro // Get container's host and port host, _ := container.Host(ctx) port, _ := container.MappedPort(ctx, "5432/tcp") - dbURL := fmt.Sprintf("postgres://testuser:testpass@%s:%s/prompt", host, port.Port()) + dbURL := fmt.Sprintf("postgres://testuser:testpass@%s:%s/prompt?sslmode=disable", host, port.Port()) // Connect to the database - conn, err := pgx.Connect(ctx, dbURL) + conn, err := pgxpool.New(ctx, dbURL) if err != nil { container.Terminate(ctx) return nil, nil, fmt.Errorf("failed to connect to the database: %w", err) @@ -52,7 +52,7 @@ func SetupTestDB(ctx context.Context, sqlDumpPath string) (*TestDB, func(), erro // Run the SQL dump if err := runSQLDump(conn, sqlDumpPath); err != nil { - conn.Close(ctx) + conn.Close() container.Terminate(ctx) return nil, nil, fmt.Errorf("failed to run SQL dump: %w", err) } @@ -62,7 +62,7 @@ func SetupTestDB(ctx context.Context, sqlDumpPath string) (*TestDB, func(), erro // Return the TestDB and a cleanup function cleanup := func() { - conn.Close(ctx) + conn.Close() container.Terminate(ctx) } @@ -72,7 +72,7 @@ func SetupTestDB(ctx context.Context, sqlDumpPath string) (*TestDB, func(), erro }, cleanup, nil } -func runSQLDump(conn *pgx.Conn, path string) error { +func runSQLDump(conn *pgxpool.Pool, path string) error { dump, err := os.ReadFile(path) if err != nil { return fmt.Errorf("could not read SQL dump file: %w", err)