ambient-local-exporter/pkg/ambient/http.go

62 lines
1.5 KiB
Go
Raw Normal View History

2025-01-04 18:15:19 +00:00
package ambient
import (
"context"
"io"
"net/http"
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
"github.com/rs/zerolog"
"gitea.libretechconsulting.com/rmcguire/ambient-weather-local-exporter/pkg/awn"
)
func GetAWNHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
l := zerolog.Ctx(appCtx)
tracer := otel.GetTracer(appCtx, "awn.http.handler")
return func(w http.ResponseWriter, r *http.Request) {
_, span := tracer.Start(r.Context(), "update")
defer span.End()
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
l.Trace().Bytes("body", bodyBytes)
l.Trace().Any("request", r.URL.Query()).Send()
update, err := awn.UnmarshalQueryParams(r.URL.Query())
if err != nil {
l.Err(err).Send()
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
}
l.Trace().Any("update", update).Send()
w.Write([]byte("ok"))
}
}
func GetWundergroundHandlerFunc(appCtx context.Context) func(http.ResponseWriter, *http.Request) {
l := zerolog.Ctx(appCtx)
tracer := otel.GetTracer(appCtx, "wunderground.http.handler")
return func(w http.ResponseWriter, r *http.Request) {
_, span := tracer.Start(r.Context(), "update")
defer span.End()
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
l.Trace().Bytes("body", bodyBytes)
l.Trace().Any("request", r.URL.Query()).Send()
w.Write([]byte("ok"))
}
}