Skip to content

Commit

Permalink
Configurable Content-Type and HTTP endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
susji committed Apr 9, 2024
1 parent 5b73a01 commit 1d27692
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ import (
)

var (
STATE = state.New()
VERBOSE = false
STATE = state.New()
VERBOSE = false
CT = "text/plain; version=0.0.4"
ENDPOINT = "/metrics"
)

func metrics(w http.ResponseWriter, r *http.Request) {
if VERBOSE {
log.Println("/metrics")
log.Println(ENDPOINT)
}
w.Header().Add("content-type", "text/plain; version=0.0.4")
w.Header().Add("content-type", CT)
fmt.Fprintln(w, "# HELP ruuvi_temperature", HELP_TEMP)
fmt.Fprintln(w, "# TYPE ruuvi_temperature gauge")
for mac, v := range STATE.Temperatures() {
Expand Down Expand Up @@ -107,10 +109,12 @@ func metrics(w http.ResponseWriter, r *http.Request) {
func main() {
var l string
flag.StringVar(&l, "listen", "localhost:9900", "Listen address")
flag.StringVar(&CT, "content-type", CT, "Content-Type header in responses")
flag.StringVar(&ENDPOINT, "endpoint", ENDPOINT, "HTTP endpoint for metrics")
flag.BoolVar(&VERBOSE, "verbose", false, "Verbose output")
flag.Parse()
log.Println("starting to listen at", l, "and verbosity is", VERBOSE)
http.HandleFunc("/metrics", metrics)
http.HandleFunc(ENDPOINT, metrics)
wg := &sync.WaitGroup{}
wg.Add(2)
s := &http.Server{Addr: l}
Expand Down

0 comments on commit 1d27692

Please sign in to comment.