Skip to content

Commit

Permalink
Add output timestamp and location name
Browse files Browse the repository at this point in the history
  • Loading branch information
triole committed Sep 1, 2024
1 parent 0668e96 commit 305ac0a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/capitals/capitals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion src/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/location/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package location

type Location struct {
Capital string
Name string
Country string
Coords Coords
}
Expand Down
10 changes: 7 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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]")
}
5 changes: 3 additions & 2 deletions src/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 305ac0a

Please sign in to comment.