Skip to content

Commit 63bdbb9

Browse files
committed
Advanced search fixes
1 parent 7a13bd1 commit 63bdbb9

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

cmd/hs/search.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ func commandSearch() *cli.Command {
5757
hostFilter := c.String("host")
5858
extFilter := c.String("extension")
5959
serverURL := c.String("server-url")
60+
limit := c.Int("limit")
6061

6162
filename := c.Args().Get(0)
6263
if c.NArg() == 0 {
6364
return fmt.Errorf("filename argument is required")
6465
}
6566

6667
if serverURL != "" {
67-
return searchServer(serverURL, filename)
68+
return searchServer(serverURL, filename, extFilter, hostFilter, limit)
6869
}
6970

7071
if c.String("tag") != "" {
@@ -182,9 +183,9 @@ func printRows(rows *sql.Rows) error {
182183
return nil
183184
}
184185

185-
func searchServer(serverURL string, filename string) error {
186+
func searchServer(serverURL string, filename string, extFilter string, hostFilter string, limit int) error {
186187
client := api.NewClient(serverURL)
187-
r, err := client.Search(filename)
188+
r, err := client.Search(filename, []string{extFilter}, []string{hostFilter}, limit)
188189
if err != nil {
189190
return fmt.Errorf("failed to search server: %v", err)
190191
}

internal/api/api.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"net/url"
1212
"strconv"
13+
"strings"
1314
"time"
1415

1516
"github.com/go-chi/chi/v5"
@@ -60,9 +61,13 @@ func NewClient(serverURL string) *Client {
6061
return &Client{client: client, serverURL: serverURL}
6162
}
6263

63-
func (c *Client) Search(query string) ([]*types.FileResult, error) {
64+
func (c *Client) Search(query string, exts []string, hosts []string, limit int) ([]*types.FileResult, error) {
6465
// Build the URL with query parameters
65-
urlStr := fmt.Sprintf("%s/search?q=%s", c.serverURL, url.QueryEscape(query))
66+
params := url.Values{}
67+
params.Set("ext", strings.Join(exts, ","))
68+
params.Set("host", strings.Join(hosts, ","))
69+
params.Set("limit", strconv.Itoa(limit))
70+
urlStr := fmt.Sprintf("%s/search?q=%s&%s", c.serverURL, url.QueryEscape(query), params.Encode())
6671

6772
// Create request
6873
req, err := http.NewRequest("GET", urlStr, nil)
@@ -149,7 +154,18 @@ func searchHandler(dbPath string) http.HandlerFunc {
149154
hosts = append(hosts, host)
150155
}
151156

152-
results, err := hsdb.Search(db, query, exts, hosts, 100)
157+
limit := r.URL.Query().Get("limit")
158+
if limit == "" {
159+
limit = "100"
160+
}
161+
162+
ilimit, err := strconv.Atoi(limit)
163+
if err != nil {
164+
statusJSON(http.StatusBadRequest, errors.New("invalid limit parameter"), w, r)
165+
return
166+
}
167+
168+
results, err := hsdb.Search(db, query, exts, hosts, ilimit)
153169
if err != nil {
154170
statusJSON(http.StatusInternalServerError, err, w, r)
155171
return

0 commit comments

Comments
 (0)