aboutsummaryrefslogtreecommitdiff
path: root/database
diff options
context:
space:
mode:
authorBobby <[email protected]>2025-04-06 12:03:07 +0530
committerBobby <[email protected]>2025-04-06 12:03:07 +0530
commit8d034c4ad93881ef95aae9760baf6415c28de839 (patch)
tree348746bb78356cc72fa871e623c6ea714b5b2bd5 /database
parentc947eb5f6c9bdcd624347f02a1d0b0bc65d96622 (diff)
downloadmetachan-8d034c4ad93881ef95aae9760baf6415c28de839.tar.xz
metachan-8d034c4ad93881ef95aae9760baf6415c28de839.zip
added health route for server stats
Diffstat (limited to 'database')
-rw-r--r--database/status.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/database/status.go b/database/status.go
new file mode 100644
index 0000000..14f24ed
--- /dev/null
+++ b/database/status.go
@@ -0,0 +1,37 @@
+package database
+
+import (
+ "context"
+ "fmt"
+ "metachan/types"
+ "metachan/utils/logger"
+ "time"
+)
+
+func DatabaseConnectionStatus() bool {
+ if DB == nil {
+ return false
+ }
+
+ sqlDB, err := DB.DB()
+ if err != nil {
+ logger.Log(fmt.Sprintf("Unable to get SQL DB: %v", err), types.LogOptions{
+ Prefix: "Database",
+ Level: types.Error,
+ })
+ return false
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+ defer cancel()
+
+ err = sqlDB.PingContext(ctx)
+ if err != nil {
+ logger.Log(fmt.Sprintf("Database connection error: %v", err), types.LogOptions{
+ Prefix: "Database",
+ Level: types.Error,
+ })
+ return false
+ }
+ return true
+}