v2 #43
|
@ -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)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
.avatar {
|
.avatar {
|
||||||
float: left;
|
float: left;
|
||||||
margin-right: 1rem;
|
margin-right: 1rem;
|
||||||
|
border-radius: 50%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.header {
|
.header {
|
||||||
|
|
|
@ -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}}
|
40
web/app.go
40
web/app.go
|
@ -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"
|
||||||
|
@ -14,11 +15,12 @@ import (
|
||||||
var embedDirStatic embed.FS
|
var embedDirStatic embed.FS
|
||||||
|
|
||||||
type WebApp struct {
|
type WebApp struct {
|
||||||
FiberApp *fiber.App
|
FiberApp *fiber.App
|
||||||
EntryService *app.EntryService
|
EntryService *app.EntryService
|
||||||
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),
|
||||||
|
@ -75,11 +84,12 @@ func NewWebApp(
|
||||||
// app.Get("/.well-known/oauth-authorization-server", userAuthMetadataHandler(repo))
|
// app.Get("/.well-known/oauth-authorization-server", userAuthMetadataHandler(repo))
|
||||||
// app.NotFound = http.HandlerFunc(notFoundHandler(repo))
|
// app.NotFound = http.HandlerFunc(notFoundHandler(repo))
|
||||||
return &WebApp{
|
return &WebApp{
|
||||||
FiberApp: app,
|
FiberApp: app,
|
||||||
EntryService: entryService,
|
EntryService: entryService,
|
||||||
Registry: typeRegistry,
|
Registry: typeRegistry,
|
||||||
BinaryService: binService,
|
BinaryService: binService,
|
||||||
AuthorService: authorService,
|
AuthorService: authorService,
|
||||||
|
SiteConfigRepo: siteConfigRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,20 +12,23 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type EditorHandler struct {
|
type EditorHandler struct {
|
||||||
entrySvc *app.EntryService
|
configRepo repository.SiteConfigRepository
|
||||||
binSvc *app.BinaryService
|
entrySvc *app.EntryService
|
||||||
registry *app.EntryTypeRegistry
|
binSvc *app.BinaryService
|
||||||
|
registry *app.EntryTypeRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEditorHandler(
|
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 {
|
||||||
|
|
|
@ -2,22 +2,26 @@ 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 {
|
||||||
registry *app.EntryTypeRegistry
|
configRepo repository.SiteConfigRepository
|
||||||
|
registry *app.EntryTypeRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
type EditorListContext struct {
|
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})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,9 +10,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type EntryHandler struct {
|
type EntryHandler struct {
|
||||||
entrySvc *app.EntryService
|
configRepo repository.SiteConfigRepository
|
||||||
authorSvc *app.AuthorService
|
entrySvc *app.EntryService
|
||||||
registry *app.EntryTypeRegistry
|
authorSvc *app.AuthorService
|
||||||
|
registry *app.EntryTypeRegistry
|
||||||
}
|
}
|
||||||
|
|
||||||
type entryData struct {
|
type entryData struct {
|
||||||
|
@ -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})
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 {
|
||||||
entrySvc *app.EntryService
|
configRepo repository.SiteConfigRepository
|
||||||
|
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,
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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/")
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue