mirror of https://github.com/jetkvm/kvm.git
feat: make metrics endpoint opt-in
This commit is contained in:
parent
ec5226ebdb
commit
f4e29f1b9c
|
@ -28,6 +28,7 @@ type Config struct {
|
||||||
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
|
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
|
||||||
EdidString string `json:"hdmi_edid_string"`
|
EdidString string `json:"hdmi_edid_string"`
|
||||||
ActiveExtension string `json:"active_extension"`
|
ActiveExtension string `json:"active_extension"`
|
||||||
|
MetricsEnabled bool `json:"enable_metrics"`
|
||||||
DisplayMaxBrightness int `json:"display_max_brightness"`
|
DisplayMaxBrightness int `json:"display_max_brightness"`
|
||||||
DisplayDimAfterSec int `json:"display_dim_after_sec"`
|
DisplayDimAfterSec int `json:"display_dim_after_sec"`
|
||||||
DisplayOffAfterSec int `json:"display_off_after_sec"`
|
DisplayOffAfterSec int `json:"display_off_after_sec"`
|
||||||
|
|
|
@ -3,8 +3,10 @@ package kvm
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
|
versioncollector "github.com/prometheus/client_golang/prometheus/collectors/version"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
"github.com/prometheus/common/version"
|
"github.com/prometheus/common/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,4 +16,17 @@ func initPrometheus() {
|
||||||
// A Prometheus metrics endpoint.
|
// A Prometheus metrics endpoint.
|
||||||
version.Version = builtAppVersion
|
version.Version = builtAppVersion
|
||||||
prometheus.MustRegister(versioncollector.NewCollector("jetkvm"))
|
prometheus.MustRegister(versioncollector.NewCollector("jetkvm"))
|
||||||
|
|
||||||
|
promHandler = promhttp.Handler()
|
||||||
|
}
|
||||||
|
|
||||||
|
func prometheusCheckAuthMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
if !config.MetricsEnabled {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Metrics endpoint is disabled"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
3
web.go
3
web.go
|
@ -12,7 +12,6 @@ import (
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -87,7 +86,7 @@ func setupRouter() *gin.Engine {
|
||||||
r.POST("/device/setup", handleSetup)
|
r.POST("/device/setup", handleSetup)
|
||||||
|
|
||||||
// A Prometheus metrics endpoint.
|
// A Prometheus metrics endpoint.
|
||||||
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
|
r.GET("/metrics", prometheusCheckAuthMiddleware(), gin.WrapH(promHandler))
|
||||||
|
|
||||||
// Protected routes (allows both password and noPassword modes)
|
// Protected routes (allows both password and noPassword modes)
|
||||||
protected := r.Group("/")
|
protected := r.Group("/")
|
||||||
|
|
Loading…
Reference in New Issue