♻️ Refactored entries HTTP delivery with ActivityPub support

This commit is contained in:
Maxim Lebedev 2024-02-15 00:06:19 +06:00
parent 495fbc523a
commit a509c797ba
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
3 changed files with 81 additions and 87 deletions

View File

@ -5,13 +5,16 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"log" "log"
"mime"
"net" "net"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"time" "time"
"source.toby3d.me/toby3d/home/internal/common"
"source.toby3d.me/toby3d/home/internal/domain" "source.toby3d.me/toby3d/home/internal/domain"
entryhttpdelivery "source.toby3d.me/toby3d/home/internal/entry/delivery/http"
pagefsrepo "source.toby3d.me/toby3d/home/internal/entry/repository/fs" pagefsrepo "source.toby3d.me/toby3d/home/internal/entry/repository/fs"
pageucase "source.toby3d.me/toby3d/home/internal/entry/usecase" pageucase "source.toby3d.me/toby3d/home/internal/entry/usecase"
"source.toby3d.me/toby3d/home/internal/middleware" "source.toby3d.me/toby3d/home/internal/middleware"
@ -25,7 +28,6 @@ import (
statichttpdelivery "source.toby3d.me/toby3d/home/internal/static/delivery/http" statichttpdelivery "source.toby3d.me/toby3d/home/internal/static/delivery/http"
staticfsrepo "source.toby3d.me/toby3d/home/internal/static/repository/fs" staticfsrepo "source.toby3d.me/toby3d/home/internal/static/repository/fs"
staticucase "source.toby3d.me/toby3d/home/internal/static/usecase" staticucase "source.toby3d.me/toby3d/home/internal/static/usecase"
themehttpdelivery "source.toby3d.me/toby3d/home/internal/theme/delivery/http"
themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs" themefsrepo "source.toby3d.me/toby3d/home/internal/theme/repository/fs"
themeucase "source.toby3d.me/toby3d/home/internal/theme/usecase" themeucase "source.toby3d.me/toby3d/home/internal/theme/usecase"
"source.toby3d.me/toby3d/home/internal/urlutil" "source.toby3d.me/toby3d/home/internal/urlutil"
@ -59,10 +61,10 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
serverer := servercase.NewServerUseCase(sites) serverer := servercase.NewServerUseCase(sites)
webfingerer := webfingerucase.NewWebFingerUseCase(sites) webfingerer := webfingerucase.NewWebFingerUseCase(sites)
webfingerHandler := webfingerhttpdelivery.NewHandler(webfingerer) webfingerHandler := webfingerhttpdelivery.NewHandler(webfingerer)
themeHandler := themehttpdelivery.NewHandler(themer)
resourceHandler := resourcehttpdelivery.NewHandler(resourcer, contentDir) resourceHandler := resourcehttpdelivery.NewHandler(resourcer, contentDir)
staticHandler := statichttpdelivery.NewHandler(staticer) staticHandler := statichttpdelivery.NewHandler(staticer)
siteHandler := sitehttpdelivery.NewHandler(siter) siteHandler := sitehttpdelivery.NewHandler(siter)
entryHandler := entryhttpdelivery.NewHandler(themer)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// NOTE(toby3d): any file in $HOME_STATIC_DIR is public and // NOTE(toby3d): any file in $HOME_STATIC_DIR is public and
// unprotected by design, so it's safe to search it first before // unprotected by design, so it's safe to search it first before
@ -96,7 +98,12 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
} }
if handler != nil { if handler != nil {
handler.ServeHTTP(w, r) mediaType, _, _ := mime.ParseMediaType(r.Header.Get(common.HeaderAccept))
if strings.EqualFold(mediaType, common.MIMEApplicationLdJSON) {
entryHandler.ActivityPubHandler.HandleProfile(site).ServeHTTP(w, r)
} else {
handler.ServeHTTP(w, r)
}
return return
} }
@ -118,7 +125,7 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
return return
} }
themeHandler.Handle(site, entry).ServeHTTP(w, r) entryHandler.Handle(site, entry).ServeHTTP(w, r)
}) })
chain := middleware.Chain{ chain := middleware.Chain{
// middleware.LogFmt(), // middleware.LogFmt(),

View File

@ -1,27 +1,31 @@
package http package http
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt"
"mime" "mime"
"net/http" "net/http"
"strings"
"time"
"github.com/go-ap/activitypub" "github.com/go-ap/activitypub"
"source.toby3d.me/toby3d/home/internal/common" "source.toby3d.me/toby3d/home/internal/common"
"source.toby3d.me/toby3d/home/internal/domain" "source.toby3d.me/toby3d/home/internal/domain"
"source.toby3d.me/toby3d/home/internal/entry" "source.toby3d.me/toby3d/home/internal/theme"
"source.toby3d.me/toby3d/home/internal/site"
) )
type ( type (
Handler struct { Handler struct {
entries entry.UseCase *ActivityPubHandler
*TemplateHandler
} }
ActivityPubHandler struct{} ActivityPubHandler struct{}
TemplateHandler struct {
themes theme.UseCase
}
/* TODO(toby3d) /* TODO(toby3d)
Profile struct { Profile struct {
*activitypub.Person *activitypub.Person
@ -48,56 +52,88 @@ type (
*/ */
) )
func NewHandler(sites site.UseCase, entries entry.UseCase) *Handler { func NewHandler(themes theme.UseCase) *Handler {
return &Handler{ return &Handler{
entries: entries, ActivityPubHandler: NewActivityPubHandler(),
TemplateHandler: NewTemplateHandler(themes),
} }
} }
func (h *Handler) Handle(ctx context.Context, site *domain.Site, path string) (*domain.Entry, http.Handler, error) { func (h *Handler) Handle(site *domain.Site, entry *domain.Entry) http.Handler {
entry, err := h.entries.Do(ctx, site.Language, path) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err != nil { mediaType, _, _ := mime.ParseMediaType(r.Header.Get(common.HeaderAccept))
return nil, nil, fmt.Errorf("cannot handle entry: %w", err)
}
return entry, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch strings.ToLower(mediaType) {
contentType, _, _ := mime.ParseMediaType(r.Header.Get(common.HeaderAccept)) default:
h.TemplateHandler.Handle(site, entry).ServeHTTP(w, r)
switch contentType {
case common.MIMEApplicationLdJSON: case common.MIMEApplicationLdJSON:
h.ActivityPubHandler.Handle(s, e).ServeHTTP(w, r) h.ActivityPubHandler.Handle(site, entry).ServeHTTP(w, r)
} }
}), nil })
}
func NewTemplateHandler(themes theme.UseCase) *TemplateHandler {
return &TemplateHandler{themes: themes}
}
func (h *TemplateHandler) Handle(site *domain.Site, entry *domain.Entry) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO(toby3d): handle home page.
// TODO(toby3d): handle sections.
// TODO(toby3d): handle errors.
// NOTE(toby3d): wrap founded entry into theme template and
// answer to client.
contentLanguage := make([]string, len(entry.Translations))
for i := range entry.Translations {
contentLanguage[i] = entry.Translations[i].Language.Code()
}
w.Header().Set(common.HeaderContentLanguage, strings.Join(contentLanguage, ", "))
template, err := h.themes.Do(r.Context(), site, entry)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
if err = template(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
} }
func NewActivityPubHandler() *ActivityPubHandler { func NewActivityPubHandler() *ActivityPubHandler {
return &ActivityPubHandler{} return &ActivityPubHandler{}
} }
func (ActivityPubHandler) Handle(s *domain.Site, e *domain.Entry) http.Handler { func (ActivityPubHandler) HandleProfile(site *domain.Site) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set(common.HeaderContentType, common.MIMEApplicationActivityJSONCharsetUTF8) w.Header().Set(common.HeaderContentType, common.MIMEApplicationActivityJSONCharsetUTF8)
encoder := json.NewEncoder(w) encoder := json.NewEncoder(w)
/* TODO(toby3d): handle profiles on root page. langRef := activitypub.LangRef(site.DefaultLanguage.Lang())
if head == "" { // NOTE(toby3d): base URL point to owner Profile. person := activitypub.PersonNew(activitypub.IRI(site.BaseURL.String()))
langRef := activitypub.LangRef(lang.Lang()) person.URL = person.ID
person := activitypub.PersonNew(activitypub.IRI(s.BaseURL.String())) person.Name.Add(activitypub.LangRefValueNew(langRef, "Maxim Lebedev"))
person.URL = person.ID person.Summary.Add(activitypub.LangRefValueNew(langRef, "Creative dude from russia"))
person.Name.Add(activitypub.LangRefValueNew(langRef, "Maxim Lebedev")) person.PreferredUsername.Add(activitypub.LangRefValueNew(langRef, "toby3d"))
person.Summary.Add(activitypub.LangRefValueNew(langRef, "Creative dude from russia")) person.Published = time.Date(2009, time.February, 0, 0, 0, 0, 0, time.UTC)
person.PreferredUsername.Add(activitypub.LangRefValueNew(langRef, "toby3d"))
person.Published = time.Date(2009, time.February, 0, 0, 0, 0, 0, time.UTC)
_ = encoder.Encode(person) _ = encoder.Encode(person)
})
}
return func (ActivityPubHandler) Handle(site *domain.Site, entry *domain.Entry) http.Handler {
} return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
*/ w.Header().Set(common.HeaderContentType, common.MIMEApplicationActivityJSONCharsetUTF8)
encoder := json.NewEncoder(w)
resp := activitypub.ObjectNew(activitypub.NoteType) resp := activitypub.ObjectNew(activitypub.NoteType)
resp.ID = activitypub.ID(s.BaseURL.JoinPath(e.File.Path()).String()) resp.ID = activitypub.ID(site.BaseURL.JoinPath(entry.File.Path()).String())
resp.Content.Add(activitypub.LangRefValueNew(activitypub.NilLangRef, string(e.Content))) resp.Content.Add(activitypub.LangRefValueNew(activitypub.NilLangRef, string(entry.Content)))
_ = encoder.Encode(resp) _ = encoder.Encode(resp)
}) })

View File

@ -1,49 +0,0 @@
package http
import (
"net/http"
"strings"
"source.toby3d.me/toby3d/home/internal/common"
"source.toby3d.me/toby3d/home/internal/domain"
"source.toby3d.me/toby3d/home/internal/theme"
)
type Handler struct {
themes theme.UseCase
}
func NewHandler(themes theme.UseCase) *Handler {
return &Handler{
themes: themes,
}
}
func (h *Handler) Handle(s *domain.Site, e *domain.Entry) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO(toby3d): handle home page.
// TODO(toby3d): handle sections.
// TODO(toby3d): handle errors.
// NOTE(toby3d): wrap founded entry into theme template and
// answer to client.
contentLanguage := make([]string, len(e.Translations))
for i := range e.Translations {
contentLanguage[i] = e.Translations[i].Language.Code()
}
w.Header().Set(common.HeaderContentLanguage, strings.Join(contentLanguage, ", "))
template, err := h.themes.Do(r.Context(), s, e)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set(common.HeaderContentType, common.MIMETextHTMLCharsetUTF8)
if err = template(w); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
}