geocoding added, but working so-so, need to fix some lines

This commit is contained in:
2026-03-26 21:37:52 +05:00
parent 2718c8ddb2
commit ea0d710428
8 changed files with 287 additions and 33 deletions

View File

@@ -7,6 +7,8 @@ import (
"fmt"
"log/slog"
"net/http"
neturl "net/url"
"strconv"
"time"
"github.com/anxi0uz/logiflow/internal/api"
@@ -28,16 +30,16 @@ const (
tokenKey ctxKey = "Authorization"
// response messages
MsgInternalError = "Internal server error"
MsgInvalidBody = "Invalid request body"
MsgNotFound = "Not found"
MsgUnauthorized = "Unauthorized"
MsgMissingToken = "Missing token"
MsgForbidden = "Forbidden"
MsgInternalError = "Internal server error"
MsgInvalidBody = "Invalid request body"
MsgNotFound = "Not found"
MsgUnauthorized = "Unauthorized"
MsgMissingToken = "Missing token"
MsgForbidden = "Forbidden"
// response types
RespError = "error"
RespSuccess = "success"
RespError = "error"
RespSuccess = "success"
RespNotFound = "not found"
)
@@ -234,3 +236,36 @@ func (s *Server) validateAccessToken(ctx context.Context, tokenStr string) (*Cla
return claims, nil
}
func (s *Server) geocode(ctx context.Context, address string) (lat, lon float64, err error) {
url := fmt.Sprintf(
"https://nominatim.openstreetmap.org/search?q=%s&format=json&limit=1",
neturl.QueryEscape(address),
)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return 0, 0, err
}
req.Header.Set("User-Agent", "logiflow/1.0")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, 0, err
}
defer resp.Body.Close()
var results []struct {
Lat string `json:"lat"`
Lon string `json:"lon"`
}
if err := json.NewDecoder(resp.Body).Decode(&results); err != nil {
return 0, 0, err
}
if len(results) == 0 {
return 0, 0, fmt.Errorf("address not found: %s", address)
}
lat, _ = strconv.ParseFloat(results[0].Lat, 64)
lon, _ = strconv.ParseFloat(results[0].Lon, 64)
return lat, lon, nil
}