refactoring to have a dedicated site config service. Default config for new setups

This commit is contained in:
Niko Abeler 2024-02-25 14:01:55 +01:00
parent be62bcd627
commit 5cc55a79ff
24 changed files with 195 additions and 153 deletions

View File

@ -4,7 +4,6 @@ import (
"crypto/sha256" "crypto/sha256"
"fmt" "fmt"
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"strings" "strings"
@ -13,11 +12,11 @@ import (
type AuthorService struct { type AuthorService struct {
repo repository.AuthorRepository repo repository.AuthorRepository
siteConfigRepo repository.ConfigRepository siteConfigService *SiteConfigService
} }
func NewAuthorService(repo repository.AuthorRepository, siteConfigRepo repository.ConfigRepository) *AuthorService { func NewAuthorService(repo repository.AuthorRepository, siteConfigService *SiteConfigService) *AuthorService {
return &AuthorService{repo: repo, siteConfigRepo: siteConfigRepo} return &AuthorService{repo: repo, siteConfigService: siteConfigService}
} }
func hashPassword(password string) (string, error) { func hashPassword(password string) (string, error) {
@ -65,14 +64,13 @@ func (s *AuthorService) Authenticate(name string, password string) bool {
} }
func (s *AuthorService) getSecretKey() string { func (s *AuthorService) getSecretKey() string {
siteConfig := model.SiteConfig{} siteConfig, err := s.siteConfigService.GetSiteConfig()
err := s.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if siteConfig.Secret == "" { if siteConfig.Secret == "" {
siteConfig.Secret = RandStringRunes(64) siteConfig.Secret = RandStringRunes(64)
err = s.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = s.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -0,0 +1,59 @@
package app
import (
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"reflect"
)
// SiteConfigService is a service to retrieve and store the site config
// Even though the site config is a standard config, it is handle by an extra service
// as it is used in many places.
// The SiteConfig contains global settings require by multiple parts of the app
type SiteConfigService struct {
repo repository.ConfigRepository
}
func NewSiteConfigService(repo repository.ConfigRepository) *SiteConfigService {
return &SiteConfigService{
repo: repo,
}
}
func (svc *SiteConfigService) defaultConfig() model.SiteConfig {
return model.SiteConfig{
Title: "My Owl-Blog",
SubTitle: "A freshly created blog",
HeaderColor: "#efc48c",
PrimaryColor: "#d37f12",
AuthorName: "",
Me: []model.MeLinks{},
Lists: []model.EntryList{},
PrimaryListInclude: []string{},
HeaderMenu: []model.MenuItem{},
FooterMenu: []model.MenuItem{},
Secret: "",
AvatarUrl: "",
FullUrl: "http://localhost:3000",
HtmlHeadExtra: "",
FooterExtra: "",
}
}
func (svc *SiteConfigService) GetSiteConfig() (model.SiteConfig, error) {
siteConfig := model.SiteConfig{}
err := svc.repo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil {
println("ERROR IN SITE CONFIG")
return model.SiteConfig{}, err
}
if reflect.ValueOf(siteConfig).IsZero() {
return svc.defaultConfig(), nil
}
return siteConfig, nil
}
func (svc *SiteConfigService) UpdateSiteConfig(cfg model.SiteConfig) error {
return svc.repo.Update(config.SITE_CONFIG, cfg)
}

View File

@ -5,28 +5,27 @@ import (
"net/url" "net/url"
"owl-blogs/app/owlhttp" "owl-blogs/app/owlhttp"
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/interactions" "owl-blogs/interactions"
"time" "time"
) )
type WebmentionService struct { type WebmentionService struct {
ConfigRepo repository.ConfigRepository siteConfigService *SiteConfigService
InteractionRepository repository.InteractionRepository InteractionRepository repository.InteractionRepository
EntryRepository repository.EntryRepository EntryRepository repository.EntryRepository
Http owlhttp.HttpClient Http owlhttp.HttpClient
} }
func NewWebmentionService( func NewWebmentionService(
configRepo repository.ConfigRepository, siteConfigService *SiteConfigService,
interactionRepository repository.InteractionRepository, interactionRepository repository.InteractionRepository,
entryRepository repository.EntryRepository, entryRepository repository.EntryRepository,
http owlhttp.HttpClient, http owlhttp.HttpClient,
bus *EventBus, bus *EventBus,
) *WebmentionService { ) *WebmentionService {
svc := &WebmentionService{ svc := &WebmentionService{
ConfigRepo: configRepo, siteConfigService: siteConfigService,
InteractionRepository: interactionRepository, InteractionRepository: interactionRepository,
EntryRepository: entryRepository, EntryRepository: entryRepository,
Http: http, Http: http,
@ -104,8 +103,7 @@ func (s *WebmentionService) ScanForLinks(entry model.Entry) ([]string, error) {
} }
func (s *WebmentionService) FullEntryUrl(entry model.Entry) string { func (s *WebmentionService) FullEntryUrl(entry model.Entry) string {
siteConfig := model.SiteConfig{} siteConfig, _ := s.siteConfigService.GetSiteConfig()
s.ConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
url, _ := url.JoinPath( url, _ := url.JoinPath(
siteConfig.FullUrl, siteConfig.FullUrl,

View File

@ -8,6 +8,7 @@ import (
"owl-blogs/infra" "owl-blogs/infra"
"owl-blogs/interactions" "owl-blogs/interactions"
"owl-blogs/plugings" "owl-blogs/plugings"
"owl-blogs/render"
"owl-blogs/web" "owl-blogs/web"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -57,13 +58,17 @@ func App(db infra.Database) *web.WebApp {
eventBus := app.NewEventBus() eventBus := app.NewEventBus()
// Create Services // Create Services
siteConfigService := app.NewSiteConfigService(configRepo)
entryService := app.NewEntryService(entryRepo, eventBus) entryService := app.NewEntryService(entryRepo, eventBus)
binaryService := app.NewBinaryFileService(binRepo) binaryService := app.NewBinaryFileService(binRepo)
authorService := app.NewAuthorService(authorRepo, configRepo) authorService := app.NewAuthorService(authorRepo, siteConfigService)
webmentionService := app.NewWebmentionService( webmentionService := app.NewWebmentionService(
configRepo, interactionRepo, entryRepo, httpClient, eventBus, siteConfigService, interactionRepo, entryRepo, httpClient, eventBus,
) )
// setup render functions
render.SiteConfigService = siteConfigService
// plugins // plugins
plugings.NewEcho(eventBus) plugings.NewEcho(eventBus)
plugings.RegisterInstagram( plugings.RegisterInstagram(
@ -74,7 +79,7 @@ func App(db infra.Database) *web.WebApp {
return web.NewWebApp( return web.NewWebApp(
entryService, entryRegister, binaryService, entryService, entryRegister, binaryService,
authorService, configRepo, configRegister, authorService, configRepo, configRegister,
webmentionService, interactionRepo, siteConfigService, webmentionService, interactionRepo,
) )
} }

View File

@ -0,0 +1,6 @@
package model
type SiteConfigInterface interface {
GetSiteConfig() (SiteConfig, error)
UpdateSiteConfig(cfg SiteConfig) error
}

View File

@ -21,6 +21,7 @@ type TemplateData struct {
//go:embed templates //go:embed templates
var templates embed.FS var templates embed.FS
var SiteConfigService model.SiteConfigInterface
var funcMap = template.FuncMap{ var funcMap = template.FuncMap{
"markdown": func(text string) string { "markdown": func(text string) string {
@ -45,7 +46,7 @@ func CreateTemplateWithBase(templateName string) (*template.Template, error) {
) )
} }
func RenderTemplateWithBase(w io.Writer, siteConfig model.SiteConfig, templateName string, data interface{}) error { func RenderTemplateWithBase(w io.Writer, templateName string, data interface{}) error {
t, err := CreateTemplateWithBase(templateName) t, err := CreateTemplateWithBase(templateName)
@ -53,6 +54,11 @@ func RenderTemplateWithBase(w io.Writer, siteConfig model.SiteConfig, templateNa
return err return err
} }
siteConfig, err := SiteConfigService.GetSiteConfig()
if err != nil {
return err
}
err = t.ExecuteTemplate(w, "base", TemplateData{ err = t.ExecuteTemplate(w, "base", TemplateData{
Data: data, Data: data,
SiteConfig: siteConfig, SiteConfig: siteConfig,

View File

@ -6,8 +6,10 @@
{{ if .Data.Entry.ImageUrl}} {{ if .Data.Entry.ImageUrl}}
<meta property="og:image" content="{{ urljoin .SiteConfig.FullUrl .Data.Entry.ImageUrl}}" /> <meta property="og:image" content="{{ urljoin .SiteConfig.FullUrl .Data.Entry.ImageUrl}}" />
{{ end }} {{ end }}
{{ if .Data.Entry.PublishedAt }}
<meta property="og:article:published_time" content="{{.Data.Entry.PublishedAt.Format "2006-01-02T15:04:05" }}" /> <meta property="og:article:published_time" content="{{.Data.Entry.PublishedAt.Format "2006-01-02T15:04:05" }}" />
{{ end }} {{ end }}
{{end}}
{{define "main"}} {{define "main"}}

View File

@ -36,7 +36,6 @@ func NewAdminHandler(
func (h *adminHandler) Handle(c *fiber.Ctx) error { func (h *adminHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := getSiteConfig(h.configRepo)
configs := h.configRegister.Configs() configs := h.configRegister.Configs()
types := h.typeRegistry.Types() types := h.typeRegistry.Types()
@ -53,8 +52,7 @@ func (h *adminHandler) Handle(c *fiber.Ctx) error {
}) })
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, siteConfig, c, "views/admin", &adminContet{
"views/admin", &adminContet{
Configs: configs, Configs: configs,
Types: typeNames, Types: typeNames,
}, },
@ -73,14 +71,13 @@ func (h *adminHandler) HandleConfigGet(c *fiber.Ctx) error {
if err != nil { if err != nil {
return err return err
} }
siteConfig := getSiteConfig(h.configRepo)
htmlForm := config.Form(h.binSvc) htmlForm := config.Form(h.binSvc)
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase(c, siteConfig, "views/admin_config", htmlForm) return render.RenderTemplateWithBase(c, "views/admin_config", htmlForm)
} }
func (h *adminHandler) HandleConfigPost(c *fiber.Ctx) error { func (h *adminHandler) HandleConfigPost(c *fiber.Ctx) error {

View File

@ -20,8 +20,6 @@ func NewAdminInteractionHandler(configRepo repository.ConfigRepository, interact
} }
func (h *AdminInteractionHandler) HandleGet(c *fiber.Ctx) error { func (h *AdminInteractionHandler) HandleGet(c *fiber.Ctx) error {
siteConfig := getSiteConfig(h.configRepo)
filter := c.Query("filter", "") filter := c.Query("filter", "")
interactions, err := h.interactionRepo.ListAllInteractions() interactions, err := h.interactionRepo.ListAllInteractions()
@ -31,7 +29,7 @@ func (h *AdminInteractionHandler) HandleGet(c *fiber.Ctx) error {
pageData := paginate(c, interactions, 50) pageData := paginate(c, interactions, 50)
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return render.RenderTemplateWithBase(c, siteConfig, "views/interaction_manager", fiber.Map{ return render.RenderTemplateWithBase(c, "views/interaction_manager", fiber.Map{
"Interactions": pageData.items, "Interactions": pageData.items,
"Page": pageData.page, "Page": pageData.page,
"NextPage": pageData.page + 1, "NextPage": pageData.page + 1,

View File

@ -7,8 +7,6 @@ import (
"net/url" "net/url"
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"owl-blogs/web/middleware" "owl-blogs/web/middleware"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -34,17 +32,18 @@ func NewWebApp(
authorService *app.AuthorService, authorService *app.AuthorService,
configRepo repository.ConfigRepository, configRepo repository.ConfigRepository,
configRegister *app.ConfigRegister, configRegister *app.ConfigRegister,
siteConfigService *app.SiteConfigService,
webmentionService *app.WebmentionService, webmentionService *app.WebmentionService,
interactionRepo repository.InteractionRepository, interactionRepo repository.InteractionRepository,
) *WebApp { ) *WebApp {
app := fiber.New() app := fiber.New()
app.Use(middleware.NewUserMiddleware(authorService).Handle) app.Use(middleware.NewUserMiddleware(authorService).Handle)
indexHandler := NewIndexHandler(entryService, configRepo) indexHandler := NewIndexHandler(entryService, siteConfigService)
listHandler := NewListHandler(entryService, configRepo) listHandler := NewListHandler(entryService, siteConfigService)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo, interactionRepo) entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo, interactionRepo)
mediaHandler := NewMediaHandler(binService) mediaHandler := NewMediaHandler(binService)
rssHandler := NewRSSHandler(entryService, configRepo) rssHandler := NewRSSHandler(entryService, siteConfigService)
loginHandler := NewLoginHandler(authorService, configRepo) loginHandler := NewLoginHandler(authorService, configRepo)
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, configRepo) editorHandler := NewEditorHandler(entryService, typeRegistry, binService, configRepo)
webmentionHandler := NewWebmentionHandler(webmentionService, configRepo) webmentionHandler := NewWebmentionHandler(webmentionService, configRepo)
@ -55,7 +54,7 @@ func NewWebApp(
// admin // admin
adminHandler := NewAdminHandler(configRepo, configRegister, typeRegistry) adminHandler := NewAdminHandler(configRepo, configRegister, typeRegistry)
draftHandler := NewDraftHandler(entryService, configRepo) draftHandler := NewDraftHandler(entryService, siteConfigService)
binaryManageHandler := NewBinaryManageHandler(configRepo, binService) binaryManageHandler := NewBinaryManageHandler(configRepo, binService)
adminInteractionHandler := NewAdminInteractionHandler(configRepo, interactionRepo) adminInteractionHandler := NewAdminInteractionHandler(configRepo, interactionRepo)
admin := app.Group("/admin") admin := app.Group("/admin")
@ -87,21 +86,21 @@ func NewWebApp(
siteConfig := app.Group("/site-config") siteConfig := app.Group("/site-config")
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle) siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
siteConfigHandler := NewSiteConfigHandler(configRepo) siteConfigHandler := NewSiteConfigHandler(siteConfigService)
siteConfig.Get("/", siteConfigHandler.HandleGet) siteConfig.Get("/", siteConfigHandler.HandleGet)
siteConfig.Post("/", siteConfigHandler.HandlePost) siteConfig.Post("/", siteConfigHandler.HandlePost)
siteConfigMeHandler := NewSiteConfigMeHandler(configRepo) siteConfigMeHandler := NewSiteConfigMeHandler(siteConfigService)
siteConfig.Get("/me", siteConfigMeHandler.HandleGet) siteConfig.Get("/me", siteConfigMeHandler.HandleGet)
siteConfig.Post("/me/create/", siteConfigMeHandler.HandleCreate) siteConfig.Post("/me/create/", siteConfigMeHandler.HandleCreate)
siteConfig.Post("/me/delete/", siteConfigMeHandler.HandleDelete) siteConfig.Post("/me/delete/", siteConfigMeHandler.HandleDelete)
siteConfigListHandler := NewSiteConfigListHandler(configRepo, typeRegistry) siteConfigListHandler := NewSiteConfigListHandler(siteConfigService, typeRegistry)
siteConfig.Get("/lists", siteConfigListHandler.HandleGet) siteConfig.Get("/lists", siteConfigListHandler.HandleGet)
siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate) siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate)
siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete) siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete)
siteConfigMenusHandler := NewSiteConfigMenusHandler(configRepo) siteConfigMenusHandler := NewSiteConfigMenusHandler(siteConfigService)
siteConfig.Get("/menus", siteConfigMenusHandler.HandleGet) siteConfig.Get("/menus", siteConfigMenusHandler.HandleGet)
siteConfig.Post("/menus/create/", siteConfigMenusHandler.HandleCreate) siteConfig.Post("/menus/create/", siteConfigMenusHandler.HandleCreate)
siteConfig.Post("/menus/delete/", siteConfigMenusHandler.HandleDelete) siteConfig.Post("/menus/delete/", siteConfigMenusHandler.HandleDelete)
@ -124,14 +123,13 @@ func NewWebApp(
app.Post("/webmention/", webmentionHandler.Handle) app.Post("/webmention/", webmentionHandler.Handle)
// robots.txt // robots.txt
app.Get("/robots.txt", func(c *fiber.Ctx) error { app.Get("/robots.txt", func(c *fiber.Ctx) error {
siteConfig := model.SiteConfig{} siteConfig, _ := siteConfigService.GetSiteConfig()
configRepo.Get(config.SITE_CONFIG, &siteConfig)
sitemapUrl, _ := url.JoinPath(siteConfig.FullUrl, "/sitemap.xml") sitemapUrl, _ := url.JoinPath(siteConfig.FullUrl, "/sitemap.xml")
c.Set("Content-Type", "text/plain") c.Set("Content-Type", "text/plain")
return c.SendString(fmt.Sprintf("User-agent: GPTBot\nDisallow: /\n\nUser-agent: *\nAllow: /\n\nSitemap: %s\n", sitemapUrl)) return c.SendString(fmt.Sprintf("User-agent: GPTBot\nDisallow: /\n\nUser-agent: *\nAllow: /\n\nSitemap: %s\n", sitemapUrl))
}) })
// sitemap.xml // sitemap.xml
app.Get("/sitemap.xml", NewSiteMapHandler(entryService, configRepo).Handle) app.Get("/sitemap.xml", NewSiteMapHandler(entryService, siteConfigService).Handle)
// ActivityPub // ActivityPub
activityPubServer := NewActivityPubServer(configRepo, entryService) activityPubServer := NewActivityPubServer(configRepo, entryService)

View File

@ -24,8 +24,6 @@ func NewBinaryManageHandler(configRepo repository.ConfigRepository, service *app
} }
func (h *BinaryManageHandler) Handle(c *fiber.Ctx) error { func (h *BinaryManageHandler) Handle(c *fiber.Ctx) error {
siteConfig := getSiteConfig(h.configRepo)
filter := c.Query("filter", "") filter := c.Query("filter", "")
allIds, err := h.service.ListIds(filter) allIds, err := h.service.ListIds(filter)
@ -38,7 +36,7 @@ func (h *BinaryManageHandler) Handle(c *fiber.Ctx) error {
pageData := paginate(c, allIds, 50) pageData := paginate(c, allIds, 50)
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return render.RenderTemplateWithBase(c, siteConfig, "views/binary_manager", fiber.Map{ return render.RenderTemplateWithBase(c, "views/binary_manager", fiber.Map{
"Binaries": pageData.items, "Binaries": pageData.items,
"Page": pageData.page, "Page": pageData.page,
"NextPage": pageData.page + 1, "NextPage": pageData.page + 1,

View File

@ -2,7 +2,6 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"sort" "sort"
@ -12,17 +11,17 @@ import (
) )
type DraftHandler struct { type DraftHandler struct {
configRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
entrySvc *app.EntryService entrySvc *app.EntryService
} }
func NewDraftHandler( func NewDraftHandler(
entryService *app.EntryService, entryService *app.EntryService,
configRepo repository.ConfigRepository, siteConfigService *app.SiteConfigService,
) *DraftHandler { ) *DraftHandler {
return &DraftHandler{ return &DraftHandler{
entrySvc: entryService, entrySvc: entryService,
configRepo: configRepo, siteConfigService: siteConfigService,
} }
} }
@ -38,7 +37,10 @@ type DraftRenderData struct {
func (h *DraftHandler) Handle(c *fiber.Ctx) error { func (h *DraftHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := getSiteConfig(h.configRepo) siteConfig, err := h.siteConfigService.GetSiteConfig()
if err != nil {
return err
}
entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, false, true) entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, false, true)
if err != nil { if err != nil {
@ -76,7 +78,7 @@ func (h *DraftHandler) Handle(c *fiber.Ctx) error {
return err return err
} }
return render.RenderTemplateWithBase(c, siteConfig, "views/draft_list", DraftRenderData{ return render.RenderTemplateWithBase(c, "views/draft_list", DraftRenderData{
Entries: entries, Entries: entries,
Page: pageNum, Page: pageNum,
NextPage: pageNum + 1, NextPage: pageNum + 1,

View File

@ -48,7 +48,7 @@ func (h *EditorHandler) HandleGetNew(c *fiber.Ctx) error {
return err return err
} }
htmlForm := entryType.MetaData().Form(h.binSvc) htmlForm := entryType.MetaData().Form(h.binSvc)
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor", htmlForm) return render.RenderTemplateWithBase(c, "views/editor", htmlForm)
} }
func (h *EditorHandler) HandlePostNew(c *fiber.Ctx) error { func (h *EditorHandler) HandlePostNew(c *fiber.Ctx) error {
@ -94,7 +94,7 @@ func (h *EditorHandler) HandleGetEdit(c *fiber.Ctx) error {
} }
htmlForm := entry.MetaData().Form(h.binSvc) htmlForm := entry.MetaData().Form(h.binSvc)
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor", htmlForm) return render.RenderTemplateWithBase(c, "views/editor", htmlForm)
} }
func (h *EditorHandler) HandlePostEdit(c *fiber.Ctx) error { func (h *EditorHandler) HandlePostEdit(c *fiber.Ctx) error {

View File

@ -72,7 +72,6 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, c,
getSiteConfig(h.configRepo),
"views/entry", "views/entry",
entryData{ entryData{
Entry: entry, Entry: entry,

View File

@ -2,7 +2,6 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"sort" "sort"
@ -12,17 +11,17 @@ import (
) )
type IndexHandler struct { type IndexHandler struct {
configRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
entrySvc *app.EntryService entrySvc *app.EntryService
} }
func NewIndexHandler( func NewIndexHandler(
entryService *app.EntryService, entryService *app.EntryService,
configRepo repository.ConfigRepository, siteConfigService *app.SiteConfigService,
) *IndexHandler { ) *IndexHandler {
return &IndexHandler{ return &IndexHandler{
entrySvc: entryService, entrySvc: entryService,
configRepo: configRepo, siteConfigService: siteConfigService,
} }
} }
@ -38,7 +37,10 @@ type indexRenderData struct {
func (h *IndexHandler) Handle(c *fiber.Ctx) error { func (h *IndexHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := getSiteConfig(h.configRepo) siteConfig, err := h.siteConfigService.GetSiteConfig()
if err != nil {
return err
}
entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, true, false) entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, true, false)
if err != nil { if err != nil {
@ -76,7 +78,7 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return err return err
} }
return render.RenderTemplateWithBase(c, siteConfig, "views/index", indexRenderData{ return render.RenderTemplateWithBase(c, "views/index", indexRenderData{
Entries: entries, Entries: entries,
Page: pageNum, Page: pageNum,
NextPage: pageNum + 1, NextPage: pageNum + 1,

View File

@ -2,7 +2,6 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"sort" "sort"
@ -12,17 +11,17 @@ import (
) )
type ListHandler struct { type ListHandler struct {
configRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
entrySvc *app.EntryService entrySvc *app.EntryService
} }
func NewListHandler( func NewListHandler(
entryService *app.EntryService, entryService *app.EntryService,
configRepo repository.ConfigRepository, siteConfigService *app.SiteConfigService,
) *ListHandler { ) *ListHandler {
return &ListHandler{ return &ListHandler{
entrySvc: entryService, entrySvc: entryService,
configRepo: configRepo, siteConfigService: siteConfigService,
} }
} }
@ -39,7 +38,10 @@ type listRenderData struct {
func (h *ListHandler) Handle(c *fiber.Ctx) error { func (h *ListHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := getSiteConfig(h.configRepo) siteConfig, err := h.siteConfigService.GetSiteConfig()
if err != nil {
return err
}
listId := c.Params("list") listId := c.Params("list")
list := model.EntryList{} list := model.EntryList{}
for _, l := range siteConfig.Lists { for _, l := range siteConfig.Lists {
@ -87,7 +89,7 @@ func (h *ListHandler) Handle(c *fiber.Ctx) error {
return err return err
} }
return render.RenderTemplateWithBase(c, siteConfig, "views/list", listRenderData{ return render.RenderTemplateWithBase(c, "views/list", listRenderData{
List: list, List: list,
Entries: entries, Entries: entries,
Page: pageNum, Page: pageNum,

View File

@ -26,7 +26,7 @@ func NewLoginHandler(
func (h *LoginHandler) HandleGet(c *fiber.Ctx) error { func (h *LoginHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/login", nil) return render.RenderTemplateWithBase(c, "views/login", nil)
} }
func (h *LoginHandler) HandlePost(c *fiber.Ctx) error { func (h *LoginHandler) HandlePost(c *fiber.Ctx) error {

View File

@ -5,7 +5,6 @@ import (
"encoding/xml" "encoding/xml"
"net/url" "net/url"
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"sort" "sort"
"time" "time"
@ -104,18 +103,21 @@ func RenderRSSFeed(config model.SiteConfig, entries []model.Entry) (string, erro
} }
type RSSHandler struct { type RSSHandler struct {
configRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
entrySvc *app.EntryService entrySvc *app.EntryService
} }
func NewRSSHandler(entryService *app.EntryService, configRepo repository.ConfigRepository) *RSSHandler { func NewRSSHandler(entryService *app.EntryService, siteConfigService *app.SiteConfigService) *RSSHandler {
return &RSSHandler{entrySvc: entryService, configRepo: configRepo} return &RSSHandler{entrySvc: entryService, siteConfigService: siteConfigService}
} }
func (h *RSSHandler) Handle(c *fiber.Ctx) error { func (h *RSSHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationXML) c.Set(fiber.HeaderContentType, fiber.MIMEApplicationXML)
siteConfig := getSiteConfig(h.configRepo) siteConfig, err := h.siteConfigService.GetSiteConfig()
if err != nil {
return err
}
entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, true, false) entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, true, false)
if err != nil { if err != nil {

View File

@ -1,42 +1,37 @@
package web package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app"
"owl-blogs/config"
"owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
type SiteConfigHandler struct { type SiteConfigHandler struct {
siteConfigRepo repository.ConfigRepository svc *app.SiteConfigService
} }
func NewSiteConfigHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigHandler { func NewSiteConfigHandler(svc *app.SiteConfigService) *SiteConfigHandler {
return &SiteConfigHandler{ return &SiteConfigHandler{
siteConfigRepo: siteConfigRepo, svc: svc,
} }
} }
func (h *SiteConfigHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.svc.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase(c, getSiteConfig(h.siteConfigRepo), "views/site_config", siteConfig) return render.RenderTemplateWithBase(c, "views/site_config", siteConfig)
} }
func (h *SiteConfigHandler) HandlePost(c *fiber.Ctx) error { func (h *SiteConfigHandler) HandlePost(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.svc.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -51,7 +46,7 @@ func (h *SiteConfigHandler) HandlePost(c *fiber.Ctx) error {
siteConfig.HtmlHeadExtra = c.FormValue("HtmlHeadExtra") siteConfig.HtmlHeadExtra = c.FormValue("HtmlHeadExtra")
siteConfig.FooterExtra = c.FormValue("FooterExtra") siteConfig.FooterExtra = c.FormValue("FooterExtra")
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = h.svc.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,8 +2,6 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"strconv" "strconv"
@ -12,7 +10,7 @@ import (
) )
type SiteConfigListHandler struct { type SiteConfigListHandler struct {
siteConfigRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
typeRegistry *app.EntryTypeRegistry typeRegistry *app.EntryTypeRegistry
} }
@ -22,11 +20,11 @@ type siteConfigListTemplateData struct {
} }
func NewSiteConfigListHandler( func NewSiteConfigListHandler(
siteConfigRepo repository.ConfigRepository, siteConfigService *app.SiteConfigService,
typeRegistry *app.EntryTypeRegistry, typeRegistry *app.EntryTypeRegistry,
) *SiteConfigListHandler { ) *SiteConfigListHandler {
return &SiteConfigListHandler{ return &SiteConfigListHandler{
siteConfigRepo: siteConfigRepo, siteConfigService: siteConfigService,
typeRegistry: typeRegistry, typeRegistry: typeRegistry,
} }
} }
@ -34,9 +32,7 @@ func NewSiteConfigListHandler(
func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -51,7 +47,7 @@ func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error {
} }
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, getSiteConfig(h.siteConfigRepo), "views/site_config_list", siteConfigListTemplateData{ c, "views/site_config_list", siteConfigListTemplateData{
Lists: siteConfig.Lists, Lists: siteConfig.Lists,
Types: types, Types: types,
}) })
@ -60,8 +56,7 @@ func (h *SiteConfigListHandler) HandleGet(c *fiber.Ctx) error {
func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error { func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
@ -79,7 +74,7 @@ func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error {
ListType: c.FormValue("ListType"), ListType: c.FormValue("ListType"),
}) })
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = h.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -90,8 +85,7 @@ func (h *SiteConfigListHandler) HandleCreate(c *fiber.Ctx) error {
func (h *SiteConfigListHandler) HandleDelete(c *fiber.Ctx) error { func (h *SiteConfigListHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
@ -104,7 +98,7 @@ func (h *SiteConfigListHandler) HandleDelete(c *fiber.Ctx) error {
siteConfig.Lists = append(siteConfig.Lists[:id], siteConfig.Lists[id+1:]...) siteConfig.Lists = append(siteConfig.Lists[:id], siteConfig.Lists[id+1:]...)
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = h.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,8 +1,7 @@
package web package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"strconv" "strconv"
@ -11,34 +10,31 @@ import (
) )
type SiteConfigMeHandler struct { type SiteConfigMeHandler struct {
siteConfigRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
} }
func NewSiteConfigMeHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigMeHandler { func NewSiteConfigMeHandler(siteConfigService *app.SiteConfigService) *SiteConfigMeHandler {
return &SiteConfigMeHandler{ return &SiteConfigMeHandler{
siteConfigRepo: siteConfigRepo, siteConfigService: siteConfigService,
} }
} }
func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, getSiteConfig(h.siteConfigRepo), "views/site_config_me", siteConfig.Me) c, "views/site_config_me", siteConfig.Me)
} }
func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error { func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
@ -49,7 +45,7 @@ func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
Url: c.FormValue("Url"), Url: c.FormValue("Url"),
}) })
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = h.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -60,8 +56,7 @@ func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error { func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
@ -73,7 +68,7 @@ func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
} }
siteConfig.Me = append(siteConfig.Me[:idx], siteConfig.Me[idx+1:]...) siteConfig.Me = append(siteConfig.Me[:idx], siteConfig.Me[idx+1:]...)
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = h.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,8 +1,7 @@
package web package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app"
"owl-blogs/config"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"strconv" "strconv"
@ -11,7 +10,7 @@ import (
) )
type SiteConfigMenusHandler struct { type SiteConfigMenusHandler struct {
siteConfigRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
} }
type siteConfigMenusTemplateData struct { type siteConfigMenusTemplateData struct {
@ -19,24 +18,23 @@ type siteConfigMenusTemplateData struct {
FooterMenu []model.MenuItem FooterMenu []model.MenuItem
} }
func NewSiteConfigMenusHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigMenusHandler { func NewSiteConfigMenusHandler(siteConfigService *app.SiteConfigService) *SiteConfigMenusHandler {
return &SiteConfigMenusHandler{ return &SiteConfigMenusHandler{
siteConfigRepo: siteConfigRepo, siteConfigService: siteConfigService,
} }
} }
func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error { func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
} }
return render.RenderTemplateWithBase( return render.RenderTemplateWithBase(
c, getSiteConfig(h.siteConfigRepo), "views/site_config_menus", siteConfigMenusTemplateData{ c, "views/site_config_menus", siteConfigMenusTemplateData{
HeaderMenu: siteConfig.HeaderMenu, HeaderMenu: siteConfig.HeaderMenu,
FooterMenu: siteConfig.FooterMenu, FooterMenu: siteConfig.FooterMenu,
}) })
@ -45,8 +43,7 @@ func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error {
func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error { func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
@ -65,7 +62,7 @@ func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
siteConfig.FooterMenu = append(siteConfig.FooterMenu, menuItem) siteConfig.FooterMenu = append(siteConfig.FooterMenu, menuItem)
} }
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = h.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
return err return err
} }
@ -76,8 +73,7 @@ func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error { func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{} siteConfig, err := h.siteConfigService.GetSiteConfig()
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil { if err != nil {
return err return err
@ -95,7 +91,7 @@ func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error {
siteConfig.FooterMenu = append(siteConfig.FooterMenu[:idx], siteConfig.FooterMenu[idx+1:]...) siteConfig.FooterMenu = append(siteConfig.FooterMenu[:idx], siteConfig.FooterMenu[idx+1:]...)
} }
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig) err = h.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil { if err != nil {
return err return err
} }

View File

@ -5,14 +5,13 @@ import (
"encoding/xml" "encoding/xml"
"net/url" "net/url"
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
type SiteMapHandler struct { type SiteMapHandler struct {
entryService *app.EntryService entryService *app.EntryService
configRepo repository.ConfigRepository siteConfigService *app.SiteConfigService
} }
type Sitemap struct { type Sitemap struct {
@ -25,15 +24,18 @@ type SitemapUrl struct {
Loc string `xml:"loc"` Loc string `xml:"loc"`
} }
func NewSiteMapHandler(entryService *app.EntryService, configRepo repository.ConfigRepository) *SiteMapHandler { func NewSiteMapHandler(entryService *app.EntryService, siteConfigService *app.SiteConfigService) *SiteMapHandler {
return &SiteMapHandler{entryService: entryService, configRepo: configRepo} return &SiteMapHandler{entryService: entryService, siteConfigService: siteConfigService}
} }
// Handle handles GET /sitemap.xml // Handle handles GET /sitemap.xml
func (h *SiteMapHandler) Handle(c *fiber.Ctx) error { func (h *SiteMapHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMEApplicationXML) c.Set(fiber.HeaderContentType, fiber.MIMEApplicationXML)
siteConfig := getSiteConfig(h.configRepo) siteConfig, err := h.siteConfigService.GetSiteConfig()
if err != nil {
return err
}
entries, err := h.entryService.FindAllByType(nil, true, false) entries, err := h.entryService.FindAllByType(nil, true, false)
if err != nil { if err != nil {
return err return err

View File

@ -1,23 +1,11 @@
package web package web
import ( import (
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"strconv" "strconv"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func getSiteConfig(repo repository.ConfigRepository) model.SiteConfig {
siteConfig := model.SiteConfig{}
err := repo.Get(config.SITE_CONFIG, &siteConfig)
if err != nil {
panic(err)
}
return siteConfig
}
type paginationData[T any] struct { type paginationData[T any] struct {
items []T items []T
page uint page uint