Initial commit
This commit is contained in:
30
internal/handlers/health.go
Normal file
30
internal/handlers/health.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
DBConnected bool `json:"db_connected"`
|
||||
}
|
||||
|
||||
type HealthcheckService struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewHealthcheckService(db *sql.DB) *HealthcheckService {
|
||||
return &HealthcheckService{db: db}
|
||||
}
|
||||
|
||||
func (h *HealthcheckService) Health(w http.ResponseWriter, r *http.Request) {
|
||||
response := HealthResponse{
|
||||
Status: "ok",
|
||||
}
|
||||
|
||||
err := h.db.Ping()
|
||||
response.DBConnected = err == nil
|
||||
|
||||
JSON(w, http.StatusOK, response)
|
||||
}
|
||||
16
internal/handlers/responses.go
Normal file
16
internal/handlers/responses.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func JSON(w http.ResponseWriter, status int, data any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func Error(w http.ResponseWriter, status int, message string) {
|
||||
JSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
Reference in New Issue
Block a user