Skip to content

Commit

Permalink
let locationGet endpoint calls the geoip directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Nov 13, 2024
1 parent ba259a7 commit 96ddfc0
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 36 deletions.
4 changes: 0 additions & 4 deletions pkg/geoip/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/rs/zerolog/log"
)

const (
LocationFile = "/tmp/location"
)

// Location holds the result of a geoip request
type Location struct {
Longitude float64 `json:"longitude"`
Expand Down
16 changes: 0 additions & 16 deletions pkg/registrar/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package registrar
import (
"context"
"crypto/ed25519"
"encoding/json"
"fmt"
"net"
"os"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
Expand Down Expand Up @@ -146,10 +144,6 @@ func registerNode(
City: info.Location.City,
}

if err := writeLocationOnFile(info.Location, geoip.LocationFile); err != nil {
return 0, 0, errors.Wrap(err, "failed to set location on disk")
}

log.Info().Str("id", mgr.NodeID(ctx).Identity()).Msg("start registration of the node")
log.Info().Msg("registering node on blockchain")

Expand Down Expand Up @@ -237,13 +231,3 @@ func ensureTwin(ctx context.Context, substrateGateway *stubs.SubstrateGatewayStu

return twinID, nil
}

func writeLocationOnFile(loc geoip.Location, filepath string) error {
file, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return errors.Wrap(err, "failed to open location file")
}
defer file.Close()

return json.NewEncoder(file).Encode(loc)
}
17 changes: 1 addition & 16 deletions pkg/zos_api/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@ package zosapi

import (
"context"
"encoding/json"
"os"

"github.com/pkg/errors"
"github.com/threefoldtech/zos/pkg/geoip"
)

func (g *ZosAPI) locationGet(ctx context.Context, payload []byte) (interface{}, error) {
if _, err := os.Stat(geoip.LocationFile); err != nil {
return nil, errors.Wrap(err, "couldn't found a location info")
}

f, err := os.Open(geoip.LocationFile)
if err != nil {
return nil, errors.Wrap(err, "couldn't get the location info")
}
defer f.Close()

var loc geoip.Location
err = json.NewDecoder(f).Decode(&loc)
return loc, err
return geoip.Fetch()
}

0 comments on commit 96ddfc0

Please sign in to comment.