added serverside filtering of routes
This commit is contained in:
parent
f66ad18a1c
commit
bb798efbc5
@ -215,13 +215,16 @@ func apiRoutesListReceived(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Filter routes based on criteria if present
|
||||
routes := apiQueryFilterNextHopGateway(req, "q", result.Imported)
|
||||
|
||||
// Paginate results
|
||||
// TODO: get pageSize from config
|
||||
page := apiQueryMustInt(req, "page", 0)
|
||||
pageSize := 200
|
||||
routes, pagination := apiPaginateRoutes(routes, page, pageSize)
|
||||
|
||||
// Make paginated response
|
||||
routes, pagination := apiPaginateRoutes(result.Imported, page, pageSize)
|
||||
response := api.PaginatedRoutesResponse{
|
||||
RoutesResponse: &api.RoutesResponse{
|
||||
Api: result.Api,
|
||||
@ -249,13 +252,16 @@ func apiRoutesListFiltered(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Filter routes based on criteria if present
|
||||
routes := apiQueryFilterNextHopGateway(req, "q", result.Filtered)
|
||||
|
||||
// Paginate results
|
||||
// TODO: get pageSize from config
|
||||
page := apiQueryMustInt(req, "page", 0)
|
||||
pageSize := 200
|
||||
routes, pagination := apiPaginateRoutes(routes, page, pageSize)
|
||||
|
||||
// Make response
|
||||
routes, pagination := apiPaginateRoutes(result.Filtered, page, pageSize)
|
||||
response := api.PaginatedRoutesResponse{
|
||||
RoutesResponse: &api.RoutesResponse{
|
||||
Api: result.Api,
|
||||
@ -283,13 +289,15 @@ func apiRoutesListNotExported(
|
||||
return nil, err
|
||||
}
|
||||
|
||||
routes := apiQueryFilterNextHopGateway(req, "q", result.NotExported)
|
||||
|
||||
// Paginate results
|
||||
// TODO: get pageSize from config
|
||||
page := apiQueryMustInt(req, "page", 0)
|
||||
pageSize := 200
|
||||
routes, pagination := apiPaginateRoutes(routes, page, pageSize)
|
||||
|
||||
// Make response
|
||||
routes, pagination := apiPaginateRoutes(result.NotExported, page, pageSize)
|
||||
response := api.PaginatedRoutesResponse{
|
||||
RoutesResponse: &api.RoutesResponse{
|
||||
Api: result.Api,
|
||||
|
@ -3,6 +3,9 @@ package main
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/alice-lg/alice-lg/backend/api"
|
||||
)
|
||||
|
||||
/*
|
||||
@ -27,3 +30,27 @@ func apiQueryMustInt(req *http.Request, param string, defaultValue int) int {
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
/*
|
||||
Filter response to match query criteria
|
||||
*/
|
||||
|
||||
func apiQueryFilterNextHopGateway(
|
||||
req *http.Request, param string, routes api.Routes,
|
||||
) api.Routes {
|
||||
query := req.URL.Query()
|
||||
q, ok := query[param]
|
||||
if !ok {
|
||||
return routes
|
||||
}
|
||||
|
||||
results := make(api.Routes, 0, len(routes))
|
||||
for _, r := range routes {
|
||||
if strings.HasPrefix(r.Network, q[0]) ||
|
||||
strings.HasPrefix(r.Gateway, q[0]) {
|
||||
results = append(results, r)
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user