Skip to content

Commit

Permalink
inject opts instead of context.withValue
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychase committed Feb 16, 2022
1 parent 0b33606 commit 4847311
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 148 deletions.
207 changes: 101 additions & 106 deletions cmd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,120 +10,115 @@ import (
"github.com/ebarkie/aprs"
)

func awpHandlerV1(w http.ResponseWriter, req *http.Request) {
opts, err := ctxOptions(req.Context())
if err != nil {
msg := fmt.Sprintf("error: %s", err)
errorHandler(w, req, msg, http.StatusInternalServerError)
return
}

wx := aprs.Wx{
Lat: opts.latitude,
Lon: opts.longitude,
Type: opts.comment,
}
func awpHandlerV1(opts options) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
wx := aprs.Wx{
Lat: float64(opts.latitude),
Lon: float64(opts.longitude),
Type: opts.comment,
}

// SwName are SwVers are concatenated in the 'comment' field and then
// immediately followed by the Wx.Type. This is performed in the upstream
// aprs library and looks like:
//
// fmt.Sprintf("%s%s%s", aprs.SwName, aprs.SwVers, wx.Type)
//
// On aprs.fi a lowercase 'v%d' gets dropped, so that is why this is 'V'.
aprs.SwName = "wxigate-V"
aprs.SwVers = Version
// SwName are SwVers are concatenated in the 'comment' field and then
// immediately followed by the Wx.Type. This is performed in the upstream
// aprs library and looks like:
//
// fmt.Sprintf("%s%s%s", aprs.SwName, aprs.SwVers, wx.Type)
//
// On aprs.fi a lowercase 'v%d' gets dropped, so that is why this is 'V'.
aprs.SwName = "wxigate-V"
aprs.SwVers = Version

query := req.URL.Query()
query := req.URL.Query()

for k, v := range query {
switch k {
case "dateutc":
layout := "2006-01-02 15:04:05"
dateutc, err := time.Parse(layout, v[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Timestamp = dateutc
case "baromrelin":
baromrelin, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Altimeter = baromrelin
case "tempf":
temp, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Temp = int(temp)
case "humidity":
humidity, err := strconv.Atoi(v[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Humidity = humidity
case "hourlyrainin":
hourlyrainin, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.RainLastHour = hourlyrainin
case "24hourrainin":
hourlyrainin24, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.RainLast24Hours = hourlyrainin24
case "dailyrainin":
dailyrainin, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
for k, v := range query {
switch k {
case "dateutc":
layout := "2006-01-02 15:04:05"
dateutc, err := time.Parse(layout, v[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Timestamp = dateutc
case "baromrelin":
baromrelin, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Altimeter = baromrelin
case "tempf":
temp, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Temp = int(temp)
case "humidity":
humidity, err := strconv.Atoi(v[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.Humidity = humidity
case "hourlyrainin":
hourlyrainin, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.RainLastHour = hourlyrainin
case "24hourrainin":
hourlyrainin24, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.RainLast24Hours = hourlyrainin24
case "dailyrainin":
dailyrainin, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.RainToday = dailyrainin
case "solarradiation":
solarradiation, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.SolarRad = int(solarradiation)
case "winddir":
winddir, err := strconv.Atoi(v[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.WindDir = winddir
case "windgustmph":
windgustmph, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.WindGust = int(windgustmph)
case "windspeedmph":
windspeedmph, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.WindSpeed = int(windspeedmph)
}
wx.RainToday = dailyrainin
case "solarradiation":
solarradiation, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.SolarRad = int(solarradiation)
case "winddir":
winddir, err := strconv.Atoi(v[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.WindDir = winddir
case "windgustmph":
windgustmph, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.WindGust = int(windgustmph)
case "windspeedmph":
windspeedmph, err := strconv.ParseFloat(v[0], 64)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v", err) // BUG(medium) change
}
wx.WindSpeed = int(windspeedmph)
}
}

fmt.Printf("sending, temp(%d): %v\n", wx.Temp, wx.String())
fmt.Printf("sending, temp(%d): %v\n", wx.Temp, wx.String())

f := aprs.Frame{
Dst: aprs.Addr{Call: "APRS"},
Src: aprs.Addr{Call: fmt.Sprintf("%s-%s", opts.callsign, opts.ssid)},
Path: aprs.Path{aprs.Addr{Call: "TCPIP", Repeated: true}},
Text: wx.String(),
}
err = f.SendIS("tcp://cwop.aprs.net:14580", -1) //BUG(medium) flag
if err != nil {
msg := fmt.Sprintf("Upload error: %s", err)
errorHandler(w, req, msg, http.StatusServiceUnavailable)
return
}
f := aprs.Frame{
Dst: aprs.Addr{Call: "APRS"},
Src: aprs.Addr{Call: fmt.Sprintf("%s-%s", opts.callsign, opts.ssid)},
Path: aprs.Path{aprs.Addr{Call: "TCPIP", Repeated: true}},
Text: wx.String(),
}
err := f.SendIS("tcp://cwop.aprs.net:14580", -1) //BUG(medium) flag
if err != nil {
msg := fmt.Sprintf("Upload error: %s", err)
errorHandler(w, req, msg, http.StatusServiceUnavailable)
return
}

w.WriteHeader(http.StatusOK)
w.WriteHeader(http.StatusOK)
})
}

func errorHandler(w http.ResponseWriter, req *http.Request, msg string, status int) {
Expand Down
16 changes: 9 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func Body() int {
return 0
}

opts, err = validate(opts)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n\n", err)
flag.Usage()
return 1
}

err = server(opts)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
Expand All @@ -44,15 +51,10 @@ func parseArgs() (options, error) {
flag.StringVar(&opts.comment, "comment", "", "comment")
flag.StringVar(&opts.ssid, "ssid", "15", "ssid")
flag.UintVar(&opts.port, "port", DEFAULT_PORT, "tcp port (automatic 0)")
flag.StringVar(&opts.inputAddress, "address", DEFAULT_ADDRESS_IPV4, "IP address")
flag.StringVar(&opts.argAddress, "address", DEFAULT_ADDRESS_IPV4, "IP address")

flag.Parse()

opts, err := validate(opts)
if err != nil {
return opts, err
}

return opts, nil
}

Expand All @@ -66,7 +68,7 @@ func validate(opts options) (options, error) {
}

// address validation
opts.address = net.ParseIP(opts.inputAddress)
opts.address = net.ParseIP(opts.argAddress)
if opts.address == nil {
return opts, fmt.Errorf("invalid address")
}
Expand Down
35 changes: 9 additions & 26 deletions cmd/options.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
package cmd

import (
"context"
"errors"
"net"
)

type optionKey string
type options struct {
inputAddress string // BUG(low) remove
address net.IP
port uint
callsign string
comment string
ssid string
longitude float64
latitude float64
showVersion bool
}

func ctxWithOptions(ctx context.Context, opts options) context.Context {
key := optionKey("options")
return context.WithValue(context.Background(), key, opts)
}

func ctxOptions(ctx context.Context) (options, error) {
opts, ok := ctx.Value(optionKey("options")).(options)
if !ok {
return opts, errors.New("unable to return options")
}

return opts, nil
argAddress string
callsign string
comment string
ssid string
address net.IP
longitude float64
latitude float64
port uint
showVersion bool
}
11 changes: 2 additions & 9 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"context"
"fmt"
"net"
"net/http"
Expand All @@ -16,21 +15,15 @@ func server(opts options) error {

// Routes
http.HandleFunc("/", defaultHandler)
http.HandleFunc("/wxigate/awp/v1", awpHandlerV1)
http.Handle("/wxigate/awp/v1", awpHandlerV1(opts))

_, err = fmt.Printf("%s-%s %f %f listening on: %s\n", opts.callsign, opts.ssid, opts.longitude, opts.latitude, listener.Addr().String())
if err != nil {
return err
}

srvr := &http.Server{
BaseContext: func(_ net.Listener) context.Context {
return ctxWithOptions(context.Background(), opts)
},
}

// blocks until err
err = srvr.Serve(listener)
err = http.Serve(listener, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 4847311

Please sign in to comment.