v2 #43

Merged
h4kor merged 41 commits from v2 into main 2023-07-19 19:09:19 +00:00
12 changed files with 179 additions and 43 deletions
Showing only changes of commit e91128fd7e - Show all commits

View File

@ -42,7 +42,7 @@ func App(db infra.Database) *web.WebApp {
binaryService := app.NewBinaryFileService(binRepo) binaryService := app.NewBinaryFileService(binRepo)
authorService := app.NewAuthorService(authorRepo, siteConfigRepo) 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) t, err := CreateTemplateWithBase(templateName)
@ -50,7 +50,7 @@ func RenderTemplateWithBase(w io.Writer, templateName string, data interface{})
err = t.ExecuteTemplate(w, "base", TemplateData{ err = t.ExecuteTemplate(w, "base", TemplateData{
Data: data, Data: data,
SiteConfig: model.SiteConfig{}, SiteConfig: siteConfig,
}) })
return err return err

View File

@ -22,7 +22,7 @@
.avatar { .avatar {
float: left; float: left;
margin-right: 1rem; margin-right: 1rem;
border-radius: 50%;
} }
.header { .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" "embed"
"net/http" "net/http"
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/web/middleware" "owl-blogs/web/middleware"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -19,6 +20,7 @@ type WebApp struct {
BinaryService *app.BinaryService BinaryService *app.BinaryService
Registry *app.EntryTypeRegistry Registry *app.EntryTypeRegistry
AuthorService *app.AuthorService AuthorService *app.AuthorService
SiteConfigRepo repository.SiteConfigRepository
} }
func NewWebApp( func NewWebApp(
@ -26,17 +28,18 @@ func NewWebApp(
typeRegistry *app.EntryTypeRegistry, typeRegistry *app.EntryTypeRegistry,
binService *app.BinaryService, binService *app.BinaryService,
authorService *app.AuthorService, authorService *app.AuthorService,
siteConfigRepo repository.SiteConfigRepository,
) *WebApp { ) *WebApp {
app := fiber.New() app := fiber.New()
indexHandler := NewIndexHandler(entryService) indexHandler := NewIndexHandler(entryService, siteConfigRepo)
listHandler := NewListHandler(entryService) listHandler := NewListHandler(entryService)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService) entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, siteConfigRepo)
mediaHandler := NewMediaHandler(binService) mediaHandler := NewMediaHandler(binService)
rssHandler := NewRSSHandler(entryService) rssHandler := NewRSSHandler(entryService)
loginHandler := NewLoginHandler(authorService) loginHandler := NewLoginHandler(authorService, siteConfigRepo)
editorListHandler := NewEditorListHandler(typeRegistry) editorListHandler := NewEditorListHandler(typeRegistry, siteConfigRepo)
editorHandler := NewEditorHandler(entryService, typeRegistry, binService) editorHandler := NewEditorHandler(entryService, typeRegistry, binService, siteConfigRepo)
// Login // Login
app.Get("/auth/login", loginHandler.HandleGet) app.Get("/auth/login", loginHandler.HandleGet)
@ -49,6 +52,12 @@ func NewWebApp(
editor.Get("/:editor/", editorHandler.HandleGet) editor.Get("/:editor/", editorHandler.HandleGet)
editor.Post("/:editor/", editorHandler.HandlePost) 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.Static("/static/*filepath", http.Dir(repo.StaticDir()))
app.Use("/static", filesystem.New(filesystem.Config{ app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(embedDirStatic), Root: http.FS(embedDirStatic),
@ -80,6 +89,7 @@ func NewWebApp(
Registry: typeRegistry, Registry: typeRegistry,
BinaryService: binService, BinaryService: binService,
AuthorService: authorService, AuthorService: authorService,
SiteConfigRepo: siteConfigRepo,
} }
} }

View File

@ -2,6 +2,7 @@ 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"
"owl-blogs/web/editor" "owl-blogs/web/editor"
@ -11,6 +12,7 @@ import (
) )
type EditorHandler struct { type EditorHandler struct {
configRepo repository.SiteConfigRepository
entrySvc *app.EntryService entrySvc *app.EntryService
binSvc *app.BinaryService binSvc *app.BinaryService
registry *app.EntryTypeRegistry registry *app.EntryTypeRegistry
@ -20,11 +22,13 @@ func NewEditorHandler(
entryService *app.EntryService, entryService *app.EntryService,
registry *app.EntryTypeRegistry, registry *app.EntryTypeRegistry,
binService *app.BinaryService, binService *app.BinaryService,
configRepo repository.SiteConfigRepository,
) *EditorHandler { ) *EditorHandler {
return &EditorHandler{ return &EditorHandler{
entrySvc: entryService, entrySvc: entryService,
registry: registry, registry: registry,
binSvc: binService, binSvc: binService,
configRepo: configRepo,
} }
} }
@ -50,7 +54,7 @@ func (h *EditorHandler) HandleGet(c *fiber.Ctx) error {
if err != nil { if err != nil {
return err 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 { func (h *EditorHandler) HandlePost(c *fiber.Ctx) error {

View File

@ -2,12 +2,14 @@ package web
import ( import (
"owl-blogs/app" "owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/render" "owl-blogs/render"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
type EditorListHandler struct { type EditorListHandler struct {
configRepo repository.SiteConfigRepository
registry *app.EntryTypeRegistry registry *app.EntryTypeRegistry
} }
@ -15,9 +17,11 @@ type EditorListContext struct {
Types []string Types []string
} }
func NewEditorListHandler(registry *app.EntryTypeRegistry) *EditorListHandler { func NewEditorListHandler(registry *app.EntryTypeRegistry,
configRepo repository.SiteConfigRepository) *EditorListHandler {
return &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) 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 ( 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"
@ -9,6 +10,7 @@ import (
) )
type EntryHandler struct { type EntryHandler struct {
configRepo repository.SiteConfigRepository
entrySvc *app.EntryService entrySvc *app.EntryService
authorSvc *app.AuthorService authorSvc *app.AuthorService
registry *app.EntryTypeRegistry registry *app.EntryTypeRegistry
@ -19,8 +21,18 @@ type entryData struct {
Author *model.Author Author *model.Author
} }
func NewEntryHandler(entryService *app.EntryService, registry *app.EntryTypeRegistry, authorService *app.AuthorService) *EntryHandler { func NewEntryHandler(
return &EntryHandler{entrySvc: entryService, authorSvc: authorService, registry: registry} 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 { func (h *EntryHandler) Handle(c *fiber.Ctx) error {
@ -37,5 +49,5 @@ func (h *EntryHandler) Handle(c *fiber.Ctx) error {
author = &model.Author{} 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 ( 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"
@ -11,11 +12,18 @@ import (
) )
type IndexHandler struct { type IndexHandler struct {
configRepo repository.SiteConfigRepository
entrySvc *app.EntryService entrySvc *app.EntryService
} }
func NewIndexHandler(entryService *app.EntryService) *IndexHandler { func NewIndexHandler(
return &IndexHandler{entrySvc: entryService} entryService *app.EntryService,
configRepo repository.SiteConfigRepository,
) *IndexHandler {
return &IndexHandler{
entrySvc: entryService,
configRepo: configRepo,
}
} }
type indexRenderData struct { type indexRenderData struct {
@ -65,7 +73,7 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return err return err
} }
return render.RenderTemplateWithBase(c, "views/index", indexRenderData{ return render.RenderTemplateWithBase(c, getConfig(h.configRepo), "views/index", indexRenderData{
Entries: entries, Entries: entries,
Page: pageNum, Page: pageNum,
NextPage: pageNum + 1, NextPage: pageNum + 1,

View File

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