Skip to content

Commit

Permalink
Add lat lon param processor
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Sep 1, 2024
1 parent 2a3e277 commit 2701015
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 20 additions & 11 deletions src/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"astrocalc/src/capitals"
"astrocalc/src/location"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -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",
)
}

}

0 comments on commit 2701015

Please sign in to comment.