[pkgsite] internal/api: implement module metadata endpoint

1 view
Skip to first unread message

Ethan Lee (Gerrit)

unread,
Mar 12, 2026, 3:04:07 PM (6 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 module metadata endpoint
Change-Id: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0

Change diff

diff --git a/internal/api/api.go b/internal/api/api.go
index 5ea98ee..57a3eae 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -151,6 +151,60 @@
return serveJSON(w, http.StatusOK, resp)
}

+// ServeModule handles requests for the v1 module metadata endpoint.
+func ServeModule(w http.ResponseWriter, r *http.Request, ds internal.DataSource) (err error) {
+ defer derrors.Wrap(&err, "ServeModule")
+
+ modulePath := strings.TrimPrefix(r.URL.Path, "/v1/module/")
+ if modulePath == "" {
+ return serveErrorJSON(w, http.StatusBadRequest, "missing module path", nil)
+ }
+
+ var params ModuleParams
+ if err := ParseParams(r.URL.Query(), &params); err != nil {
+ return serveErrorJSON(w, http.StatusBadRequest, err.Error(), nil)
+ }
+
+ requestedVersion := params.Version
+ if requestedVersion == "" {
+ requestedVersion = version.Latest
+ }
+
+ // For modules, we can use GetUnitMeta on the module path.
+ um, err := ds.GetUnitMeta(r.Context(), modulePath, modulePath, requestedVersion)
+ if err != nil {
+ if errors.Is(err, derrors.NotFound) {
+ return serveErrorJSON(w, http.StatusNotFound, err.Error(), nil)
+ }
+ return err
+ }
+
+ resp := Module{
+ Path: um.ModulePath,
+ Version: um.Version,
+ IsStandardLibrary: stdlib.Contains(um.ModulePath),
+ IsRedistributable: um.IsRedistributable,
+ // RepoURL needs to be extracted from source info if available
+ }
+ if um.SourceInfo != nil {
+ resp.RepoURL = um.SourceInfo.RepoURL()
+ }
+
+ if params.Readme {
+ readme, err := ds.GetModuleReadme(r.Context(), um.ModulePath, um.Version)
+ if err == nil && readme != nil {
+ resp.Readme = &Readme{
+ Filepath: readme.Filepath,
+ Contents: readme.Contents,
+ }
+ }
+ }
+
+ // Future: handle licenses param.
+
+ 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 75ba24d..572b259 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -16,6 +16,87 @@
"golang.org/x/pkgsite/internal/testing/fakedatasource"
)

+func TestServeModule(t *testing.T) {
+ ctx := context.Background()
+ ds := fakedatasource.New()
+
+ const (
+ modulePath = "example.com"
+ version = "v1.2.3"
+ )
+
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{
+ ModulePath: modulePath,
+ Version: version,
+ },
+ Units: []*internal.Unit{{
+ UnitMeta: internal.UnitMeta{
+ Path: modulePath,
+ ModuleInfo: internal.ModuleInfo{
+ ModulePath: modulePath,
+ Version: version,
+ },
+ },
+ Readme: &internal.Readme{Filepath: "README.md", Contents: "Hello world"},
+ }},
+ })
+
+ for _, test := range []struct {
+ name string
+ url string
+ wantStatus int
+ want *Module
+ }{
+ {
+ name: "basic module metadata",
+ url: "/v1/module/example.com?version=v1.2.3",
+ wantStatus: http.StatusOK,
+ want: &Module{
+ Path: modulePath,
+ Version: version,
+ },
+ },
+ {
+ name: "module with readme",
+ url: "/v1/module/example.com?version=v1.2.3&readme=true",
+ wantStatus: http.StatusOK,
+ want: &Module{
+ Path: modulePath,
+ Version: version,
+ Readme: &Readme{
+ Filepath: "README.md",
+ Contents: "Hello world",
+ },
+ },
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ r := httptest.NewRequest("GET", test.url, nil)
+ w := httptest.NewRecorder()
+
+ err := ServeModule(w, r, ds)
+ if err != nil {
+ t.Fatalf("ServeModule returned error: %v", err)
+ }
+
+ if w.Code != test.wantStatus {
+ t.Errorf("status = %d, want %d", w.Code, test.wantStatus)
+ }
+
+ if test.want != nil {
+ var got Module
+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
+ t.Fatalf("json.Unmarshal: %v", err)
+ }
+ if diff := cmp.Diff(test.want, &got); diff != "" {
+ t.Errorf("mismatch (-want +got):\n%s", diff)
+ }
+ }
+ })
+ }
+}
+
func TestServeSearch(t *testing.T) {
ctx := context.Background()
ds := fakedatasource.New()
diff --git a/internal/frontend/server.go b/internal/frontend/server.go
index ad68770..17644fd 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/module/", s.errorHandler(api.ServeModule))
handle("GET /v1/search", s.errorHandler(api.ServeSearch))
handle("/opensearch.xml", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

diff --git a/internal/testing/fakedatasource/fakedatasource.go b/internal/testing/fakedatasource/fakedatasource.go
index 1f69519..754a00c 100644
--- a/internal/testing/fakedatasource/fakedatasource.go
+++ b/internal/testing/fakedatasource/fakedatasource.go
@@ -234,9 +234,17 @@
return nil
}

-// GetModuleReadme is not implemented.
+// GetModuleReadme gets the readme for the module.
func (ds *FakeDataSource) GetModuleReadme(ctx context.Context, modulePath, resolvedVersion string) (*internal.Readme, error) {
- return nil, nil
+ m := ds.getModule(modulePath, resolvedVersion)
+ if m == nil {
+ return nil, derrors.NotFound
+ }
+ u := findUnit(m, modulePath)
+ if u == nil {
+ return nil, nil
+ }
+ return u.Readme, nil
}

// GetLatestInfo gets information about the latest versions of a unit and module.

Change information

Files:
  • M internal/api/api.go
  • M internal/api/api_test.go
  • M internal/frontend/server.go
  • M internal/testing/fakedatasource/fakedatasource.go
Change size: M
Delta: 4 files changed, 146 insertions(+), 2 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: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0
Gerrit-Change-Number: 754861
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:26 PM (6 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/ab0c9083-abbc-4d29-8d3f-d501908e6db5

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: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0
Gerrit-Change-Number: 754861
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:17:23 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Jonathan Amsterdam (Gerrit)

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

Jonathan Amsterdam voted and added 1 comment

Votes added by Jonathan Amsterdam

Code-Review+2

1 comment

File internal/api/api.go
Line 187, Patchset 1 (Latest): // RepoURL needs to be extracted from source info if available
Jonathan Amsterdam . unresolved

Move this this down one line.

Open in Gerrit

Related details

Attention is currently required from:
  • Ethan Lee
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement 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: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0
Gerrit-Change-Number: 754861
Gerrit-PatchSet: 1
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: Ethan Lee <etha...@google.com>
Gerrit-Comment-Date: Wed, 18 Mar 2026 15:52:17 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Ethan Lee (Gerrit)

unread,
2:37 PM (4 hours ago) 2:37 PM
to goph...@pubsubhelper.golang.org, Go LUCI, Jonathan Amsterdam, kokoro, golang-co...@googlegroups.com
Attention needed from Jonathan Amsterdam

New activity on the change

Open in Gerrit

Related details

Attention is currently required from:
  • Jonathan Amsterdam
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement is not 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: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0
Gerrit-Change-Number: 754861
Gerrit-PatchSet: 5
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-Comment-Date: Wed, 18 Mar 2026 18:37:49 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
open
diffy

Ethan Lee (Gerrit)

unread,
2:38 PM (4 hours ago) 2:38 PM
to goph...@pubsubhelper.golang.org, Go LUCI, Jonathan Amsterdam, kokoro, golang-co...@googlegroups.com
Attention needed from Jonathan Amsterdam

Ethan Lee added 1 comment

File internal/api/api.go
Line 187, Patchset 1: // RepoURL needs to be extracted from source info if available
Jonathan Amsterdam . resolved

Move this this down one line.

Ethan Lee

Done

Open in Gerrit

Related details

Attention is currently required from:
  • 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: comment
    Gerrit-Project: pkgsite
    Gerrit-Branch: master
    Gerrit-Change-Id: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0
    Gerrit-Change-Number: 754861
    Gerrit-PatchSet: 5
    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-Comment-Date: Wed, 18 Mar 2026 18:38:00 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Jonathan Amsterdam <j...@google.com>
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Jonathan Amsterdam (Gerrit)

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

    Jonathan Amsterdam voted Code-Review+2

    Code-Review+2
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Ethan Lee
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement 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: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0
    Gerrit-Change-Number: 754861
    Gerrit-PatchSet: 5
    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: Ethan Lee <etha...@google.com>
    Gerrit-Comment-Date: Wed, 18 Mar 2026 19:07:01 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy
    Reply all
    Reply to author
    Forward
    0 new messages