Make the home page's AS name search work (closes #151)
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
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>, which searches the AS list built into asinfo, and lists the matches as links to their AS pages. The answer holds the query, the number of matches and the first 50 of them; an empty q answers 400. asinfo.Search now ignores case, uses strings.Contains instead of its own substring loop, and returns its matches in AS number order. A number, or AS and a number, typed into the card still opens that AS's page. The card builds the list with textContent, since the names come from outside data. Model: opus-5-5
This commit is contained in:
+17
-18
@@ -2,10 +2,13 @@ package asinfo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"compress/gzip"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -129,36 +132,32 @@ func All() []Info {
|
||||
return result
|
||||
}
|
||||
|
||||
// Search searches for AS entries where the handle or description contains the query
|
||||
// This is a simple case-sensitive substring search
|
||||
// Search returns the AS entries whose handle or description contains query,
|
||||
// ignoring case, in AS number order. An empty query matches nothing.
|
||||
func Search(query string) []Info {
|
||||
once.Do(initRegistry)
|
||||
|
||||
if query == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
query = strings.ToLower(query)
|
||||
|
||||
defaultRegistry.mu.RLock()
|
||||
defer defaultRegistry.mu.RUnlock()
|
||||
|
||||
var results []Info
|
||||
|
||||
for _, info := range defaultRegistry.byASN {
|
||||
if contains(info.Handle, query) || contains(info.Description, query) {
|
||||
if strings.Contains(strings.ToLower(info.Handle), query) ||
|
||||
strings.Contains(strings.ToLower(info.Description), query) {
|
||||
results = append(results, *info)
|
||||
}
|
||||
}
|
||||
|
||||
slices.SortFunc(results, func(a, b Info) int {
|
||||
return cmp.Compare(a.ASN, b.ASN)
|
||||
})
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
// contains is a simple substring search
|
||||
func contains(s, substr string) bool {
|
||||
return len(substr) > 0 && len(s) >= len(substr) && containsImpl(s, substr)
|
||||
}
|
||||
|
||||
func containsImpl(s, substr string) bool {
|
||||
for i := 0; i <= len(s)-len(substr); i++ {
|
||||
if s[i:i+len(substr)] == substr {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user