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

60 lines
1.0 KiB
Go
Raw Normal View History

2021-03-22 16:25:47 +01:00
package backend
2018-07-13 16:39:53 +02:00
import (
"net/http"
"strconv"
2018-07-15 16:55:26 +02:00
"strings"
2021-03-22 16:50:08 +01:00
"github.com/alice-lg/alice-lg/pkg/api"
2018-07-13 16:39:53 +02:00
)
/*
Convenience methods for accessing the query string
in the request object.
*/
/*
Get int value by name from query string
*/
func apiQueryMustInt(req *http.Request, param string, defaultValue int) int {
query := req.URL.Query()
strVal, ok := query[param]
if !ok {
return defaultValue
}
value, err := strconv.Atoi(strVal[0])
if err != nil {
return defaultValue
}
return value
}
2018-07-15 16:55:26 +02:00
/*
Filter response to match query criteria
*/
func apiQueryFilterNextHopGateway(
req *http.Request, param string, routes api.Routes,
) api.Routes {
query := req.URL.Query()
queryParam, ok := query[param]
2018-07-15 16:55:26 +02:00
if !ok {
return routes
}
// Normalize to lowercase
queryString := strings.ToLower(queryParam[0])
2018-07-15 16:55:26 +02:00
results := make(api.Routes, 0, len(routes))
for _, r := range routes {
if strings.HasPrefix(strings.ToLower(r.Network), queryString) ||
strings.HasPrefix(strings.ToLower(r.Gateway), queryString) {
2018-07-15 16:55:26 +02:00
results = append(results, r)
}
}
return results
}