-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: move server function to internal/
for easier integration testing
- Loading branch information
Showing
3 changed files
with
45 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/wbollock/ping_exporter/internal/collector" | ||
) | ||
|
||
const ( | ||
defaultHTML = `<html> | ||
<head><title>Ping Exporter</title></head> | ||
<body> | ||
<h1>Ping Exporter</h1> | ||
<p><a href='%s'>Metrics</a></p> | ||
</body> | ||
</html>` | ||
defaultMetricsPath = "/metrics" | ||
) | ||
|
||
func SetupServer(metricsPath string) http.Handler { | ||
mux := http.NewServeMux() | ||
|
||
mux.Handle(metricsPath, promhttp.Handler()) | ||
mux.HandleFunc("/probe", collector.PingHandler) | ||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
response := fmt.Sprintf(defaultHTML, metricsPath) | ||
_, err := w.Write([]byte(response)) | ||
if err != nil { | ||
log.WithError(err).Error("Failed to write main page response") | ||
} | ||
}) | ||
|
||
return mux | ||
} |