[pkgsite] internal/api: implement search endpoint

2 views
Skip to first unread message

Ethan Lee (Gerrit)

unread,
Mar 12, 2026, 3:04:07 PM (7 days ago) Mar 12
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Ethan Lee has uploaded the change for review

Commit message

internal/api: implement search endpoint
Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405

Change diff

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(), &params); 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")

Change information

Files:
  • M internal/api/api.go
  • M internal/api/api_test.go
  • M internal/frontend/server.go
Change size: M
Delta: 3 files changed, 122 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
  • requirement is not satisfiedkokoro-CI-Passes
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: pkgsite
Gerrit-Branch: master
Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
Gerrit-Change-Number: 754860
Gerrit-PatchSet: 1
Gerrit-Owner: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: Ethan Lee <etha...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

kokoro (Gerrit)

unread,
Mar 12, 2026, 3:17:00 PM (7 days ago) Mar 12
to Ethan Lee, goph...@pubsubhelper.golang.org, Go LUCI, golang-co...@googlegroups.com
Attention needed from Ethan Lee

kokoro voted kokoro-CI-1

Kokoro presubmit build finished with status: FAILURE
Logs at: https://source.cloud.google.com/results/invocations/4eb19798-7caa-4d37-be06-9dfd918ce2a4

kokoro-CI-1
Open in Gerrit

Related details

Attention is currently required from:
  • Ethan Lee
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
  • requirement is not satisfiedkokoro-CI-Passes
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: pkgsite
Gerrit-Branch: master
Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
Gerrit-Change-Number: 754860
Gerrit-PatchSet: 1
Gerrit-Owner: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: kokoro <noreply...@google.com>
Gerrit-CC: kokoro <noreply...@google.com>
Gerrit-Attention: Ethan Lee <etha...@google.com>
Gerrit-Comment-Date: Thu, 12 Mar 2026 19:16:56 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Ethan Lee (Gerrit)

unread,
2:27 PM (8 hours ago) 2:27 PM
to goph...@pubsubhelper.golang.org, Go LUCI, kokoro, golang-co...@googlegroups.com

New activity on the change

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
  • requirement is not satisfiedkokoro-CI-Passes
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: pkgsite
Gerrit-Branch: master
Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
Gerrit-Change-Number: 754860
Gerrit-PatchSet: 4
Gerrit-Owner: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: kokoro <noreply...@google.com>
Gerrit-CC: kokoro <noreply...@google.com>
Gerrit-Comment-Date: Thu, 19 Mar 2026 18:27:41 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Ethan Lee (Gerrit)

unread,
2:52 PM (7 hours ago) 2:52 PM
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Ethan Lee

Ethan Lee uploaded new patchset

Ethan Lee uploaded patch set #5 to this change.
Following approvals got outdated and were removed:
  • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit

Related details

Attention is currently required from:
  • Ethan Lee
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
  • requirement is not satisfiedkokoro-CI-Passes
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: pkgsite
Gerrit-Branch: master
Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
Gerrit-Change-Number: 754860
Gerrit-PatchSet: 5
Gerrit-Owner: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: kokoro <noreply...@google.com>
Gerrit-CC: kokoro <noreply...@google.com>
Gerrit-Attention: Ethan Lee <etha...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

kokoro (Gerrit)

unread,
2:52 PM (7 hours ago) 2:52 PM
to Ethan Lee, goph...@pubsubhelper.golang.org, Go LUCI, golang-co...@googlegroups.com
Attention needed from Ethan Lee

kokoro voted kokoro-CI+1

Kokoro presubmit build finished with status: SUCCESS
Logs at: https://source.cloud.google.com/results/invocations/8f6d6d9a-6294-43b3-add8-ee635808b905

kokoro-CI+1
Open in Gerrit

Related details

Attention is currently required from:
  • Ethan Lee
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
  • requirement is not satisfiedkokoro-CI-Passes
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: pkgsite
Gerrit-Branch: master
Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
Gerrit-Change-Number: 754860
Gerrit-PatchSet: 4
Gerrit-Owner: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: kokoro <noreply...@google.com>
Gerrit-CC: kokoro <noreply...@google.com>
Gerrit-Attention: Ethan Lee <etha...@google.com>
Gerrit-Comment-Date: Thu, 19 Mar 2026 18:52:49 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Ethan Lee (Gerrit)

unread,
2:53 PM (7 hours ago) 2:53 PM
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Ethan Lee and Jonathan Amsterdam

Ethan Lee uploaded new patchset

Ethan Lee uploaded patch set #6 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Ethan Lee
  • Jonathan Amsterdam
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
  • requirement is not satisfiedkokoro-CI-Passes
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: pkgsite
Gerrit-Branch: master
Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
Gerrit-Change-Number: 754860
Gerrit-PatchSet: 6
Gerrit-Owner: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: Ethan Lee <etha...@google.com>
Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
Gerrit-Reviewer: kokoro <noreply...@google.com>
Gerrit-CC: kokoro <noreply...@google.com>
Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
Gerrit-Attention: Ethan Lee <etha...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Ethan Lee (Gerrit)

unread,
2:57 PM (7 hours ago) 2:57 PM
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Ethan Lee and Jonathan Amsterdam

Ethan Lee uploaded new patchset

Ethan Lee uploaded patch set #7 to this change.
Following approvals got outdated and were removed:
  • TryBots-Pass: LUCI-TryBot-Result-1 by Go LUCI
Open in Gerrit

Related details

Attention is currently required from:
  • Ethan Lee
  • Jonathan Amsterdam
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
  • requirement is not satisfiedkokoro-CI-Passes
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: pkgsite
Gerrit-Branch: master
Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
Gerrit-Change-Number: 754860
Gerrit-PatchSet: 7
unsatisfied_requirement
satisfied_requirement
open
diffy

kokoro (Gerrit)

unread,
3:19 PM (7 hours ago) 3:19 PM
to Ethan Lee, goph...@pubsubhelper.golang.org, Go LUCI, Jonathan Amsterdam, golang-co...@googlegroups.com
Attention needed from Ethan Lee and Jonathan Amsterdam

kokoro voted kokoro-CI+1

Kokoro presubmit build finished with status: SUCCESS

Related details

Attention is currently required from:
  • Ethan Lee
  • Jonathan Amsterdam
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    • requirement is not satisfiedkokoro-CI-Passes
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: pkgsite
    Gerrit-Branch: master
    Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
    Gerrit-Change-Number: 754860
    Gerrit-PatchSet: 6
    Gerrit-Owner: Ethan Lee <etha...@google.com>
    Gerrit-Reviewer: Ethan Lee <etha...@google.com>
    Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
    Gerrit-Reviewer: kokoro <noreply...@google.com>
    Gerrit-CC: kokoro <noreply...@google.com>
    Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
    Gerrit-Attention: Ethan Lee <etha...@google.com>
    Gerrit-Comment-Date: Thu, 19 Mar 2026 19:19:36 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    kokoro (Gerrit)

    unread,
    3:24 PM (7 hours ago) 3:24 PM
    to Ethan Lee, goph...@pubsubhelper.golang.org, Go LUCI, Jonathan Amsterdam, golang-co...@googlegroups.com
    Attention needed from Ethan Lee and Jonathan Amsterdam

    kokoro voted kokoro-CI+1

    Kokoro presubmit build finished with status: SUCCESS

    Related details

    Attention is currently required from:
    • Ethan Lee
    • Jonathan Amsterdam
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      • requirement satisfiedkokoro-CI-Passes
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: pkgsite
      Gerrit-Branch: master
      Gerrit-Change-Id: I109d0861b5fa0cfbc20ce20308561ef6b5616405
      Gerrit-Change-Number: 754860
      Gerrit-PatchSet: 7
      Gerrit-Owner: Ethan Lee <etha...@google.com>
      Gerrit-Reviewer: Ethan Lee <etha...@google.com>
      Gerrit-Reviewer: Jonathan Amsterdam <j...@google.com>
      Gerrit-Reviewer: kokoro <noreply...@google.com>
      Gerrit-CC: kokoro <noreply...@google.com>
      Gerrit-Attention: Jonathan Amsterdam <j...@google.com>
      Gerrit-Attention: Ethan Lee <etha...@google.com>
      Gerrit-Comment-Date: Thu, 19 Mar 2026 19:24:21 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages