Skip to content

Commit

Permalink
ref: move server function to internal/
Browse files Browse the repository at this point in the history
for easier integration testing
  • Loading branch information
wbollock committed Oct 9, 2023
1 parent e44d31c commit a896e1a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
31 changes: 6 additions & 25 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,15 @@ import (
"net/http"
"os"

"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/wbollock/ping_exporter/internal/collector"
"github.com/wbollock/ping_exporter/internal/server"
)

const (
version = "0.1.0"
defaultHTML = `<html>
<head><title>Ping Exporter</title></head>
<body>
<h1>Ping Exporter</h1>
<p><a href='%s'>Metrics</a></p>
</body>
</html>`
version = "0.1.0"
defaultLogLevel = "info"
defaultListenAddress = ":9141"
defaultMetricsPath = "/metrics"
defaultLogLevel = "info"
)

var (
Expand All @@ -37,14 +29,6 @@ func printVersion() {
fmt.Printf("ping_exporter\nVersion: %s\nmulti-target ICMP prometheus exporter\n", version)
}

func mainPage(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")
}
}

func main() {
flag.Parse()

Expand All @@ -61,13 +45,10 @@ func main() {
log.SetLevel(log.InfoLevel)
}

log.Info("Listening on address ", *listenAddress)

http.Handle(*metricsPath, promhttp.Handler())
http.HandleFunc("/probe", collector.PingHandler)
http.HandleFunc("/", mainPage)
handler := server.SetupServer(*metricsPath)

if err := http.ListenAndServe(*listenAddress, nil); err != nil {
log.Infof("Starting server on %s", *listenAddress)
if err := http.ListenAndServe(*listenAddress, handler); err != nil {
log.WithError(err).Fatal("Failed to start the server")
}
}
4 changes: 2 additions & 2 deletions internal/collector/icmp_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func PingHandler(w http.ResponseWriter, r *http.Request) {
log.Debug("Request received with parameters ", p)

// TODO: ensure ResolveIPAddr is the best way to do lookups
// TODO: fails with IP address
// TODO: fails with IP address as target
ra, err := net.ResolveIPAddr(p.protocol, p.target)
if err != nil {
log.Error(err)
Expand All @@ -170,7 +170,7 @@ func PingHandler(w http.ResponseWriter, r *http.Request) {

if p.protocol == "icmp" {
pinger.SetPrivileged(true)
} else { // udp
} else {
pinger.SetPrivileged(false)
}

Expand Down
37 changes: 37 additions & 0 deletions internal/server/server.go
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
}

0 comments on commit a896e1a

Please sign in to comment.