From 305ac0ae2a9b1cbd7db3a7fe1bb6e8d91e79c7b2 Mon Sep 17 00:00:00 2001 From: olaf michaelis Date: Sun, 1 Sep 2024 11:42:16 +0200 Subject: [PATCH] Add output timestamp and location name --- src/calc.go | 3 ++- src/capitals/capitals.go | 2 +- src/location.go | 3 ++- src/location/main.go | 2 +- src/main.go | 10 +++++++--- src/server.go | 5 +++-- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/calc.go b/src/calc.go index f96e8c7..839aa33 100644 --- a/src/calc.go +++ b/src/calc.go @@ -39,13 +39,14 @@ func newDataset() (ds tResults) { return } -func calc(now time.Time, lat float64, lon float64) (res tResults) { +func calc(now time.Time, lat float64, lon float64, name string) (res tResults) { res = newDataset() res.Time = now res.Location = make(map[string]interface{}) res.Location["lat"] = lat res.Location["lon"] = lon + res.Location["name"] = name // sunlight times arr := suncalc.GetTimes(now, lat, lon) diff --git a/src/capitals/capitals.go b/src/capitals/capitals.go index 223185e..96e82c7 100644 --- a/src/capitals/capitals.go +++ b/src/capitals/capitals.go @@ -33,7 +33,7 @@ func readJSON(content string) (capitals tCapitals) { func (cap tCapital) GetLocation(s string) (loc location.Location) { for _, cap := range cap.Capitals { - if strings.EqualFold(s, cap.Capital) { + if strings.EqualFold(s, cap.Name) { loc = cap break } diff --git a/src/location.go b/src/location.go index 0727dba..837f183 100644 --- a/src/location.go +++ b/src/location.go @@ -9,13 +9,14 @@ func getLocation(arr []string) (loc location.Location) { caps := capitals.Init() if len(arr) == 1 { loc = caps.GetLocation(arr[0]) + loc.Name = arr[0] } if len(arr) > 1 { coords, err := parseCoords(arr) if err == nil { - loc.Capital = "custom" loc.Coords.Lat = coords.Lat loc.Coords.Lon = coords.Lon + loc.Name = "custom" } } return loc diff --git a/src/location/main.go b/src/location/main.go index b811499..6f8e2a9 100644 --- a/src/location/main.go +++ b/src/location/main.go @@ -1,7 +1,7 @@ package location type Location struct { - Capital string + Name string Country string Coords Coords } diff --git a/src/main.go b/src/main.go index 3c43260..6bda5e5 100644 --- a/src/main.go +++ b/src/main.go @@ -21,12 +21,12 @@ func main() { } else { // try to find capital, if fails assume to be coords loc := getLocation(CLI.Location) - if loc.Capital == "" { + if loc.Name == "" { displayErr() } else { now := time.Now() - t := calc(now, loc.Coords.Lat, loc.Coords.Lon) - t.Location["name"] = loc.Capital + t := calc(now, loc.Coords.Lat, loc.Coords.Lon, loc.Name) + t.Location["name"] = loc.Name out := stringToJSON(t) @@ -65,3 +65,7 @@ func displayErr() { fmt.Println("example: astrocalc berlin, astrocalc 55.2 22.1") os.Exit(1) } + +func ts() string { + return time.Now().Format("[2006-01-02 15:04:05]") +} diff --git a/src/server.go b/src/server.go index df01be6..5c8c0c0 100644 --- a/src/server.go +++ b/src/server.go @@ -10,7 +10,7 @@ import ( ) func runServer() { - fmt.Printf("listening at %d", CLI.Port) + fmt.Printf("%s listening at %d\n", ts(), CLI.Port) http.HandleFunc("/", returnHandler) err := http.ListenAndServe(":"+strconv.Itoa(CLI.Port), nil) if err != nil { @@ -39,7 +39,8 @@ func returnHandler(w http.ResponseWriter, r *http.Request) { loc = getLocation([]string{paramLat, paramLon}) } now := time.Now() - data := calc(now, loc.Coords.Lat, loc.Coords.Lon) + data := calc(now, loc.Coords.Lat, loc.Coords.Lon, loc.Name) + fmt.Printf("%s respond for location %+v\n", ts(), data.Location) w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(data)