Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add location endpoint #2481

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at this point you can add caching if needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, the node location should be updated like once per day and then cached for full day before it's fetched again.

}
Loading