diff --git a/internal/build/build.go b/internal/build/build.go index d87bd1a..cdb42ba 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -2,11 +2,13 @@ package build import ( + "cmp" "context" "errors" "fmt" "html/template" "log/slog" + "maps" "path/filepath" "slices" "strings" @@ -124,33 +126,15 @@ func (b *Builder) Render(ctx context.Context, dir string) error { return nil } -var linkOrder = []string{"website", "atproto", "tangled", "github"} -var linkNames = map[string]string{"website": "website", "atproto": "bluesky", "tangled": "tangled", "github": "github"} - +// links orders the footer links: known keys first, then the rest by key. func links(m map[string]string) []render.Link { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - rank := func(k string) int { - if i := slices.Index(linkOrder, k); i >= 0 { - return i - } - return len(linkOrder) - } - slices.SortFunc(keys, func(a, c string) int { - if d := rank(a) - rank(c); d != 0 { - return d - } - return strings.Compare(a, c) - }) - out := make([]render.Link, 0, len(keys)) - for _, k := range keys { - name := linkNames[k] - if name == "" { - name = k + seen := map[string]bool{} + var out []render.Link + for _, k := range append([]string{"website", "atproto", "tangled", "github"}, slices.Sorted(maps.Keys(m))...) { + if u, ok := m[k]; ok && !seen[k] { + seen[k] = true + out = append(out, render.Link{Name: cmp.Or(map[string]string{"atproto": "bluesky"}[k], k), URL: u}) } - out = append(out, render.Link{Name: name, URL: m[k]}) } return out } diff --git a/internal/build/extra.go b/internal/build/extra.go index efbb017..bec1821 100644 --- a/internal/build/extra.go +++ b/internal/build/extra.go @@ -59,7 +59,6 @@ func (b *Builder) renderExtras(rd *render.Renderer, dir string, r *repo) error { } pkgs := ExtraPage{Repo: r, Tab: "packages"} - host := strings.TrimPrefix(strings.TrimPrefix(b.Cfg.SiteURL, "https://"), "http://") for _, x := range r.Packages { if strings.HasPrefix(x.Version, "sha256:") { continue // untagged image manifests @@ -69,7 +68,7 @@ func (b *Builder) renderExtras(rd *render.Renderer, dir string, r *repo) error { if !b.Cfg.HasImage(x.Name) { continue // not pullable through the proxy, so don't advertise it } - v.Command = "docker pull " + host + "/" + b.Cfg.User + "/" + x.Name + ":" + x.Version + v.Command = "docker pull " + siteHost(b.Cfg.SiteURL) + "/" + b.Cfg.User + "/" + x.Name + ":" + x.Version } pkgs.Packages = append(pkgs.Packages, v) } diff --git a/internal/build/repo.go b/internal/build/repo.go index 58a2ecc..c623769 100644 --- a/internal/build/repo.go +++ b/internal/build/repo.go @@ -261,6 +261,9 @@ func isImage(name string) bool { } func goImport(siteURL, name string) string { - host := strings.TrimPrefix(strings.TrimPrefix(siteURL, "https://"), "http://") - return host + "/" + name + " git " + siteURL + "/" + name + ".git" + return siteHost(siteURL) + "/" + name + " git " + siteURL + "/" + name + ".git" +} + +func siteHost(siteURL string) string { + return strings.TrimPrefix(strings.TrimPrefix(siteURL, "https://"), "http://") } diff --git a/internal/gitea/gitea.go b/internal/gitea/gitea.go index f2e0431..34de47f 100644 --- a/internal/gitea/gitea.go +++ b/internal/gitea/gitea.go @@ -43,7 +43,6 @@ type User struct { FullName string `json:"full_name"` AvatarURL string `json:"avatar_url"` Description string `json:"description"` - Website string `json:"website"` Location string `json:"location"` } @@ -61,9 +60,7 @@ type Repo struct { } type Asset struct { - Name string `json:"name"` - Size int64 `json:"size"` - DownloadURL string `json:"browser_download_url"` + Name string `json:"name"` // the download URL is rebuilt, never Gitea's } type Release struct { @@ -89,31 +86,38 @@ type PullRequest struct { } type Package struct { - Name string `json:"name"` - Type string `json:"type"` - Version string `json:"version"` - CreatedAt time.Time `json:"created_at"` - Repository *Repo `json:"repository"` + Name string `json:"name"` + Type string `json:"type"` + Version string `json:"version"` + Repository *Repo `json:"repository"` } -func (c *Client) get(ctx context.Context, path string, v any) error { +// fetch GETs base+path (never anything else) without credentials. +func (c *Client) fetch(ctx context.Context, path string) ([]byte, error) { req, err := http.NewRequestWithContext(ctx, "GET", c.base+path, nil) if err != nil { - return err + return nil, err } - req.Header.Set("Accept", "application/json") resp, err := c.http.Do(req) if err != nil { - return err + return nil, err } defer resp.Body.Close() if resp.StatusCode == http.StatusNotFound { - return fmt.Errorf("GET %s: %w", path, ErrNotFound) + return nil, fmt.Errorf("GET %s: %w", path, ErrNotFound) } if resp.StatusCode != http.StatusOK { - return fmt.Errorf("gitea: GET %s: %s", path, resp.Status) + return nil, fmt.Errorf("gitea: GET %s: %s", path, resp.Status) + } + return io.ReadAll(io.LimitReader(resp.Body, maxBody)) +} + +func (c *Client) get(ctx context.Context, path string, v any) error { + b, err := c.fetch(ctx, path) + if err != nil { + return err } - return json.NewDecoder(io.LimitReader(resp.Body, maxBody)).Decode(v) + return json.Unmarshal(b, v) } func (c *Client) repoPath(repo, rest string) string { @@ -186,39 +190,21 @@ func (c *Client) Packages(ctx context.Context, repos []string) ([]Package, error return out, nil } -// Avatar fetches the user's avatar image. Gitea builds avatar URLs from its -// ROOT_URL, so Gitea-hosted avatars are re-fetched through the configured base. +// Avatar fetches the user's avatar. Gitea builds avatar URLs from its +// ROOT_URL, so only the path is kept and fetched from the configured base. func (c *Client) Avatar(ctx context.Context, avatarURL string) ([]byte, string, error) { u, err := url.Parse(avatarURL) if err != nil { return nil, "", err } - target := avatarURL - if strings.HasPrefix(u.Path, "/avatars/") || strings.HasPrefix(u.Path, "/avatar/") { - target = c.base + u.RequestURI() - } else if u.Scheme != "https" { - return nil, "", fmt.Errorf("gitea: unsupported avatar url %q", avatarURL) - } - req, err := http.NewRequestWithContext(ctx, "GET", target, nil) - if err != nil { - return nil, "", err - } - resp, err := c.http.Do(req) - if err != nil { - return nil, "", err - } - defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - return nil, "", fmt.Errorf("gitea: avatar: %s", resp.Status) - } - b, err := io.ReadAll(io.LimitReader(resp.Body, 2<<20)) + b, err := c.fetch(ctx, u.RequestURI()) if err != nil { return nil, "", err } - ct := http.DetectContentType(b) - switch ct { + switch ct := http.DetectContentType(b); ct { case "image/png", "image/jpeg", "image/gif", "image/webp": return b, ct, nil + default: + return nil, "", fmt.Errorf("gitea: avatar has type %s", ct) } - return nil, "", fmt.Errorf("gitea: avatar has type %s", ct) } diff --git a/justfile b/justfile index 169d6f0..78420e5 100644 --- a/justfile +++ b/justfile @@ -16,6 +16,19 @@ lint: test -z "$(gofmt -l .)" || { gofmt -l .; exit 1; } go vet ./... +# Lint the Dockerfile +lint-docker: + hadolint Dockerfile + +# Print the current digests of the pinned base images, to compare with the Dockerfile +digests: + @echo "golang:1.27.4-alpine3.24 $(crane digest golang:1.27.4-alpine3.24)" + @echo "alpine:3.24.4 $(crane digest alpine:3.24.4)" + +# Build the image (needs Docker, e.g. on the host) +image: + docker build -t daumdev:latest . + # Build the binary into ./bin build: CGO_ENABLED=0 go build -trimpath -o bin/daumdev ./cmd/daumdev @@ -44,4 +57,4 @@ demo: # Count Go lines (budget: ~1,800, excluding tests, templates and CSS) loc: - find . -name '*.go' ! -name '*_test.go' -print0 | xargs -0 cat | wc -l + find cmd internal web -name '*.go' ! -name '*_test.go' -print0 | xargs -0 cat | wc -l