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 all commits
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
27 changes: 27 additions & 0 deletions pkg/zos_api/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package zosapi

import (
"context"

"github.com/patrickmn/go-cache"
"github.com/threefoldtech/zos/pkg/geoip"
)

const (
locationCacheKey = "location"
)

func (g *ZosAPI) locationGet(ctx context.Context, payload []byte) (interface{}, error) {
if loc, found := g.inMemCache.Get(locationCacheKey); found {
return loc, nil
}

loc, err := geoip.Fetch()
if err != nil {
return nil, err
}

g.inMemCache.Set(locationCacheKey, loc, cache.DefaultExpiration)

return loc, nil
}
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)
}
9 changes: 9 additions & 0 deletions pkg/zos_api/zos_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package zosapi

import (
"fmt"
"time"

"github.com/patrickmn/go-cache"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg/capacity"
Expand All @@ -11,6 +13,11 @@ import (
"github.com/threefoldtech/zos/pkg/stubs"
)

const (
cacheDefaultExpiration = 24 * time.Hour
cacheDefaultCleanup = 24 * time.Hour
)

type ZosAPI struct {
oracle *capacity.ResourceOracle
versionMonitorStub *stubs.VersionMonitorStub
Expand All @@ -22,6 +29,7 @@ type ZosAPI struct {
performanceMonitorStub *stubs.PerformanceMonitorStub
diagnosticsManager *diagnostics.DiagnosticsManager
farmerID uint32
inMemCache *cache.Cache
}

func NewZosAPI(manager substrate.Manager, client zbus.Client, msgBrokerCon string) (ZosAPI, error) {
Expand Down Expand Up @@ -56,5 +64,6 @@ func NewZosAPI(manager substrate.Manager, client zbus.Client, msgBrokerCon strin
return ZosAPI{}, err
}
api.farmerID = uint32(farmer.ID)
api.inMemCache = cache.New(cacheDefaultExpiration, cacheDefaultCleanup)
return api, nil
}
Loading