1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
alice-lg-alice-lg/pkg/backend/api_endpoints_neighbors.go

61 lines
1.3 KiB
Go
Raw Normal View History

2021-03-22 16:25:47 +01:00
package backend
2018-09-22 15:51:20 +02:00
import (
"net/http"
"sort"
2021-03-22 16:50:08 +01:00
"github.com/julienschmidt/httprouter"
"github.com/alice-lg/alice-lg/pkg/api"
2018-09-22 15:51:20 +02:00
)
// Handle get neighbors on routeserver
2019-09-25 01:53:21 +02:00
func apiNeighborsList(
_req *http.Request,
params httprouter.Params,
) (api.Response, error) {
2021-03-22 16:50:08 +01:00
rsId, err := validateSourceID(params.ByName("id"))
2018-09-22 15:51:20 +02:00
if err != nil {
return nil, err
}
var neighborsResponse *api.NeighboursResponse
// Try to fetch neighbors from store, only fall back
// to RS query if store is not ready yet
sourceStatus := AliceNeighboursStore.SourceStatus(rsId)
if sourceStatus.State == STATE_READY {
neighbors := AliceNeighboursStore.GetNeighborsAt(rsId)
// Make response
neighborsResponse = &api.NeighboursResponse{
Api: api.ApiStatus{
2021-04-15 19:40:53 +02:00
Version: Version,
CacheStatus: api.CacheStatus{
OrigTtl: 0,
CachedAt: sourceStatus.LastRefresh,
},
ResultFromCache: true, // you bet!
Ttl: sourceStatus.LastRefresh.Add(
AliceNeighboursStore.refreshInterval),
},
Neighbours: neighbors,
}
} else {
source := AliceConfig.SourceInstanceById(rsId)
if source == nil {
return nil, SOURCE_NOT_FOUND_ERROR
}
neighborsResponse, err = source.Neighbours()
if err != nil {
apiLogSourceError("neighbors", rsId, err)
2018-10-22 16:32:32 +02:00
return nil, err
}
2018-10-02 14:52:47 +02:00
}
// Sort result
sort.Sort(&neighborsResponse.Neighbours)
2018-10-22 16:32:32 +02:00
return neighborsResponse, nil
2018-09-22 15:51:20 +02:00
}