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"
"fmt"
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"strings"
@ -12,12 +11,12 @@ import (
)
type AuthorService struct {
repo repository.AuthorRepository
siteConfigRepo repository.ConfigRepository
repo repository.AuthorRepository
siteConfigService *SiteConfigService
}
func NewAuthorService(repo repository.AuthorRepository, siteConfigRepo repository.ConfigRepository) *AuthorService {
return &AuthorService{repo: repo, siteConfigRepo: siteConfigRepo}
func NewAuthorService(repo repository.AuthorRepository, siteConfigService *SiteConfigService) *AuthorService {
return &AuthorService{repo: repo, siteConfigService: siteConfigService}
}
func hashPassword(password string) (string, error) {
@ -65,14 +64,13 @@ func (s *AuthorService) Authenticate(name string, password string) bool {
}
func (s *AuthorService) getSecretKey() string {
siteConfig := model.SiteConfig{}
err := s.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
siteConfig, err := s.siteConfigService.GetSiteConfig()
if err != nil {
panic(err)
}
if siteConfig.Secret == "" {
siteConfig.Secret = RandStringRunes(64)
err = s.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
err = s.siteConfigService.UpdateSiteConfig(siteConfig)
if err != nil {
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"
"owl-blogs/app/owlhttp"
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"owl-blogs/interactions"
"time"
)
type WebmentionService struct {
ConfigRepo repository.ConfigRepository
siteConfigService *SiteConfigService
InteractionRepository repository.InteractionRepository
EntryRepository repository.EntryRepository
Http owlhttp.HttpClient
}
func NewWebmentionService(
configRepo repository.ConfigRepository,
siteConfigService *SiteConfigService,
interactionRepository repository.InteractionRepository,
entryRepository repository.EntryRepository,
http owlhttp.HttpClient,
bus *EventBus,
) *WebmentionService {
svc := &WebmentionService{
ConfigRepo: configRepo,
siteConfigService: siteConfigService,
InteractionRepository: interactionRepository,
EntryRepository: entryRepository,
Http: http,
@ -104,8 +103,7 @@ func (s *WebmentionService) ScanForLinks(entry model.Entry) ([]string, error) {
}
func (s *WebmentionService) FullEntryUrl(entry model.Entry) string {
siteConfig := model.SiteConfig{}
s.ConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
siteConfig, _ := s.siteConfigService.GetSiteConfig()
url, _ := url.JoinPath(
siteConfig.FullUrl,

View File

@ -8,6 +8,7 @@ import (
"owl-blogs/infra"
"owl-blogs/interactions"
"owl-blogs/plugings"
"owl-blogs/render"
"owl-blogs/web"
"github.com/spf13/cobra"
@ -57,13 +58,17 @@ func App(db infra.Database) *web.WebApp {
eventBus := app.NewEventBus()
// Create Services
siteConfigService := app.NewSiteConfigService(configRepo)
entryService := app.NewEntryService(entryRepo, eventBus)
binaryService := app.NewBinaryFileService(binRepo)
authorService := app.NewAuthorService(authorRepo, configRepo)
authorService := app.NewAuthorService(authorRepo, siteConfigService)
webmentionService := app.NewWebmentionService(
configRepo, interactionRepo, entryRepo, httpClient, eventBus,
siteConfigService, interactionRepo, entryRepo, httpClient, eventBus,
)
// setup render functions
render.SiteConfigService = siteConfigService
// plugins
plugings.NewEcho(eventBus)
plugings.RegisterInstagram(
@ -74,7 +79,7 @@ func App(db infra.Database) *web.WebApp {
return web.NewWebApp(
entryService, entryRegister, binaryService,
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
var templates embed.FS
var SiteConfigService model.SiteConfigInterface
var funcMap = template.FuncMap{
"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)
@ -53,6 +54,11 @@ func RenderTemplateWithBase(w io.Writer, siteConfig model.SiteConfig, templateNa
return err
}
siteConfig, err := SiteConfigService.GetSiteConfig()
if err != nil {
return err
}
err = t.ExecuteTemplate(w, "base", TemplateData{
Data: data,
SiteConfig: siteConfig,

View File

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

View File

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

View File

@ -20,8 +20,6 @@ func NewAdminInteractionHandler(configRepo repository.ConfigRepository, interact
}
func (h *AdminInteractionHandler) HandleGet(c *fiber.Ctx) error {
siteConfig := getSiteConfig(h.configRepo)
filter := c.Query("filter", "")
interactions, err := h.interactionRepo.ListAllInteractions()
@ -31,7 +29,7 @@ func (h *AdminInteractionHandler) HandleGet(c *fiber.Ctx) error {
pageData := paginate(c, interactions, 50)
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,
"Page": pageData.page,
"NextPage": pageData.page + 1,

View File

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

View File

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

View File

@ -48,7 +48,7 @@ func (h *EditorHandler) HandleGetNew(c *fiber.Ctx) error {
return err
}
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 {
@ -94,7 +94,7 @@ func (h *EditorHandler) HandleGetEdit(c *fiber.Ctx) error {
}
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 {

View File

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

View File

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

View File

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

View File

@ -26,7 +26,7 @@ func NewLoginHandler(
func (h *LoginHandler) HandleGet(c *fiber.Ctx) error {
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 {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -5,14 +5,13 @@ import (
"encoding/xml"
"net/url"
"owl-blogs/app"
"owl-blogs/app/repository"
"github.com/gofiber/fiber/v2"
)
type SiteMapHandler struct {
entryService *app.EntryService
configRepo repository.ConfigRepository
entryService *app.EntryService
siteConfigService *app.SiteConfigService
}
type Sitemap struct {
@ -25,15 +24,18 @@ type SitemapUrl struct {
Loc string `xml:"loc"`
}
func NewSiteMapHandler(entryService *app.EntryService, configRepo repository.ConfigRepository) *SiteMapHandler {
return &SiteMapHandler{entryService: entryService, configRepo: configRepo}
func NewSiteMapHandler(entryService *app.EntryService, siteConfigService *app.SiteConfigService) *SiteMapHandler {
return &SiteMapHandler{entryService: entryService, siteConfigService: siteConfigService}
}
// Handle handles GET /sitemap.xml
func (h *SiteMapHandler) Handle(c *fiber.Ctx) error {
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)
if err != nil {
return err

View File

@ -1,23 +1,11 @@
package web
import (
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
"strconv"
"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 {
items []T
page uint