🚧 WIP of entry handler split
This commit is contained in:
parent
1ee30e4d18
commit
3e4b54cf92
|
@ -77,18 +77,19 @@ func NewApp(logger *log.Logger, config *domain.Config) (*App, error) {
|
||||||
head, tail := urlutil.ShiftPath(r.URL.Path)
|
head, tail := urlutil.ShiftPath(r.URL.Path)
|
||||||
|
|
||||||
switch strings.ToLower(head) {
|
switch strings.ToLower(head) {
|
||||||
case "":
|
case "": // NOTE(toby3d): render home page or redirect to home subfolder.
|
||||||
contentType, _, _ := mime.ParseMediaType(r.Header.Get(common.HeaderAccept))
|
mediaType, _, _ := mime.ParseMediaType(r.Header.Get(common.HeaderAccept))
|
||||||
|
|
||||||
switch contentType {
|
switch mediaType {
|
||||||
case common.MIMEApplicationLdJSON: // NOTE(toby3d): show entry as ActivityPub object.
|
case common.MIMEApplicationLdJSON:
|
||||||
entryHandler.ServeHTTP(w, r)
|
entryHandler.ServeHTTP(w, r)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE(toby3d): read $HOME_CONTENT_DIR/index.md as a source of
|
// NOTE(toby3d): read $HOME_CONTENT_DIR/index.md as a
|
||||||
// truth and global settings for any child entry.
|
// source of truth and global settings for any child
|
||||||
|
// entry.
|
||||||
s, err := siter.Do(r.Context(), domain.LanguageUnd)
|
s, err := siter.Do(r.Context(), domain.LanguageUnd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-ap/activitypub"
|
"github.com/go-ap/activitypub"
|
||||||
|
|
||||||
|
@ -20,11 +19,20 @@ import (
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Handler struct {
|
Handler struct {
|
||||||
entries entry.UseCase
|
*ThemeHandler
|
||||||
|
*ActivityPubHandler
|
||||||
sites site.UseCase
|
sites site.UseCase
|
||||||
|
entries entry.UseCase
|
||||||
themes theme.UseCase
|
themes theme.UseCase
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ThemeHandler struct {
|
||||||
|
themes theme.UseCase
|
||||||
|
}
|
||||||
|
|
||||||
|
ActivityPubHandler struct{}
|
||||||
|
|
||||||
|
/* TODO(toby3d)
|
||||||
Profile struct {
|
Profile struct {
|
||||||
*activitypub.Person
|
*activitypub.Person
|
||||||
|
|
||||||
|
@ -47,6 +55,7 @@ type (
|
||||||
// [Discoverability flag]: https://docs.joinmastodon.org/spec/activitypub/#discoverable
|
// [Discoverability flag]: https://docs.joinmastodon.org/spec/activitypub/#discoverable
|
||||||
Discoverable bool `json:"discoverable"`
|
Discoverable bool `json:"discoverable"`
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewHandler(sites site.UseCase, entries entry.UseCase, themes theme.UseCase) *Handler {
|
func NewHandler(sites site.UseCase, entries entry.UseCase, themes theme.UseCase) *Handler {
|
||||||
|
@ -54,6 +63,8 @@ func NewHandler(sites site.UseCase, entries entry.UseCase, themes theme.UseCase)
|
||||||
sites: sites,
|
sites: sites,
|
||||||
entries: entries,
|
entries: entries,
|
||||||
themes: themes,
|
themes: themes,
|
||||||
|
ThemeHandler: NewThemeHandler(themes),
|
||||||
|
ActivityPubHandler: NewActivityPubHandler(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,67 +80,35 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
contentType, params, _ := mime.ParseMediaType(r.Header.Get(common.HeaderAccept))
|
e, err := h.entries.Do(r.Context(), lang, tail)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, entry.ErrNotExist) {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
} else {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
contentType, _, _ := mime.ParseMediaType(r.Header.Get(common.HeaderAccept))
|
||||||
|
|
||||||
switch contentType {
|
switch contentType {
|
||||||
case common.MIMEApplicationLdJSON: // NOTE(toby3d): show entry as ActivityPub object.
|
default:
|
||||||
if profile, ok := params["profile"]; ok && profile != "https://www.w3.org/ns/activitystreams" {
|
h.ThemeHandler.Handle(s, e).ServeHTTP(w, r)
|
||||||
http.Error(w, "got '"+profile+"' profile value, want 'https://www.w3.org/ns/activitystreams'",
|
case common.MIMEApplicationLdJSON:
|
||||||
http.StatusBadRequest)
|
h.ActivityPubHandler.Handle(s, e).ServeHTTP(w, r)
|
||||||
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set(common.HeaderContentType, common.MIMEApplicationActivityJSONCharsetUTF8)
|
func NewThemeHandler(themes theme.UseCase) *ThemeHandler {
|
||||||
encoder := json.NewEncoder(w)
|
return &ThemeHandler{
|
||||||
|
themes: themes,
|
||||||
if head == "" { // NOTE(toby3d): base URL point to owner Profile.
|
}
|
||||||
langRef := activitypub.LangRef(lang.Lang())
|
|
||||||
person := activitypub.PersonNew(activitypub.IRI(s.BaseURL.String()))
|
|
||||||
person.URL = person.ID
|
|
||||||
person.Name.Add(activitypub.LangRefValueNew(langRef, "Maxim Lebedev"))
|
|
||||||
person.Summary.Add(activitypub.LangRefValueNew(langRef, "Creative dude from russia"))
|
|
||||||
person.PreferredUsername.Add(activitypub.LangRefValueNew(langRef, "toby3d"))
|
|
||||||
person.Published = time.Date(2009, time.February, 0, 0, 0, 0, 0, time.UTC)
|
|
||||||
|
|
||||||
_ = encoder.Encode(person)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
e, err := h.entries.Do(r.Context(), lang, tail)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, entry.ErrNotExist) {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
} else {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
resp := activitypub.ObjectNew(activitypub.NoteType)
|
|
||||||
resp.ID = activitypub.ID(s.BaseURL.JoinPath(e.File.Path()).String())
|
|
||||||
resp.Content.Add(activitypub.LangRefValueNew(activitypub.NilLangRef, string(e.Content)))
|
|
||||||
|
|
||||||
_ = encoder.Encode(resp)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// NOTE(toby3d): search entry for requested URL and language
|
|
||||||
// code in subdir.
|
|
||||||
e, err := h.entries.Do(r.Context(), lang, tail)
|
|
||||||
if err != nil {
|
|
||||||
if errors.Is(err, entry.ErrNotExist) {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
} else {
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *ThemeHandler) Handle(s *domain.Site, e *domain.Entry) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// NOTE(toby3d): wrap founded entry into theme template and
|
// NOTE(toby3d): wrap founded entry into theme template and
|
||||||
// answer to client.
|
// answer to client.
|
||||||
contentLanguage := make([]string, len(e.Translations))
|
contentLanguage := make([]string, len(e.Translations))
|
||||||
|
@ -150,4 +129,38 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if err = template(w); err != nil {
|
if err = template(w); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewActivityPubHandler() *ActivityPubHandler {
|
||||||
|
return &ActivityPubHandler{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ActivityPubHandler) Handle(s *domain.Site, e *domain.Entry) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||||
|
w.Header().Set(common.HeaderContentType, common.MIMEApplicationActivityJSONCharsetUTF8)
|
||||||
|
encoder := json.NewEncoder(w)
|
||||||
|
|
||||||
|
/* TODO(toby3d): handle profiles on root page.
|
||||||
|
if head == "" { // NOTE(toby3d): base URL point to owner Profile.
|
||||||
|
langRef := activitypub.LangRef(lang.Lang())
|
||||||
|
person := activitypub.PersonNew(activitypub.IRI(s.BaseURL.String()))
|
||||||
|
person.URL = person.ID
|
||||||
|
person.Name.Add(activitypub.LangRefValueNew(langRef, "Maxim Lebedev"))
|
||||||
|
person.Summary.Add(activitypub.LangRefValueNew(langRef, "Creative dude from russia"))
|
||||||
|
person.PreferredUsername.Add(activitypub.LangRefValueNew(langRef, "toby3d"))
|
||||||
|
person.Published = time.Date(2009, time.February, 0, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
|
_ = encoder.Encode(person)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
resp := activitypub.ObjectNew(activitypub.NoteType)
|
||||||
|
resp.ID = activitypub.ID(s.BaseURL.JoinPath(e.File.Path()).String())
|
||||||
|
resp.Content.Add(activitypub.LangRefValueNew(activitypub.NilLangRef, string(e.Content)))
|
||||||
|
|
||||||
|
_ = encoder.Encode(resp)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user