diff --git a/templates/default/home.tmpl b/templates/default/home.tmpl index 2d6ac61f..69ae3dc3 100644 --- a/templates/default/home.tmpl +++ b/templates/default/home.tmpl @@ -51,7 +51,7 @@

{{.Title}}

- {{.HeaderParsed}} + {{.Header}}
{{end}} diff --git a/website/public/home.go b/website/public/home.go index 8397c0a6..4d8658f1 100644 --- a/website/public/home.go +++ b/website/public/home.go @@ -14,7 +14,7 @@ type HomeInput struct { Queue []radio.QueueEntry LastPlayed []radio.Song - News []radio.NewsPost + News []NewsInputPost } func NewHomeInput(r *http.Request) HomeInput { @@ -57,7 +57,11 @@ func (s State) getHome(w http.ResponseWriter, r *http.Request) error { if err != nil { return errors.E(op, errors.InternalServer, err) } - input.News = news.Entries + + input.News, err = AsNewsInputPost(ctx, s.News, news.Entries) + if err != nil { + return errors.E(op, errors.InternalServer) + } return s.Templates.Execute(w, r, input) } diff --git a/website/public/news.go b/website/public/news.go index f700921e..f4628180 100644 --- a/website/public/news.go +++ b/website/public/news.go @@ -1,6 +1,7 @@ package public import ( + "context" "crypto/sha1" "fmt" "html/template" @@ -13,6 +14,7 @@ import ( "github.com/R-a-dio/valkyrie/website/middleware" "github.com/R-a-dio/valkyrie/website/shared" "github.com/go-chi/chi/v5" + "go.opentelemetry.io/otel/trace" ) const newsPageSize = 20 @@ -39,21 +41,12 @@ func (NewsInput) TemplateBundle() string { return "news" } -func NewNewsInput(cache *shared.NewsCache, ns radio.NewsStorageService, r *http.Request) (*NewsInput, error) { - const op errors.Op = "website/public.NewNewsInput" - - page, offset, err := shared.PageAndOffset(r, newsPageSize) - if err != nil { - return nil, errors.E(op, err) - } - - entries, err := ns.News(r.Context()).ListPublic(newsPageSize, offset) - if err != nil { - return nil, err - } +func AsNewsInputPost(ctx context.Context, cache *shared.NewsCache, entries []radio.NewsPost) ([]NewsInputPost, error) { + ctx, span := trace.SpanFromContext(ctx).TracerProvider().Tracer("markdown").Start(ctx, "markdown") + defer span.End() - posts := make([]NewsInputPost, 0, len(entries.Entries)) - for _, post := range entries.Entries { + posts := make([]NewsInputPost, 0, len(entries)) + for _, post := range entries { md, err := cache.RenderHeader(post) if err != nil { return nil, err @@ -68,9 +61,30 @@ func NewNewsInput(cache *shared.NewsCache, ns radio.NewsStorageService, r *http. UpdatedAt: post.UpdatedAt, }) } + return posts, nil +} + +func NewNewsInput(cache *shared.NewsCache, ns radio.NewsStorageService, r *http.Request) (*NewsInput, error) { + const op errors.Op = "website/public.NewNewsInput" + ctx := r.Context() + + page, offset, err := shared.PageAndOffset(r, newsPageSize) + if err != nil { + return nil, errors.E(op, err) + } + + entries, err := ns.News(r.Context()).ListPublic(newsPageSize, offset) + if err != nil { + return nil, errors.E(op, err) + } + + posts, err := AsNewsInputPost(ctx, cache, entries.Entries) + if err != nil { + return nil, errors.E(op, err) + } return &NewsInput{ - Input: middleware.InputFromRequest(r), + Input: middleware.InputFromContext(ctx), News: posts, NewsTotal: entries.Total, Page: shared.NewPagination(page, shared.PageCount(int64(entries.Total), newsPageSize), @@ -142,16 +156,20 @@ func NewNewsEntryInput(cache *shared.NewsCache, ns radio.NewsStorage, r *http.Re return nil, err } + ctx, span := trace.SpanFromContext(ctx).TracerProvider().Tracer("markdown").Start(ctx, "markdown") nm, err := cache.RenderBody(*post) if err != nil { + span.End() return nil, err } + span.End() raw, err := ns.Comments(post.ID) if err != nil { return nil, err } + ctx, span = trace.SpanFromContext(ctx).TracerProvider().Tracer("markdown").Start(ctx, "markdown") comments := make([]NewsEntryComment, 0, len(raw)) for _, comm := range raw { if comm.DeletedAt != nil { @@ -160,6 +178,7 @@ func NewNewsEntryInput(cache *shared.NewsCache, ns radio.NewsStorage, r *http.Re nm, err := cache.RenderComment(comm) if err != nil { + span.End() return nil, err } comments = append(comments, NewsEntryComment{ @@ -173,6 +192,7 @@ func NewNewsEntryInput(cache *shared.NewsCache, ns radio.NewsStorage, r *http.Re UpdatedAt: comm.UpdatedAt, }) } + span.End() return &NewsEntryInput{ Input: middleware.InputFromContext(ctx),