basic site config editing

This commit is contained in:
Niko Abeler 2023-07-17 20:44:59 +02:00
parent cecd94b296
commit e91128fd7e
12 changed files with 179 additions and 43 deletions

View File

@ -42,7 +42,7 @@ func App(db infra.Database) *web.WebApp {
binaryService := app.NewBinaryFileService(binRepo)
authorService := app.NewAuthorService(authorRepo, siteConfigRepo)
return web.NewWebApp(entryService, registry, binaryService, authorService)
return web.NewWebApp(entryService, registry, binaryService, authorService, siteConfigRepo)
}

View File

@ -40,7 +40,7 @@ func CreateTemplateWithBase(templateName string) (*template.Template, error) {
)
}
func RenderTemplateWithBase(w io.Writer, templateName string, data interface{}) error {
func RenderTemplateWithBase(w io.Writer, siteConfig model.SiteConfig, templateName string, data interface{}) error {
t, err := CreateTemplateWithBase(templateName)
@ -50,7 +50,7 @@ func RenderTemplateWithBase(w io.Writer, templateName string, data interface{})
err = t.ExecuteTemplate(w, "base", TemplateData{
Data: data,
SiteConfig: model.SiteConfig{},
SiteConfig: siteConfig,
})
return err

View File

@ -22,7 +22,7 @@
.avatar {
float: left;
margin-right: 1rem;
border-radius: 50%;
}
.header {

View File

@ -0,0 +1,25 @@
{{define "title"}}Editor{{end}}
{{define "main"}}
<form method="post" enctyep="multipart/form-data">
<label for="Title">Title</label>
<input type="text" name="Title" id="Title" value="{{.Title}}"/>
<label for="SubTitle">SubTitle</label>
<input type="text" name="SubTitle" id="SubTitle" value="{{.SubTitle}}"/>
<label for="HeaderColor">HeaderColor</label>
<input type="color" name="HeaderColor" id="HeaderColor" value="{{.HeaderColor}}"/>
<label for="AuthorName">AuthorName</label>
<input type="text" name="AuthorName" id="AuthorName" value="{{.AuthorName}}"/>
<label for="AvatarUrl">AvatarUrl</label>
<input type="text" name="AvatarUrl" id="AvatarUrl" value="{{.AvatarUrl}}"/>
<input type="submit" value="Save" />
</form>
{{end}}

View File

@ -4,6 +4,7 @@ import (
"embed"
"net/http"
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/web/middleware"
"github.com/gofiber/fiber/v2"
@ -14,11 +15,12 @@ import (
var embedDirStatic embed.FS
type WebApp struct {
FiberApp *fiber.App
EntryService *app.EntryService
BinaryService *app.BinaryService
Registry *app.EntryTypeRegistry
AuthorService *app.AuthorService
FiberApp *fiber.App
EntryService *app.EntryService
BinaryService *app.BinaryService
Registry *app.EntryTypeRegistry
AuthorService *app.AuthorService
SiteConfigRepo repository.SiteConfigRepository
}
func NewWebApp(
@ -26,17 +28,18 @@ func NewWebApp(
typeRegistry *app.EntryTypeRegistry,
binService *app.BinaryService,
authorService *app.AuthorService,
siteConfigRepo repository.SiteConfigRepository,
) *WebApp {
app := fiber.New()
indexHandler := NewIndexHandler(entryService)
indexHandler := NewIndexHandler(entryService, siteConfigRepo)
listHandler := NewListHandler(entryService)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, siteConfigRepo)
mediaHandler := NewMediaHandler(binService)
rssHandler := NewRSSHandler(entryService)
loginHandler := NewLoginHandler(authorService)
editorListHandler := NewEditorListHandler(typeRegistry)
editorHandler := NewEditorHandler(entryService, typeRegistry, binService)
loginHandler := NewLoginHandler(authorService, siteConfigRepo)
editorListHandler := NewEditorListHandler(typeRegistry, siteConfigRepo)
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, siteConfigRepo)
// Login
app.Get("/auth/login", loginHandler.HandleGet)
@ -49,6 +52,12 @@ func NewWebApp(
editor.Get("/:editor/", editorHandler.HandleGet)
editor.Post("/:editor/", editorHandler.HandlePost)
// SiteConfig
siteConfig := app.Group("/site-config")
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
siteConfig.Get("/", NewSiteConfigHandler(siteConfigRepo).HandleGet)
siteConfig.Post("/", NewSiteConfigHandler(siteConfigRepo).HandlePost)
// app.Static("/static/*filepath", http.Dir(repo.StaticDir()))
app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(embedDirStatic),
@ -75,11 +84,12 @@ func NewWebApp(
// app.Get("/.well-known/oauth-authorization-server", userAuthMetadataHandler(repo))
// app.NotFound = http.HandlerFunc(notFoundHandler(repo))
return &WebApp{
FiberApp: app,
EntryService: entryService,
Registry: typeRegistry,
BinaryService: binService,
AuthorService: authorService,
FiberApp: app,
EntryService: entryService,
Registry: typeRegistry,
BinaryService: binService,
AuthorService: authorService,
SiteConfigRepo: siteConfigRepo,
}
}

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"owl-blogs/render"
"owl-blogs/web/editor"
@ -11,20 +12,23 @@ import (
)
type EditorHandler struct {
entrySvc *app.EntryService
binSvc *app.BinaryService
registry *app.EntryTypeRegistry
configRepo repository.SiteConfigRepository
entrySvc *app.EntryService
binSvc *app.BinaryService
registry *app.EntryTypeRegistry
}
func NewEditorHandler(
entryService *app.EntryService,
registry *app.EntryTypeRegistry,
binService *app.BinaryService,
configRepo repository.SiteConfigRepository,
) *EditorHandler {
return &EditorHandler{
entrySvc: entryService,
registry: registry,
binSvc: binService,
entrySvc: entryService,
registry: registry,
binSvc: binService,
configRepo: configRepo,
}
}
@ -50,7 +54,7 @@ func (h *EditorHandler) HandleGet(c *fiber.Ctx) error {
if err != nil {
return err
}
return render.RenderTemplateWithBase(c, "views/editor", htmlForm)
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/editor", htmlForm)
}
func (h *EditorHandler) HandlePost(c *fiber.Ctx) error {

View File

@ -2,22 +2,26 @@ package web
import (
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/render"
"github.com/gofiber/fiber/v2"
)
type EditorListHandler struct {
registry *app.EntryTypeRegistry
configRepo repository.SiteConfigRepository
registry *app.EntryTypeRegistry
}
type EditorListContext struct {
Types []string
}
func NewEditorListHandler(registry *app.EntryTypeRegistry) *EditorListHandler {
func NewEditorListHandler(registry *app.EntryTypeRegistry,
configRepo repository.SiteConfigRepository) *EditorListHandler {
return &EditorListHandler{
registry: registry,
registry: registry,
configRepo: configRepo,
}
}
@ -33,5 +37,5 @@ func (h *EditorListHandler) Handle(c *fiber.Ctx) error {
typeNames = append(typeNames, name)
}
return render.RenderTemplateWithBase(c, "views/editor_list", &EditorListContext{Types: typeNames})
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/editor_list", &EditorListContext{Types: typeNames})
}

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"owl-blogs/render"
@ -9,9 +10,10 @@ import (
)
type EntryHandler struct {
entrySvc *app.EntryService
authorSvc *app.AuthorService
registry *app.EntryTypeRegistry
configRepo repository.SiteConfigRepository
entrySvc *app.EntryService
authorSvc *app.AuthorService
registry *app.EntryTypeRegistry
}
type entryData struct {
@ -19,8 +21,18 @@ type entryData struct {
Author *model.Author
}
func NewEntryHandler(entryService *app.EntryService, registry *app.EntryTypeRegistry, authorService *app.AuthorService) *EntryHandler {
return &EntryHandler{entrySvc: entryService, authorSvc: authorService, registry: registry}
func NewEntryHandler(
entryService *app.EntryService,
registry *app.EntryTypeRegistry,
authorService *app.AuthorService,
configRepo repository.SiteConfigRepository,
) *EntryHandler {
return &EntryHandler{
entrySvc: entryService,
authorSvc: authorService,
registry: registry,
configRepo: configRepo,
}
}
func (h *EntryHandler) Handle(c *fiber.Ctx) error {
@ -37,5 +49,5 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
author = &model.Author{}
}
return render.RenderTemplateWithBase(c, "views/entry", entryData{Entry: entry, Author: author})
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/entry", entryData{Entry: entry, Author: author})
}

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"owl-blogs/render"
"sort"
@ -11,11 +12,18 @@ import (
)
type IndexHandler struct {
entrySvc *app.EntryService
configRepo repository.SiteConfigRepository
entrySvc *app.EntryService
}
func NewIndexHandler(entryService *app.EntryService) *IndexHandler {
return &IndexHandler{entrySvc: entryService}
func NewIndexHandler(
entryService *app.EntryService,
configRepo repository.SiteConfigRepository,
) *IndexHandler {
return &IndexHandler{
entrySvc: entryService,
configRepo: configRepo,
}
}
type indexRenderData struct {
@ -65,7 +73,7 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return err
}
return render.RenderTemplateWithBase(c, "views/index", indexRenderData{
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/index", indexRenderData{
Entries: entries,
Page: pageNum,
NextPage: pageNum + 1,

View File

@ -2,6 +2,7 @@ package web
import (
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/render"
"time"
@ -9,16 +10,23 @@ import (
)
type LoginHandler struct {
configRepo repository.SiteConfigRepository
authorService *app.AuthorService
}
func NewLoginHandler(authorService *app.AuthorService) *LoginHandler {
return &LoginHandler{authorService: authorService}
func NewLoginHandler(
authorService *app.AuthorService,
configRepo repository.SiteConfigRepository,
) *LoginHandler {
return &LoginHandler{
authorService: authorService,
configRepo: configRepo,
}
}
func (h *LoginHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
return render.RenderTemplateWithBase(c, "views/login", nil)
return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/login", nil)
}
func (h *LoginHandler) HandlePost(c *fiber.Ctx) error {

51
web/siteconfig_handler.go Normal file
View File

@ -0,0 +1,51 @@
package web
import (
"owl-blogs/app/repository"
"owl-blogs/render"
"github.com/gofiber/fiber/v2"
)
type SiteConfigHandler struct {
siteConfigRepo repository.SiteConfigRepository
}
func NewSiteConfigHandler(siteConfigRepo repository.SiteConfigRepository) *SiteConfigHandler {
return &SiteConfigHandler{
siteConfigRepo: siteConfigRepo,
}
}
func (h *SiteConfigHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get()
if err != nil {
return err
}
return render.RenderTemplateWithBase(c, getConfig(h.siteConfigRepo), "views/site_config", config)
}
func (h *SiteConfigHandler) HandlePost(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
config, err := h.siteConfigRepo.Get()
if err != nil {
return err
}
config.Title = c.FormValue("Title")
config.SubTitle = c.FormValue("SubTitle")
config.HeaderColor = c.FormValue("HeaderColor")
config.AuthorName = c.FormValue("AuthorName")
config.AvatarUrl = c.FormValue("AvatarUrl")
err = h.siteConfigRepo.Update(config)
if err != nil {
return err
}
return c.Redirect("/site-config/")
}

14
web/utils.go Normal file
View File

@ -0,0 +1,14 @@
package web
import (
"owl-blogs/app/repository"
"owl-blogs/domain/model"
)
func getConfig(repo repository.SiteConfigRepository) model.SiteConfig {
config, err := repo.Get()
if err != nil {
panic(err)
}
return config
}