mirror of
https://github.com/chubin/wttr.in
synced 2025-01-26 10:45:01 +00:00
42 lines
789 B
Go
42 lines
789 B
Go
|
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),
|
||
|
)}
|
||
|
}
|