🏗️ Created theme HTTP delivery

This commit is contained in:
Maxim Lebedev 2024-02-14 23:43:50 +06:00
parent 8437b57c73
commit 8cf7d160c2
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
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)
}
})
}