2021-02-03 23:28:02 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HealthApiHandler struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHealthApiHandler(db *gorm.DB) *HealthApiHandler {
|
|
|
|
return &HealthApiHandler{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HealthApiHandler) RegisterRoutes(router *mux.Router) {
|
|
|
|
r := router.PathPrefix("/health").Subrouter()
|
2021-04-30 19:08:53 +03:00
|
|
|
r.Path("").Methods(http.MethodGet).HandlerFunc(h.Get)
|
2021-02-03 23:28:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-07 13:54:07 +03:00
|
|
|
// @Summary Check the application's health status
|
|
|
|
// @ID get-health
|
|
|
|
// @Tags misc
|
|
|
|
// @Produce plain
|
|
|
|
// @Success 200 {string} string
|
|
|
|
// @Router /health [get]
|
2021-02-03 23:28:02 +03:00
|
|
|
func (h *HealthApiHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var dbStatus int
|
|
|
|
if sqlDb, err := h.db.DB(); err == nil {
|
|
|
|
if err := sqlDb.Ping(); err == nil {
|
|
|
|
dbStatus = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
w.Write([]byte(fmt.Sprintf("app=1\ndb=%d", dbStatus)))
|
|
|
|
}
|