v1_ip2location.go 744 B

12345678910111213141516171819202122232425262728293031
  1. package v1_ip2location
  2. import (
  3. "net/http"
  4. "github.com/vladimirok5959/golang-ip2location/internal/client"
  5. "github.com/vladimirok5959/golang-ip2location/internal/server/handler/base"
  6. "github.com/vladimirok5959/golang-utils/utils/http/apiserv"
  7. "github.com/vladimirok5959/golang-utils/utils/http/helpers"
  8. "github.com/vladimirok5959/golang-utils/utils/http/render"
  9. )
  10. type Handler struct {
  11. base.Handler
  12. }
  13. func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  14. var res *client.Result
  15. var err error
  16. if h.Client != nil {
  17. if res, err = h.Client.IP2Location(r.Context(), apiserv.GetParams(r)[1].String()); err != nil {
  18. helpers.RespondAsBadRequest(w, r, err)
  19. return
  20. }
  21. }
  22. if !render.JSON(w, r, res) {
  23. return
  24. }
  25. }