Refactor and rename custom error pages vars

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

View file

@ -69,13 +69,13 @@ const (
// cannot be mapped to a file extension. // cannot be mapped to a file extension.
DefaultFormatVar = "DEFAULT_RESPONSE_FORMAT" DefaultFormatVar = "DEFAULT_RESPONSE_FORMAT"
// IsExposeSignalsVar is the name of the environment variable indicating // IsExportMetricsVar is the name of the environment variable indicating
// whether or not to expose signals such as /metrics and /healthz. // whether or not to export /metrics and /healthz.
IsExposeSignalsVar = "IS_EXPOSE_SIGNALS" IsExportMetricsVar = "IS_EXPORT_METRICS"
// ExposeSignalsPortVar is the name of the environment variable indicating // MetricsPortVar is the name of the environment variable indicating
// the port on which to expose signals such as /metrics and /healthz. // the port on which to export /metrics and /healthz.
ExposeSignalsPortVar = "EXPOSE_SIGNALS_PORT" MetricsPortVar = "METRICS_PORT"
// CustomErrorPagesPort is the port on which to listen to serve custom error pages. // CustomErrorPagesPort is the port on which to listen to serve custom error pages.
CustomErrorPagesPort = "8080" CustomErrorPagesPort = "8080"
@ -97,60 +97,51 @@ func main() {
defaultFormat = os.Getenv(DefaultFormatVar) defaultFormat = os.Getenv(DefaultFormatVar)
} }
isExposeSignals := true isExportMetrics := true
if os.Getenv(IsExposeSignalsVar) != "" { if os.Getenv(IsExportMetricsVar) != "" {
val, err := strconv.ParseBool(os.Getenv(IsExposeSignalsVar)) val, err := strconv.ParseBool(os.Getenv(IsExportMetricsVar))
if err == nil { if err == nil {
isExposeSignals = val isExportMetrics = val
} }
} }
exposeSignalsPort := "8080" metricsPort := "8080"
if os.Getenv(ExposeSignalsPortVar) != "" { if os.Getenv(MetricsPortVar) != "" {
exposeSignalsPort = os.Getenv(ExposeSignalsPortVar) metricsPort = os.Getenv(MetricsPortVar)
} }
var mux *http.ServeMux var mux *http.ServeMux
if isExposeSignals { if isExportMetrics {
if exposeSignalsPort == CustomErrorPagesPort { if metricsPort == CustomErrorPagesPort {
mux = http.DefaultServeMux mux = http.DefaultServeMux
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
mux.Handle("/metrics", promhttp.Handler()) mux.Handle("/metrics", promhttp.Handler())
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
}) })
err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), mux)
if err != nil {
panic(err)
}
} else { } else {
mux = http.NewServeMux() mux = http.NewServeMux()
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { http.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", CustomErrorPagesPort), mux) err := http.ListenAndServe(fmt.Sprintf(":%s", metricsPort), nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }
}() }()
err := http.ListenAndServe(fmt.Sprintf(":%s", exposeSignalsPort), nil)
if err != nil {
panic(err)
}
} }
} 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() mux = http.NewServeMux()
}
mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat)) mux.HandleFunc("/", errorHandler(errFilesPath, defaultFormat))
err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), mux) err := http.ListenAndServe(fmt.Sprintf(":%s", CustomErrorPagesPort), mux)
if err != nil { if err != nil {
panic(err) panic(err)
} }
}
} }
func errorHandler(path, defaultFormat string) func(http.ResponseWriter, *http.Request) { func errorHandler(path, defaultFormat string) func(http.ResponseWriter, *http.Request) {