The AS Name Search card on the home page said the search was coming soon.
It now asks the new GET /api/v1/search/as?q=<text> and lists the matches
as links to their AS pages. asinfo.Search ignores case, returns the first
matches up to a limit in AS number order, and counts all matches. An
empty q answers 400.
When the database has no record of an AS, its page and /api/v1/as/{asn}
take its handle and description from asinfo and still list its prefixes.
They answer 404 only for an AS with no record, no asinfo entry and no
prefix. AS 0 means no known origin: it always answers 404, and the search
never lists it.
Model: opus-5-5
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"git.eeqj.de/sneak/routewatch/internal/database"
|
||||
"git.eeqj.de/sneak/routewatch/internal/metrics"
|
||||
"git.eeqj.de/sneak/routewatch/internal/ristypes"
|
||||
"git.eeqj.de/sneak/routewatch/internal/streamer"
|
||||
"git.eeqj.de/sneak/routewatch/internal/templates"
|
||||
asinfo "git.eeqj.de/sneak/routewatch/pkg/asinfo"
|
||||
@@ -56,10 +57,18 @@ const (
|
||||
// Error messages an answer sends, in place of the error behind it where there
|
||||
// is one.
|
||||
const (
|
||||
invalidPrefixMessage = "Invalid prefix"
|
||||
hostNotResolvedMessage = "could not resolve host name"
|
||||
hostNotPublicMessage = "host name has no public address"
|
||||
tooManyTargetsMessage = "Too many addresses or host names"
|
||||
invalidPrefixMessage = "Invalid prefix"
|
||||
hostNotResolvedMessage = "could not resolve host name"
|
||||
hostNotPublicMessage = "host name has no public address"
|
||||
tooManyTargetsMessage = "Too many addresses or host names"
|
||||
searchQueryMissingMessage = "Missing search query"
|
||||
)
|
||||
|
||||
// The AS name search reads the text it looks for from the URL query parameter
|
||||
// asSearchQueryParam, and answers with at most maxASSearchResults matches.
|
||||
const (
|
||||
asSearchQueryParam = "q"
|
||||
maxASSearchResults = 50
|
||||
)
|
||||
|
||||
// IP versions as database.LiveRoute.IPVersion records them.
|
||||
@@ -1001,11 +1010,12 @@ func (s *Server) handleASDetailJSON() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// splitRoutesByIPVersion groups routes into the IPv4 and the IPv6 ones.
|
||||
// splitRoutesByIPVersion groups routes into the IPv4 and the IPv6 ones. Neither
|
||||
// group is nil, so that a JSON answer holds [] for an empty one and not null.
|
||||
func splitRoutesByIPVersion(
|
||||
routes []database.LiveRoute,
|
||||
) ([]database.LiveRoute, []database.LiveRoute) {
|
||||
var ipv4Routes, ipv6Routes []database.LiveRoute
|
||||
ipv4Routes, ipv6Routes := []database.LiveRoute{}, []database.LiveRoute{}
|
||||
|
||||
for _, route := range routes {
|
||||
if route.IPVersion == ipVersionV4 {
|
||||
@@ -1105,6 +1115,56 @@ func (s *Server) handlePrefixDetailJSON() http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// ASSearchResponse is the answer to an AS name search: the text searched for,
|
||||
// the number of ASes that match it, and the first maxASSearchResults of them in
|
||||
// AS number order.
|
||||
type ASSearchResponse struct {
|
||||
Query string `json:"query"`
|
||||
Total int `json:"total"`
|
||||
Results []asinfo.Info `json:"results"`
|
||||
}
|
||||
|
||||
// handleASSearch returns a handler that searches the AS list built into
|
||||
// asinfo for the text in the q parameter, without its leading and trailing
|
||||
// spaces. It answers 400 when no text is left.
|
||||
func (s *Server) handleASSearch() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
query := strings.TrimSpace(r.URL.Query().Get(asSearchQueryParam))
|
||||
if query == "" {
|
||||
s.writeJSONError(w, http.StatusBadRequest, searchQueryMissingMessage)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AS 0 means no known origin and has no AS page, so the answer leaves
|
||||
// it out. The matches come in AS number order, so AS 0 can only be the
|
||||
// first. The search asks for one match more than the answer holds, to
|
||||
// take its place.
|
||||
results, total := asinfo.Search(query, maxASSearchResults+1)
|
||||
if len(results) > 0 && results[0].ASN == ristypes.NoOrigin {
|
||||
results = results[1:]
|
||||
total--
|
||||
}
|
||||
|
||||
results = results[:min(len(results), maxASSearchResults)]
|
||||
|
||||
// results is an empty list rather than nil when nothing matches, so
|
||||
// that the answer holds [] and not null.
|
||||
if results == nil {
|
||||
results = []asinfo.Info{}
|
||||
}
|
||||
|
||||
err := writeJSONSuccess(w, ASSearchResponse{
|
||||
Query: query,
|
||||
Total: total,
|
||||
Results: results,
|
||||
})
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to encode AS search results", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleASDetail returns a handler that serves the AS detail HTML page
|
||||
func (s *Server) handleASDetail() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user