diff --git a/internal/api/api.go b/internal/api/api.go
index b394e0a..5ea98ee 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -96,6 +96,61 @@
return serveJSON(w, http.StatusOK, resp)
}
+// ServeSearch handles requests for the v1 search endpoint.
+func ServeSearch(w http.ResponseWriter, r *http.Request, ds internal.DataSource) (err error) {
+ defer derrors.Wrap(&err, "ServeSearch")
+
+ var params SearchParams
+ if err := ParseParams(r.URL.Query(), ¶ms); err != nil {
+ return serveErrorJSON(w, http.StatusBadRequest, err.Error(), nil)
+ }
+
+ if params.Query == "" {
+ return serveErrorJSON(w, http.StatusBadRequest, "missing query", nil)
+ }
+
+ limit := params.Limit
+ if limit <= 0 {
+ limit = 25
+ }
+ if limit > 100 {
+ limit = 100
+ }
+
+ // For now, we only support basic package search without offset/token.
+ // Future iterations can add pagination support.
+ dbresults, err := ds.Search(r.Context(), params.Query, internal.SearchOptions{
+ MaxResults: limit,
+ SearchSymbols: params.Symbol != "",
+ SymbolFilter: params.Symbol,
+ })
+ if err != nil {
+ return err
+ }
+
+ var results []SearchResult
+ for _, r := range dbresults {
+ results = append(results, SearchResult{
+ PackagePath: r.PackagePath,
+ ModulePath: r.ModulePath,
+ Version: r.Version,
+ Synopsis: r.Synopsis,
+ })
+ }
+
+ var total int
+ if len(dbresults) > 0 {
+ total = int(dbresults[0].NumResults)
+ }
+
+ resp := PaginatedResponse[SearchResult]{
+ Items: results,
+ Total: total,
+ }
+
+ return serveJSON(w, http.StatusOK, resp)
+}
+
func serveJSON(w http.ResponseWriter, status int, data any) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index 9022bef..75ba24d 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -16,6 +16,72 @@
"golang.org/x/pkgsite/internal/testing/fakedatasource"
)
+func TestServeSearch(t *testing.T) {
+ ctx := context.Background()
+ ds := fakedatasource.New()
+
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.0.0"},
+ Units: []*internal.Unit{{
+ UnitMeta: internal.UnitMeta{
+ Path: "example.com/pkg",
+ ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.0.0"},
+ Name: "pkg",
+ },
+ Documentation: []*internal.Documentation{{Synopsis: "A great package."}},
+ }},
+ })
+
+ for _, test := range []struct {
+ name string
+ url string
+ wantStatus int
+ wantCount int
+ }{
+ {
+ name: "basic search",
+ url: "/v1/search?q=great",
+ wantStatus: http.StatusOK,
+ wantCount: 1,
+ },
+ {
+ name: "no results",
+ url: "/v1/search?q=nonexistent",
+ wantStatus: http.StatusOK,
+ wantCount: 0,
+ },
+ {
+ name: "missing query",
+ url: "/v1/search",
+ wantStatus: http.StatusBadRequest,
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ r := httptest.NewRequest("GET", test.url, nil)
+ w := httptest.NewRecorder()
+
+ err := ServeSearch(w, r, ds)
+ if err != nil {
+ t.Fatalf("ServeSearch returned error: %v", err)
+ }
+
+ if w.Code != test.wantStatus {
+ t.Errorf("status = %d, want %d", w.Code, test.wantStatus)
+ }
+
+ if test.wantStatus == http.StatusOK {
+ var got PaginatedResponse[SearchResult]
+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
+ t.Fatalf("json.Unmarshal: %v", err)
+ }
+ if len(got.Items) != test.wantCount {
+ t.Errorf("count = %d, want %d", len(got.Items), test.wantCount)
+ }
+ }
+ })
+ }
+}
+
func TestServePackage(t *testing.T) {
ctx := context.Background()
ds := fakedatasource.New()
diff --git a/internal/frontend/server.go b/internal/frontend/server.go
index f972c8c..ad68770 100644
--- a/internal/frontend/server.go
+++ b/internal/frontend/server.go
@@ -237,6 +237,7 @@
handle("GET /files/", http.StripPrefix("/files", s.fileMux))
handle("GET /vuln/", vulnHandler)
handle("GET /v1/package/", s.errorHandler(api.ServePackage))
+ handle("GET /v1/search", s.errorHandler(api.ServeSearch))
handle("/opensearch.xml", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
serveFileFS(w, r, s.staticFS, "shared/opensearch.xml")