aboutsummaryrefslogtreecommitdiff
path: root/internal/api/api.go
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2026-04-07 14:44:16 +0000
committerEthan Lee <ethanalee@google.com>2026-04-07 09:23:15 -0700
commit3c8abd6a69a5031ca55e55d16e725476f1d69cba (patch)
treec089fea9b2c6168836cadb7ff46d95e82c060760 /internal/api/api.go
parentd1eedfe77fdd85e4ec9dcef7e4a4411116eddbb2 (diff)
downloadgo-x-pkgsite-3c8abd6a69a5031ca55e55d16e725476f1d69cba.tar.xz
internal/api: add support for readme and licenses in ServeModule
- Utilize conditional fieldset in GetUnit to efficiently retrieve readme and license fields. - Modify Module type to include HasGoMod. Change-Id: Id22ac3f2485392749742332701d2e913f047b3da Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/763401 kokoro-CI: kokoro <noreply+kokoro@google.com> Auto-Submit: Ethan Lee <ethanalee@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/api/api.go')
-rw-r--r--internal/api/api.go36
1 files changed, 30 insertions, 6 deletions
diff --git a/internal/api/api.go b/internal/api/api.go
index 49873c9b..5bf14eec 100644
--- a/internal/api/api.go
+++ b/internal/api/api.go
@@ -100,21 +100,45 @@ func ServeModule(w http.ResponseWriter, r *http.Request, ds internal.DataSource)
resp := Module{
Path: um.ModulePath,
Version: um.Version,
+ IsLatest: um.Version == um.LatestVersion,
IsStandardLibrary: stdlib.Contains(um.ModulePath),
IsRedistributable: um.IsRedistributable,
+ HasGoMod: um.HasGoMod,
}
// RepoURL needs to be extracted from source info if available
if um.SourceInfo != nil {
resp.RepoURL = um.SourceInfo.RepoURL()
}
+ if !params.Readme && !params.Licenses {
+ return serveJSON(w, http.StatusOK, resp)
+ }
+
+ fs := internal.MinimalFields
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,
- }
+ fs |= internal.WithMain // WithMain includes Readme in GetUnit
+ }
+ if params.Licenses {
+ fs |= internal.WithLicenses
+ }
+ unit, err := ds.GetUnit(r.Context(), um, fs, internal.BuildContext{})
+ if err != nil {
+ return serveJSON(w, http.StatusOK, resp)
+ }
+
+ if params.Readme && unit.Readme != nil {
+ resp.Readme = &Readme{
+ Filepath: unit.Readme.Filepath,
+ Contents: unit.Readme.Contents,
+ }
+ }
+ if params.Licenses {
+ for _, l := range unit.LicenseContents {
+ resp.Licenses = append(resp.Licenses, License{
+ Types: l.Metadata.Types,
+ FilePath: l.Metadata.FilePath,
+ Contents: string(l.Contents),
+ })
}
}