From 27010159264bdfdab70e567572bc0646167d57e6 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Sun, 1 Sep 2024 10:28:34 +0200 Subject: [PATCH] Add lat lon param processor --- src/location.go | 4 ++-- src/main.go | 6 +++--- src/server.go | 31 ++++++++++++++++++++----------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/location.go b/src/location.go index 65f8daf..0727dba 100644 --- a/src/location.go +++ b/src/location.go @@ -8,10 +8,10 @@ import ( func getLocation(arr []string) (loc location.Location) { caps := capitals.Init() if len(arr) == 1 { - loc = caps.GetLocation(CLI.Location[0]) + loc = caps.GetLocation(arr[0]) } if len(arr) > 1 { - coords, err := parseCoords(CLI.Location) + coords, err := parseCoords(arr) if err == nil { loc.Capital = "custom" loc.Coords.Lat = coords.Lat diff --git a/src/main.go b/src/main.go index 300cd87..3c43260 100644 --- a/src/main.go +++ b/src/main.go @@ -60,8 +60,8 @@ func parseCoords(arr []string) (c tCoords, err error) { } func displayErr() { - fmt.Println("Failed to parse args.") - fmt.Println("Location required, either capital's name or lat lon coordinates") - fmt.Println("Examples: astrocalc berlin, astrocalc 55.2 22.1") + fmt.Println("can not parse args, location required") + fmt.Println("use either capital's name or lat lon coordinates") + fmt.Println("example: astrocalc berlin, astrocalc 55.2 22.1") os.Exit(1) } diff --git a/src/server.go b/src/server.go index 85d7b78..df01be6 100644 --- a/src/server.go +++ b/src/server.go @@ -1,7 +1,7 @@ package main import ( - "astrocalc/src/capitals" + "astrocalc/src/location" "encoding/json" "fmt" "net/http" @@ -19,20 +19,29 @@ func runServer() { } func returnHandler(w http.ResponseWriter, r *http.Request) { - location := r.URL.Query().Get("loc") - if location != "" { + paramLoc := r.URL.Query().Get("loc") + paramLat := r.URL.Query().Get("lat") + paramLon := r.URL.Query().Get("lon") + + if paramLoc == "" && paramLat == "" && paramLon == "" { + fmt.Fprintf( + w, + "location request parameter missing, e.g. %q or %q", + "?loc=london", "?lat=51.50216&lon=-0.109", + ) + } else { + + var loc location.Location + + if paramLoc != "" { + loc = getLocation([]string{paramLoc}) + } else if paramLat != "" && paramLon != "" { + loc = getLocation([]string{paramLat, paramLon}) + } now := time.Now() - caps := capitals.Init() - loc := caps.GetLocation(location) data := calc(now, loc.Coords.Lat, loc.Coords.Lon) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(data) - } else { - fmt.Fprintf( - w, "location request parameter missing, add something like %s", - "?loc=london", - ) } - }