// package cluster provides services and proxies related to the Kubernetes cluster.
package cluster

import (
	"fmt"
	"net/http"

	"github.com/tidbcloud/clusterproxy/resolve"
	"github.com/tidbcloud/clusterproxy/route"
	"github.com/tidbcloud/clusterproxy/route/router"
)

const (
	promServiceFormat = "%s-prometheus"
	promPort          = "9090"
)

func init() {
	router.RegisterRoute(promRoute)
}

var promRoute = &route.Route{
	Name:    "prometheus",
	Path:    "prometheus",
	Version: route.V1,
	Schema: &route.Schema{
		PathRegex:       `.*`,
		RequiredHeaders: []string{},
		JSONBodyModel:   nil,
	},
	GetHandler: promGetHandler,
}

func promGetHandler(c *route.Context, cl resolve.Cluster) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		promServiceName := fmt.Sprintf(promServiceFormat, cl.MonitorName)
		route.KubernetesServiceProxy("http", cl.MonitorNamespace, promServiceName, promPort).ServeHTTP(w, r)
	})
}
