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/http/api_endpoints_neighbors.go

63 lines
1.5 KiB
Go
Raw Normal View History

2021-10-22 22:40:03 +02:00
package http
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"
2021-10-27 14:48:52 +00:00
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/store"
2018-09-22 15:51:20 +02:00
)
// Handle get neighbors on routeserver
2021-10-26 23:11:42 +02:00
func (s *Server) apiNeighborsList(
2019-09-25 01:53:21 +02:00
_req *http.Request,
params httprouter.Params,
2021-10-27 17:54:51 +00:00
) (response, error) {
2021-07-02 14:30:43 +02:00
rsID, err := validateSourceID(params.ByName("id"))
2018-09-22 15:51:20 +02:00
if err != nil {
return nil, err
}
2021-10-15 21:24:24 +02:00
var neighborsResponse *api.NeighborsResponse
// Try to fetch neighbors from store, only fall back
// to RS query if store is not ready yet
2021-10-26 23:11:42 +02:00
sourceStatus := s.neighborsStore.SourceStatus(rsID)
2021-10-27 14:48:52 +00:00
if sourceStatus.State == store.StateReady {
2021-10-26 23:11:42 +02:00
neighbors := s.neighborsStore.GetNeighborsAt(rsID)
// Make response
2021-10-15 21:24:24 +02:00
neighborsResponse = &api.NeighborsResponse{
2021-10-27 17:54:51 +00:00
Response: api.Response{
Meta: &api.Meta{
Version: config.Version,
CacheStatus: api.CacheStatus{
OrigTTL: 0,
CachedAt: sourceStatus.LastRefresh,
},
ResultFromCache: true, // you bet!
TTL: sourceStatus.LastRefresh.Add(
s.neighborsStore.RefreshInterval),
},
},
2021-10-15 21:24:24 +02:00
Neighbors: neighbors,
}
} else {
2021-10-27 17:54:51 +00:00
source := s.cfg.SourceInstanceByID(rsID)
if source == nil {
2021-10-27 14:48:52 +00:00
return nil, ErrSourceNotFound
}
2021-10-15 21:24:24 +02:00
neighborsResponse, err = source.Neighbors()
if err != nil {
2021-10-27 17:54:51 +00:00
s.logSourceError("neighbors", rsID, err)
2018-10-22 16:32:32 +02:00
return nil, err
}
2018-10-02 14:52:47 +02:00
}
// Sort result
2021-10-15 21:24:24 +02:00
sort.Sort(&neighborsResponse.Neighbors)
2018-10-22 16:32:32 +02:00
return neighborsResponse, nil
2018-09-22 15:51:20 +02:00
}