all repos — go-lift @ main

Lightweight workout tracker prototype..

src/api/health.go (view raw)

 1package api
 2
 3import (
 4	"net/http"
 5
 6	"gorm.io/gorm"
 7)
 8
 9type AutheliaUserInfo struct {
10	DisplayName string   `json:"display_name"`
11	Emails      []string `json:"emails"`
12	Method      string   `json:"method"`
13	HasTOTP     bool     `json:"has_totp"`
14	HasWebAuthn bool     `json:"has_webauthn"`
15	HasDuo      bool     `json:"has_duo"`
16}
17
18type AutheliaUserInfoResponse struct {
19	Status string           `json:"status"`
20	Data   AutheliaUserInfo `json:"data"`
21}
22
23var mockAutheliaResponse = AutheliaUserInfoResponse{
24	Status: "OK",
25	Data: AutheliaUserInfo{
26		DisplayName: "Admin",
27		Emails:      []string{"a***n@admin.com"},
28		Method:      "totp",
29	},
30}
31
32func pingHandler(w http.ResponseWriter, r *http.Request) {
33	jsonResponse(w, http.StatusOK, map[string]string{"message": "pong"})
34}
35func connectionHandler(db *gorm.DB) http.HandlerFunc {
36	return func(w http.ResponseWriter, r *http.Request) {
37		sqlDB, err := db.DB()
38		if err != nil {
39			jsonError(w, http.StatusInternalServerError, "Failed to get database connection:", err.Error())
40			return
41		}
42
43		err = sqlDB.Ping()
44		if err != nil {
45			jsonError(w, http.StatusInternalServerError, "Database ping failed:", err.Error())
46			return
47		}
48
49		jsonResponse(w, http.StatusOK, map[string]string{"message": "Database connection is healthy"})
50	}
51}
52
53func mockAutheliaHandler(w http.ResponseWriter, r *http.Request) {
54	jsonResponse(w, http.StatusOK, mockAutheliaResponse)
55}