package tool

import (
	"fmt"
	"net/http"

	"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

// HandleLive 一些健康检查
func HandleLive(serviceMux *runtime.ServeMux) error {
	// k8s健康检查
	err := serviceMux.HandlePath(http.MethodGet, "/healthz/ready", func(
		w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
		w.WriteHeader(http.StatusOK)
		_, _ = w.Write([]byte("OK"))
	})
	if err != nil {
		return err
	}

	// prometheus metrics
	err = serviceMux.HandlePath(http.MethodGet, "/metrics",
		func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
			promhttp.Handler().ServeHTTP(w, r)
		})
	if err != nil {
		return fmt.Errorf("serviceMux.HandlePath: %w", err)
	}

	return nil
}
