Skip to content

Commit

Permalink
- write node location info on a file while registering
Browse files Browse the repository at this point in the history
- add location endpoint to get this file info
  • Loading branch information
Omarabdul3ziz committed Nov 10, 2024
1 parent d85a925 commit ba259a7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
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)
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 {
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)
}

0 comments on commit ba259a7

Please sign in to comment.