Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/controllers/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controllers
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename this file, functions to HealthCheck instead of just Health

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks , now we have proper healthCheck file name and function instead health plss check !


import (
"encoding/json"
"net/http"
)

func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{
"status": "healthy",
"service": "ccsync-backend",
})
}
56 changes: 56 additions & 0 deletions backend/controllers/healthcheck_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package controllers

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)

func TestHealthCheckHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/health", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(HealthCheckHandler)
handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}

expected := "application/json"
if contentType := rr.Header().Get("Content-Type"); contentType != expected {
t.Errorf("handler returned wrong content type: got %v want %v", contentType, expected)
}

var response map[string]string
if err := json.Unmarshal(rr.Body.Bytes(), &response); err != nil {
t.Errorf("could not parse response JSON: %v", err)
}

if response["status"] != "healthy" {
t.Errorf("expected status to be 'healthy', got %v", response["status"])
}

if response["service"] != "ccsync-backend" {
t.Errorf("expected service to be 'ccsync-backend', got %v", response["service"])
}
}

func TestHealthCheckHandlerMethodNotAllowed(t *testing.T) {
req, err := http.NewRequest("POST", "/health", nil)
if err != nil {
t.Fatal(err)
}

rr := httptest.NewRecorder()
handler := http.HandlerFunc(HealthCheckHandler)
handler.ServeHTTP(rr, req)

if status := rr.Code; status != http.StatusMethodNotAllowed {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusMethodNotAllowed)
}
}
2 changes: 2 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ func main() {
mux.Handle("/delete-task", rateLimitedHandler(http.HandlerFunc(controllers.DeleteTaskHandler)))
mux.Handle("/sync/logs", rateLimitedHandler(http.HandlerFunc(controllers.SyncLogsHandler)))

mux.HandleFunc("/health", controllers.HealthCheckHandler)

mux.HandleFunc("/ws", controllers.WebSocketHandler)

// API documentation endpoint
Expand Down
Loading