Refactor branches in custom error pages

Signed-off-by: Ricardo Lopes <ricardoapl.dev@gmail.com>
This commit is contained in:
Ricardo Lopes 2024-02-14 11:50:43 +00:00
parent 20e86ed27a
commit ddd1c90f5a

View file

@ -110,22 +110,23 @@ func main() {
metricsPort = os.Getenv(MetricsPortVar) metricsPort = os.Getenv(MetricsPortVar)
} }
var mux *http.ServeMux var errorsMux, metricsMux *http.ServeMux
if isExportMetrics { if isExportMetrics {
if metricsPort == CustomErrorPagesPort { if metricsPort == CustomErrorPagesPort {
mux = http.DefaultServeMux errorsMux = http.DefaultServeMux
mux.Handle("/metrics", promhttp.Handler()) errorsMux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { errorsMux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}) })
} else { } else {
mux = http.NewServeMux() errorsMux = http.NewServeMux()
http.Handle("/metrics", promhttp.Handler()) metricsMux = http.DefaultServeMux
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { metricsMux.Handle("/metrics", promhttp.Handler())
metricsMux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}) })
go func() { go func() {
err := http.ListenAndServe(fmt.Sprintf(":%s", metricsPort), nil) err := http.ListenAndServe(fmt.Sprintf(":%s", metricsPort), metricsMux)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -134,11 +135,11 @@ func main() {
} else { } else {
// Use a new ServerMux because expvar HTTP handler registers itself against DefaultServerMux // Use a new ServerMux because expvar HTTP handler registers itself against DefaultServerMux
// as a consequence of importing it in client_golang/prometheus. // as a consequence of importing it in client_golang/prometheus.
mux = http.NewServeMux() errorsMux = http.NewServeMux()
} }
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat)) errorsMux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), mux) err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), errorsMux)
if err != nil { if err != nil {
panic(err) panic(err)
} }