api: move prometheus handler inside api handler /v1/metrics

This commit is contained in:
yusing 2025-02-01 02:09:43 +08:00
parent 6ae391a3c9
commit 0d518166ee
4 changed files with 10 additions and 17 deletions

View file

@ -42,8 +42,8 @@ GODOXY_HTTPS_ADDR=:443
# API listening address # API listening address
GODOXY_API_ADDR=127.0.0.1:8888 GODOXY_API_ADDR=127.0.0.1:8888
# Prometheus Metrics listening address (uncomment to enable) # Prometheus Metrics
#GODOXY_PROMETHEUS_ADDR=:8889 GODOXY_PROMETHEUS_ENABLED=true
# Debug mode # Debug mode
GODOXY_DEBUG=false GODOXY_DEBUG=false

View file

@ -3,9 +3,11 @@ package api
import ( import (
"net/http" "net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
v1 "github.com/yusing/go-proxy/internal/api/v1" v1 "github.com/yusing/go-proxy/internal/api/v1"
"github.com/yusing/go-proxy/internal/api/v1/auth" "github.com/yusing/go-proxy/internal/api/v1/auth"
"github.com/yusing/go-proxy/internal/api/v1/favicon" "github.com/yusing/go-proxy/internal/api/v1/favicon"
"github.com/yusing/go-proxy/internal/common"
config "github.com/yusing/go-proxy/internal/config/types" config "github.com/yusing/go-proxy/internal/config/types"
"github.com/yusing/go-proxy/internal/utils/strutils" "github.com/yusing/go-proxy/internal/utils/strutils"
) )
@ -36,6 +38,10 @@ func NewHandler(cfg config.ConfigInstance) http.Handler {
mux.HandleFunc("GET", "/v1/favicon", auth.RequireAuth(favicon.GetFavIcon)) mux.HandleFunc("GET", "/v1/favicon", auth.RequireAuth(favicon.GetFavIcon))
mux.HandleFunc("POST", "/v1/homepage/set", auth.RequireAuth(v1.SetHomePageOverrides)) mux.HandleFunc("POST", "/v1/homepage/set", auth.RequireAuth(v1.SetHomePageOverrides))
if common.PrometheusEnabled {
mux.Handle("GET /v1/metrics", promhttp.Handler())
}
defaultAuth := auth.GetDefaultAuth() defaultAuth := auth.GetDefaultAuth()
if defaultAuth != nil { if defaultAuth != nil {
mux.HandleFunc("GET", "/v1/auth/redirect", defaultAuth.RedirectLoginPage) mux.HandleFunc("GET", "/v1/auth/redirect", defaultAuth.RedirectLoginPage)

View file

@ -38,11 +38,7 @@ var (
APIHTTPPort, APIHTTPPort,
APIHTTPURL = GetAddrEnv("API_ADDR", "127.0.0.1:8888", "http") APIHTTPURL = GetAddrEnv("API_ADDR", "127.0.0.1:8888", "http")
MetricsHTTPAddr, PrometheusEnabled = GetEnvBool("PROMETHEUS_ENABLED", false)
MetricsHTTPHost,
MetricsHTTPPort,
MetricsHTTPURL = GetAddrEnv("PROMETHEUS_ADDR", "", "http")
PrometheusEnabled = MetricsHTTPURL != ""
APIJWTSecret = decodeJWTKey(GetEnvString("API_JWT_SECRET", "")) APIJWTSecret = decodeJWTKey(GetEnvString("API_JWT_SECRET", ""))
APIJWTTokenTTL = GetDurationEnv("API_JWT_TOKEN_TTL", time.Hour) APIJWTTokenTTL = GetDurationEnv("API_JWT_TOKEN_TTL", time.Hour)

View file

@ -15,7 +15,6 @@ import (
"github.com/yusing/go-proxy/internal/entrypoint" "github.com/yusing/go-proxy/internal/entrypoint"
E "github.com/yusing/go-proxy/internal/error" E "github.com/yusing/go-proxy/internal/error"
"github.com/yusing/go-proxy/internal/logging" "github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/metrics"
"github.com/yusing/go-proxy/internal/net/http/server" "github.com/yusing/go-proxy/internal/net/http/server"
"github.com/yusing/go-proxy/internal/notif" "github.com/yusing/go-proxy/internal/notif"
proxy "github.com/yusing/go-proxy/internal/route/provider" proxy "github.com/yusing/go-proxy/internal/route/provider"
@ -182,7 +181,7 @@ func (cfg *Config) StartProxyProviders() {
} }
type StartServersOptions struct { type StartServersOptions struct {
Proxy, API, Metrics bool Proxy, API bool
} }
func (cfg *Config) StartServers(opts ...*StartServersOptions) { func (cfg *Config) StartServers(opts ...*StartServersOptions) {
@ -207,14 +206,6 @@ func (cfg *Config) StartServers(opts ...*StartServersOptions) {
Handler: api.NewHandler(cfg), Handler: api.NewHandler(cfg),
}) })
} }
if opt.Metrics && common.PrometheusEnabled {
server.StartServer(cfg.task, server.Options{
Name: "metrics",
CertProvider: cfg.AutoCertProvider(),
HTTPAddr: common.MetricsHTTPAddr,
Handler: metrics.NewHandler(),
})
}
} }
func (cfg *Config) load() E.Error { func (cfg *Config) load() E.Error {