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/backend/api_query.go
2018-07-15 16:55:26 +02:00

57 lines
942 B
Go

package main
import (
"net/http"
"strconv"
"strings"
"github.com/alice-lg/alice-lg/backend/api"
)
/*
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
}
/*
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
}