diff --git a/client/src/components/Pages/Home/Home.tsx b/client/src/components/Pages/Home/Home.tsx
index dbd0290..8e3c3b3 100644
--- a/client/src/components/Pages/Home/Home.tsx
+++ b/client/src/components/Pages/Home/Home.tsx
@@ -48,48 +48,6 @@ export const Home = () => {
routeTarget='xss'
/>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
);
diff --git a/server/internal/db/db.go b/server/internal/db/db.go
index 3634248..4da16f5 100644
--- a/server/internal/db/db.go
+++ b/server/internal/db/db.go
@@ -27,7 +27,7 @@ var allowedUsernames map[string]bool
func ConnectToDb() (*pgxpool.Pool, error) {
// this server is intended to be ran on the same system as the db
dbUrl := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s", user, password, host, port, dbname)
- config, err := pgxpool.ParseConfig((dbUrl))
+ config, err := pgxpool.ParseConfig(dbUrl)
if err != nil {
return nil, fmt.Errorf("unable to parse data URL: %w", err)
}
@@ -37,10 +37,43 @@ func ConnectToDb() (*pgxpool.Pool, error) {
return nil, fmt.Errorf("unable to create connection pool: %w", err)
}
- log.Println("Connected to DB :)")
+ // validate connection with a simple query
+ conn, err := pool.Acquire(context.Background())
+ if err != nil {
+ return nil, fmt.Errorf("unable to acquire a connection from the pool: %w", err)
+ }
+ defer conn.Release()
+
+ // run a test query
+ err = conn.QueryRow(context.Background(), "SELECT 1").Scan(new(int))
+ if err != nil {
+ return nil, fmt.Errorf("unable to validate database connection: %w", err)
+ }
+
+ log.Printf("Connected to DB at port %d :)", port)
return pool, nil
}
+// ping the database to check health
+func DbHealthCheck(w http.ResponseWriter, r *http.Request) {
+ // define the health check query
+ healthCheckSQL := `SELECT 1;`
+
+ // execute the query
+ var result int
+ err := DbPool.QueryRow(context.Background(), healthCheckSQL).Scan(&result)
+ if err != nil {
+ http.Error(w, "Database is unhealthy", http.StatusServiceUnavailable)
+ log.Printf("Database health check failed: %v", err)
+ return
+ }
+
+ // send success response
+ w.WriteHeader(http.StatusOK)
+ w.Write([]byte("Database is healthy"))
+ log.Println("Database health check passed")
+}
+
// setup demo db
func SetupDemoDb(w http.ResponseWriter, r *http.Request) {
// create table and insert demo data
diff --git a/server/internal/http_server/http_server.go b/server/internal/http_server/http_server.go
index be6aec3..01902bd 100644
--- a/server/internal/http_server/http_server.go
+++ b/server/internal/http_server/http_server.go
@@ -17,6 +17,7 @@ func healthCheck(w http.ResponseWriter, r *http.Request) {
// setup the http server
func ServeApi() {
http.HandleFunc("/health", healthCheck)
+ http.HandleFunc("/health-db", db.DbHealthCheck)
http.HandleFunc("/setup-demo-db", db.SetupDemoDb)
http.HandleFunc("/nuke-db", db.NukeDb)
http.HandleFunc("/fetch-all-users", db.FetchAllUsers)