Skip to content

Commit

Permalink
website: add news rendering and telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Feb 28, 2024
1 parent b7cbf9a commit 67a9210
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion templates/default/home.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<p>{{.Title}}</p>
</div>
<div class="message-body">
{{.HeaderParsed}}
{{.Header}}
</div>
</article>
{{end}}
Expand Down
8 changes: 6 additions & 2 deletions website/public/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type HomeInput struct {

Queue []radio.QueueEntry
LastPlayed []radio.Song
News []radio.NewsPost
News []NewsInputPost
}

func NewHomeInput(r *http.Request) HomeInput {
Expand Down Expand Up @@ -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)
}
50 changes: 35 additions & 15 deletions website/public/news.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package public

import (
"context"
"crypto/sha1"
"fmt"
"html/template"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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),
Expand Down

0 comments on commit 67a9210

Please sign in to comment.