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

68 lines
1.6 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"
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(
2021-12-06 10:03:17 +01:00
req *http.Request,
2019-09-25 01:53:21 +02:00
params httprouter.Params,
2021-10-27 17:54:51 +00:00
) (response, error) {
2021-12-06 10:03:17 +01:00
ctx := req.Context()
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.
// The stored neighbors response includes details like
// the number of filtered routes which might be lacking
// from the summary.
2021-12-06 10:03:17 +01:00
if s.neighborsStore.IsInitialized(rsID) {
status, err := s.neighborsStore.GetStatus(rsID)
neighbors, err := s.neighborsStore.GetNeighborsAt(ctx, rsID)
if err != nil {
return nil, err
}
// 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,
2021-12-06 10:03:17 +01:00
CachedAt: status.LastRefresh,
2021-10-27 17:54:51 +00:00
},
ResultFromCache: true, // you bet!
2021-12-07 19:11:11 +01:00
TTL: s.neighborsStore.SourceCacheTTL(ctx, rsID),
},
},
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
}
neighborsResponse, err = source.NeighborsSummary()
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
}