1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00
birdwatcher/endpoints/routes.go

203 lines
6 KiB
Go
Raw Permalink Normal View History

2016-11-11 14:14:38 +01:00
package endpoints
import (
2016-12-06 13:02:12 +01:00
"fmt"
"net/http"
2016-12-06 13:02:12 +01:00
"github.com/alice-lg/birdwatcher/bird"
2016-11-11 14:14:38 +01:00
"github.com/julienschmidt/httprouter"
)
func ProtoRoutes(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2016-12-06 13:02:12 +01:00
protocol, err := ValidateProtocolParam(ps.ByName("protocol"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
2019-02-05 17:37:53 +01:00
return bird.RoutesProto(useCache, protocol)
2016-11-11 14:14:38 +01:00
}
func RoutesFiltered(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2016-12-06 17:20:27 +01:00
protocol, err := ValidateProtocolParam(ps.ByName("protocol"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
2019-02-05 17:37:53 +01:00
return bird.RoutesFiltered(useCache, protocol)
2016-12-06 17:20:27 +01:00
}
2020-01-26 23:12:06 +09:00
func RoutesExport(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
protocol, err := ValidateProtocolParam(ps.ByName("protocol"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesExport(useCache, protocol)
}
func RoutesNoExport(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2017-04-07 11:11:43 +02:00
protocol, err := ValidateProtocolParam(ps.ByName("protocol"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
2019-02-05 17:37:53 +01:00
return bird.RoutesNoExport(useCache, protocol)
2017-04-07 11:11:43 +02:00
}
func RoutesPrefixed(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
qs := r.URL.Query()
prefixl := qs["prefix"]
if len(prefixl) != 1 {
return bird.Parsed{"error": "need a prefix as single query parameter"}, false
}
prefix, err := ValidatePrefixParam(prefixl[0])
2016-12-08 11:09:25 +01:00
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
2019-02-05 17:37:53 +01:00
return bird.RoutesPrefixed(useCache, prefix)
2016-12-08 11:09:25 +01:00
}
func TableRoutes(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2019-02-05 17:37:53 +01:00
table, err := ValidateProtocolParam(ps.ByName("table"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesTable(useCache, table)
2016-11-11 14:14:38 +01:00
}
func TableRoutesFiltered(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2019-02-05 17:37:53 +01:00
table, err := ValidateProtocolParam(ps.ByName("table"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesTableFiltered(useCache, table)
}
func TableAndPeerRoutes(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2019-02-05 17:37:53 +01:00
table, err := ValidateProtocolParam(ps.ByName("table"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
peer, err := ValidatePrefixParam(ps.ByName("peer"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesTableAndPeer(useCache, table, peer)
}
func ProtoCount(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
protocol, err := ValidateProtocolParam(ps.ByName("protocol"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
2019-02-05 17:37:53 +01:00
return bird.RoutesProtoCount(useCache, protocol)
2016-11-11 14:14:38 +01:00
}
func ProtoPrimaryCount(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
protocol, err := ValidateProtocolParam(ps.ByName("protocol"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesProtoPrimaryCount(useCache, protocol)
}
func TableCount(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2019-02-05 17:37:53 +01:00
table, err := ValidateProtocolParam(ps.ByName("table"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesTableCount(useCache, table)
2016-11-11 14:14:38 +01:00
}
func RouteNet(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2019-02-05 17:37:53 +01:00
net, err := ValidatePrefixParam(ps.ByName("net"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesLookupTable(useCache, net, "master")
2016-11-11 14:14:38 +01:00
}
func RouteNetTable(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2019-02-05 17:37:53 +01:00
net, err := ValidatePrefixParam(ps.ByName("net"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
table, err := ValidateProtocolParam(ps.ByName("table"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesLookupTable(useCache, net, table)
2016-11-11 14:14:38 +01:00
}
2017-02-15 12:20:55 +01:00
func PipeRoutesFiltered(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2017-02-15 12:20:55 +01:00
qs := r.URL.Query()
2019-02-05 17:37:53 +01:00
if len(qs["table"]) != 1 {
return bird.Parsed{"error": "need a table as single query parameter"}, false
}
table, err := ValidateProtocolParam(qs["table"][0])
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
if len(qs["pipe"]) != 1 {
return bird.Parsed{"error": "need a pipe as single query parameter"}, false
}
pipe, err := ValidateProtocolParam(qs["pipe"][0])
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.PipeRoutesFiltered(useCache, pipe, table)
}
2017-02-15 12:20:55 +01:00
func PipeRoutesFilteredCount(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
qs := r.URL.Query()
2019-02-05 17:37:53 +01:00
if len(qs["table"]) != 1 {
return bird.Parsed{"error": "need a table as single query parameter"}, false
}
table, err := ValidateProtocolParam(qs["table"][0])
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
if len(qs["pipe"]) != 1 {
return bird.Parsed{"error": "need a pipe as single query parameter"}, false
}
pipe, err := ValidateProtocolParam(qs["pipe"][0])
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
if len(qs["address"]) != 1 {
return bird.Parsed{"error": "need a address as single query parameter"}, false
}
address, err := ValidatePrefixParam(qs["address"][0])
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.PipeRoutesFilteredCount(useCache, pipe, table, address)
2017-02-15 12:20:55 +01:00
}
2017-06-22 15:06:54 +02:00
func PeerRoutes(r *http.Request, ps httprouter.Params, useCache bool) (bird.Parsed, bool) {
2019-02-05 17:37:53 +01:00
peer, err := ValidatePrefixParam(ps.ByName("peer"))
if err != nil {
return bird.Parsed{"error": fmt.Sprintf("%s", err)}, false
}
return bird.RoutesPeer(useCache, peer)
2017-06-22 15:06:54 +02:00
}