mirror of https://github.com/chubin/wttr.in
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
848 B
45 lines
848 B
package location
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"github.com/chubin/wttr.in/internal/routing"
|
|
)
|
|
|
|
// Response provides routing interface to the geo cache.
|
|
func (c *Cache) Response(r *http.Request) *routing.Cadre {
|
|
var (
|
|
locationName = r.URL.Query().Get("location")
|
|
loc *Location
|
|
bytes []byte
|
|
err error
|
|
)
|
|
|
|
if locationName == "" {
|
|
return errorResponse("location is not specified")
|
|
}
|
|
|
|
loc, err = c.Resolve(locationName)
|
|
if err != nil {
|
|
log.Println("geo/location error:", locationName)
|
|
|
|
return errorResponse(fmt.Sprint(err))
|
|
}
|
|
|
|
bytes, err = json.Marshal(loc)
|
|
if err != nil {
|
|
return errorResponse(fmt.Sprint(err))
|
|
}
|
|
|
|
return &routing.Cadre{Body: bytes}
|
|
}
|
|
|
|
func errorResponse(s string) *routing.Cadre {
|
|
return &routing.Cadre{Body: []byte(
|
|
fmt.Sprintf(`{"error": %q}`, s),
|
|
)}
|
|
}
|