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: 4 additions & 0 deletions pkg/geoip/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ 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: 16 additions & 0 deletions pkg/registrar/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ 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 @@ -144,6 +146,10 @@ 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 @@ -231,3 +237,13 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

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

is it possible for the file to already exists?
If possible, what is the default behavior? Is truncate or append?

if err != nil {
return errors.Wrap(err, "failed to open location file")
}
defer file.Close()

return json.NewEncoder(file).Encode(loc)
}
26 changes: 26 additions & 0 deletions pkg/zos_api/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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 {
Copy link
Member

Choose a reason for hiding this comment

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

is it needed?

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
}
3 changes: 3 additions & 0 deletions pkg/zos_api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ func (g *ZosAPI) SetupRoutes(router *peer.Router) {
admin.WithHandler("interfaces", g.adminInterfacesHandler)
admin.WithHandler("set_public_nic", g.adminSetPublicNICHandler)
admin.WithHandler("get_public_nic", g.adminGetPublicNICHandler)

location := root.SubRoute("location")
location.WithHandler("get", g.locationGet)
}
Loading