Compare commits
	
		
			3 Commits
		
	
	
		
			v0.5.1
			...
			44bf293eab
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 44bf293eab | |||
| f87b35b306 | |||
| eaf2cf211d | 
							
								
								
									
										2
									
								
								TODO.md
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								TODO.md
									
									
									
									
									
								
							@@ -4,6 +4,8 @@
 | 
				
			|||||||
- [ ] Add Grafana dashboard
 | 
					- [ ] Add Grafana dashboard
 | 
				
			||||||
- [ ] Add proxy to upstream support
 | 
					- [ ] Add proxy to upstream support
 | 
				
			||||||
- [ ] Add new spans
 | 
					- [ ] Add new spans
 | 
				
			||||||
 | 
					- [ ] Fix wunderground 401
 | 
				
			||||||
 | 
					- [ ] Perform proxy calls in goroutines
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Done
 | 
					## Done
 | 
				
			||||||
- [x] Configuration for app
 | 
					- [x] Configuration for app
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
 | 
						"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
 | 
				
			||||||
	"github.com/rs/zerolog"
 | 
						"github.com/rs/zerolog"
 | 
				
			||||||
@@ -120,26 +121,40 @@ func (aw *AmbientWeather) handleProviderRequest(
 | 
				
			|||||||
	// Uses a weather update to allow awn to publish to wunderground and
 | 
						// Uses a weather update to allow awn to publish to wunderground and
 | 
				
			||||||
	// visa versa.
 | 
						// visa versa.
 | 
				
			||||||
	if station := update.StationConfig; station != nil {
 | 
						if station := update.StationConfig; station != nil {
 | 
				
			||||||
 | 
							// Perform proxy updates in parallel if enabled
 | 
				
			||||||
 | 
							var proxyWg sync.WaitGroup
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if station.ProxyToAWN {
 | 
							if station.ProxyToAWN {
 | 
				
			||||||
			err := aw.awnProvider.ProxyReq(ctx, update)
 | 
								proxyWg.Add(1)
 | 
				
			||||||
			if err != nil {
 | 
								go func() {
 | 
				
			||||||
				zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
 | 
									defer proxyWg.Done()
 | 
				
			||||||
				return
 | 
									err := aw.awnProvider.ProxyReq(ctx, update)
 | 
				
			||||||
			}
 | 
									if err != nil {
 | 
				
			||||||
			zerolog.Ctx(aw.appCtx).Debug().
 | 
										zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
 | 
				
			||||||
				Str("station", station.Name).
 | 
										return
 | 
				
			||||||
				Msg("proxied weather update to awn")
 | 
									}
 | 
				
			||||||
 | 
									zerolog.Ctx(aw.appCtx).Debug().
 | 
				
			||||||
 | 
										Str("station", station.Name).
 | 
				
			||||||
 | 
										Msg("proxied weather update to awn")
 | 
				
			||||||
 | 
								}()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if station.ProxyToWunderground {
 | 
							if station.ProxyToWunderground {
 | 
				
			||||||
			err := aw.wuProvider.ProxyReq(ctx, update)
 | 
								proxyWg.Add(1)
 | 
				
			||||||
			if err != nil {
 | 
								go func() {
 | 
				
			||||||
				zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
 | 
									defer proxyWg.Done()
 | 
				
			||||||
				return
 | 
									err := aw.wuProvider.ProxyReq(ctx, update)
 | 
				
			||||||
			}
 | 
									if err != nil {
 | 
				
			||||||
			zerolog.Ctx(aw.appCtx).Debug().
 | 
										zerolog.Ctx(aw.appCtx).Err(err).Msg("failed to proxy to ambient weather")
 | 
				
			||||||
				Str("station", station.Name).
 | 
										return
 | 
				
			||||||
				Msg("proxied weather update to wunderground")
 | 
									}
 | 
				
			||||||
 | 
									zerolog.Ctx(aw.appCtx).Debug().
 | 
				
			||||||
 | 
										Str("station", station.Name).
 | 
				
			||||||
 | 
										Msg("proxied weather update to wunderground")
 | 
				
			||||||
 | 
								}()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							proxyWg.Wait()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
 | 
						"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
 | 
				
			||||||
	"github.com/go-resty/resty/v2"
 | 
						"github.com/go-resty/resty/v2"
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
	"go.opentelemetry.io/otel/attribute"
 | 
						"go.opentelemetry.io/otel/attribute"
 | 
				
			||||||
	"go.opentelemetry.io/otel/codes"
 | 
						"go.opentelemetry.io/otel/codes"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -43,12 +44,18 @@ func (awn *AWNProvider) ProxyReq(ctx context.Context, update *weather.WeatherUpd
 | 
				
			|||||||
		Get(awnURL)
 | 
							Get(awnURL)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		span.RecordError(err)
 | 
							span.RecordError(err)
 | 
				
			||||||
 | 
							span.SetAttributes(
 | 
				
			||||||
 | 
								attribute.String("query", resp.Request.QueryParam.Encode()),
 | 
				
			||||||
 | 
								attribute.String("body", string(resp.Body())),
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
		span.SetStatus(codes.Error, err.Error())
 | 
							span.SetStatus(codes.Error, err.Error())
 | 
				
			||||||
 | 
							log.Err(err).Any("query", resp.Request.PathParams).
 | 
				
			||||||
 | 
								Int("statusCode", resp.StatusCode()).
 | 
				
			||||||
 | 
								Msg("awn proxy failed")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	span.SetAttributes(
 | 
						span.SetAttributes(
 | 
				
			||||||
		attribute.Int("statusCode", resp.StatusCode()),
 | 
							attribute.Int("statusCode", resp.StatusCode()),
 | 
				
			||||||
		attribute.String("body", string(resp.Body())),
 | 
					 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,7 @@ type WUProvider struct{}
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
	providerName = "weatherunderground"
 | 
						providerName = "weatherunderground"
 | 
				
			||||||
	wuURL        = "http://rtupdate.wunderground.com/weatherstation/updateweatherstation.php"
 | 
						wuURL        = "https://rtupdate.wunderground.com/weatherstation/updateweatherstation.php"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (wu *WUProvider) Name() string {
 | 
					func (wu *WUProvider) Name() string {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,9 +4,9 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"time"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/go-resty/resty/v2"
 | 
						"github.com/go-resty/resty/v2"
 | 
				
			||||||
 | 
						"github.com/rs/zerolog/log"
 | 
				
			||||||
	"go.opentelemetry.io/otel/attribute"
 | 
						"go.opentelemetry.io/otel/attribute"
 | 
				
			||||||
	"go.opentelemetry.io/otel/codes"
 | 
						"go.opentelemetry.io/otel/codes"
 | 
				
			||||||
	"k8s.io/utils/ptr"
 | 
						"k8s.io/utils/ptr"
 | 
				
			||||||
@@ -39,12 +39,19 @@ func (wu *WUProvider) ProxyReq(ctx context.Context, update *weather.WeatherUpdat
 | 
				
			|||||||
		Get(wuURL)
 | 
							Get(wuURL)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		span.SetStatus(codes.Error, err.Error())
 | 
							span.SetStatus(codes.Error, err.Error())
 | 
				
			||||||
 | 
							span.SetAttributes(
 | 
				
			||||||
 | 
								attribute.String("query", resp.Request.QueryParam.Encode()),
 | 
				
			||||||
 | 
								attribute.String("body", string(resp.Body())),
 | 
				
			||||||
 | 
							)
 | 
				
			||||||
		span.RecordError(err)
 | 
							span.RecordError(err)
 | 
				
			||||||
 | 
							log.Err(err).
 | 
				
			||||||
 | 
								Int("statusCode", resp.StatusCode()).
 | 
				
			||||||
 | 
								Any("query", resp.Request.PathParams).
 | 
				
			||||||
 | 
								Msg("wunderground proxy failed")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	span.SetAttributes(
 | 
						span.SetAttributes(
 | 
				
			||||||
		attribute.Int("statusCode", resp.StatusCode()),
 | 
							attribute.Int("statusCode", resp.StatusCode()),
 | 
				
			||||||
		attribute.String("body", string(resp.Body())),
 | 
					 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return err
 | 
						return err
 | 
				
			||||||
@@ -52,11 +59,11 @@ func (wu *WUProvider) ProxyReq(ctx context.Context, update *weather.WeatherUpdat
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func updateToWuParams(u *weather.WeatherUpdate) *url.Values {
 | 
					func updateToWuParams(u *weather.WeatherUpdate) *url.Values {
 | 
				
			||||||
	params := &url.Values{}
 | 
						params := &url.Values{}
 | 
				
			||||||
	params.Set("dateutc", time.Now().Format(time.DateTime))
 | 
					 | 
				
			||||||
	weather.SetURLVal(params, "ID", &u.StationConfig.WundergroundID)
 | 
						weather.SetURLVal(params, "ID", &u.StationConfig.WundergroundID)
 | 
				
			||||||
	weather.SetURLVal(params, "PASSWORD", &u.StationConfig.WundergroundPassword)
 | 
						weather.SetURLVal(params, "PASSWORD", &u.StationConfig.WundergroundPassword)
 | 
				
			||||||
 | 
						params.Set("action", "updateraw")
 | 
				
			||||||
 | 
						params.Set("dateutc", "now")
 | 
				
			||||||
	weather.SetURLVal(params, "UV", u.UV)
 | 
						weather.SetURLVal(params, "UV", u.UV)
 | 
				
			||||||
	weather.SetURLVal(params, "action", ptr.To("updateraw"))
 | 
					 | 
				
			||||||
	weather.SetURLVal(params, "baromin", u.BaromRelativeIn)
 | 
						weather.SetURLVal(params, "baromin", u.BaromRelativeIn)
 | 
				
			||||||
	weather.SetURLVal(params, "dailyrainin", u.DailyRainIn)
 | 
						weather.SetURLVal(params, "dailyrainin", u.DailyRainIn)
 | 
				
			||||||
	weather.SetURLVal(params, "dewptf", u.DewPointF)
 | 
						weather.SetURLVal(params, "dewptf", u.DewPointF)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user