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.
42 lines
789 B
42 lines
789 B
![]()
2 years ago
|
package location
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"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 {
|
||
|
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),
|
||
|
)}
|
||
|
}
|