mirror of
https://github.com/jlelse/GoBlog
synced 2024-06-27 14:37:35 +00:00
Make UI plugins much more memory efficient by using io.Pipe and add methods to the post plugintype
This commit is contained in:
parent
0f14d71ad3
commit
aa9b1996a2
|
@ -25,8 +25,16 @@ type Database interface {
|
||||||
|
|
||||||
// Post
|
// Post
|
||||||
type Post interface {
|
type Post interface {
|
||||||
|
// Get the post path
|
||||||
|
GetPath() string
|
||||||
// Get a string array map with all the post's parameters
|
// Get a string array map with all the post's parameters
|
||||||
GetParameters() map[string][]string
|
GetParameters() map[string][]string
|
||||||
|
// Get the post section name
|
||||||
|
GetSection() string
|
||||||
|
// Get the published date string
|
||||||
|
GetPublished() string
|
||||||
|
// Get the updated date string
|
||||||
|
GetUpdated() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// RenderContext
|
// RenderContext
|
||||||
|
|
|
@ -131,11 +131,27 @@ func (W _go_goblog_app_app_pkgs_plugintypes_Middleware) Prio() int {
|
||||||
type _go_goblog_app_app_pkgs_plugintypes_Post struct {
|
type _go_goblog_app_app_pkgs_plugintypes_Post struct {
|
||||||
IValue interface{}
|
IValue interface{}
|
||||||
WGetParameters func() map[string][]string
|
WGetParameters func() map[string][]string
|
||||||
|
WGetPath func() string
|
||||||
|
WGetPublished func() string
|
||||||
|
WGetSection func() string
|
||||||
|
WGetUpdated func() string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (W _go_goblog_app_app_pkgs_plugintypes_Post) GetParameters() map[string][]string {
|
func (W _go_goblog_app_app_pkgs_plugintypes_Post) GetParameters() map[string][]string {
|
||||||
return W.WGetParameters()
|
return W.WGetParameters()
|
||||||
}
|
}
|
||||||
|
func (W _go_goblog_app_app_pkgs_plugintypes_Post) GetPath() string {
|
||||||
|
return W.WGetPath()
|
||||||
|
}
|
||||||
|
func (W _go_goblog_app_app_pkgs_plugintypes_Post) GetPublished() string {
|
||||||
|
return W.WGetPublished()
|
||||||
|
}
|
||||||
|
func (W _go_goblog_app_app_pkgs_plugintypes_Post) GetSection() string {
|
||||||
|
return W.WGetSection()
|
||||||
|
}
|
||||||
|
func (W _go_goblog_app_app_pkgs_plugintypes_Post) GetUpdated() string {
|
||||||
|
return W.WGetUpdated()
|
||||||
|
}
|
||||||
|
|
||||||
// _go_goblog_app_app_pkgs_plugintypes_RenderContext is an interface wrapper for RenderContext type
|
// _go_goblog_app_app_pkgs_plugintypes_RenderContext is an interface wrapper for RenderContext type
|
||||||
type _go_goblog_app_app_pkgs_plugintypes_RenderContext struct {
|
type _go_goblog_app_app_pkgs_plugintypes_RenderContext struct {
|
||||||
|
|
16
plugins.go
16
plugins.go
|
@ -78,6 +78,22 @@ func (a *goBlog) GetPost(path string) (plugintypes.Post, error) {
|
||||||
return a.getPost(path)
|
return a.getPost(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *post) GetPath() string {
|
||||||
|
return p.Path
|
||||||
|
}
|
||||||
|
|
||||||
func (p *post) GetParameters() map[string][]string {
|
func (p *post) GetParameters() map[string][]string {
|
||||||
return p.Parameters
|
return p.Parameters
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *post) GetSection() string {
|
||||||
|
return p.Section
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *post) GetPublished() string {
|
||||||
|
return p.Published
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *post) GetUpdated() string {
|
||||||
|
return p.Updated
|
||||||
|
}
|
||||||
|
|
44
render.go
44
render.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"go.goblog.app/app/pkgs/bufferpool"
|
|
||||||
"go.goblog.app/app/pkgs/contenttype"
|
"go.goblog.app/app/pkgs/contenttype"
|
||||||
"go.goblog.app/app/pkgs/htmlbuilder"
|
"go.goblog.app/app/pkgs/htmlbuilder"
|
||||||
"go.goblog.app/app/pkgs/plugintypes"
|
"go.goblog.app/app/pkgs/plugintypes"
|
||||||
|
@ -41,26 +40,35 @@ func (a *goBlog) renderWithStatusCode(w http.ResponseWriter, r *http.Request, st
|
||||||
// Write status code
|
// Write status code
|
||||||
w.WriteHeader(statusCode)
|
w.WriteHeader(statusCode)
|
||||||
// Render
|
// Render
|
||||||
buf := bufferpool.Get()
|
renderPipeReader, renderPipeWriter := io.Pipe()
|
||||||
defer bufferpool.Put(buf)
|
go func() {
|
||||||
f(htmlbuilder.NewHtmlBuilder(buf), data)
|
f(htmlbuilder.NewHtmlBuilder(renderPipeWriter), data)
|
||||||
// Check if UI plugins are registered
|
renderPipeWriter.Close()
|
||||||
uiPlugins := a.getPlugins(pluginUiType)
|
}()
|
||||||
if len(uiPlugins) > 0 {
|
// Run UI plugins
|
||||||
pluginBuf := bufferpool.Get()
|
pluginPipeReader, pluginPipeWriter := io.Pipe()
|
||||||
defer bufferpool.Put(pluginBuf)
|
go func() {
|
||||||
for _, plug := range uiPlugins {
|
a.chainUiPlugins(a.getPlugins(pluginUiType), &pluginRenderContext{
|
||||||
pluginBuf.Reset()
|
|
||||||
plug.(plugintypes.UI).Render(&pluginRenderContext{
|
|
||||||
blog: data.BlogString,
|
blog: data.BlogString,
|
||||||
path: r.URL.Path,
|
path: r.URL.Path,
|
||||||
}, buf, pluginBuf)
|
}, renderPipeReader, pluginPipeWriter)
|
||||||
buf.Reset()
|
pluginPipeWriter.Close()
|
||||||
_, _ = io.Copy(buf, pluginBuf)
|
}()
|
||||||
}
|
|
||||||
}
|
|
||||||
// Return minified HTML
|
// Return minified HTML
|
||||||
_ = a.min.Get().Minify(contenttype.HTML, w, buf)
|
_ = a.min.Get().Minify(contenttype.HTML, w, pluginPipeReader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *goBlog) chainUiPlugins(plugins []any, rc *pluginRenderContext, rendered io.Reader, modified io.Writer) {
|
||||||
|
if len(plugins) == 0 {
|
||||||
|
_, _ = io.Copy(modified, rendered)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reader, writer := io.Pipe()
|
||||||
|
go func() {
|
||||||
|
plugins[0].(plugintypes.UI).Render(rc, rendered, writer)
|
||||||
|
_ = writer.Close()
|
||||||
|
}()
|
||||||
|
a.chainUiPlugins(plugins[1:], rc, reader, modified)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *goBlog) checkRenderData(r *http.Request, data *renderData) {
|
func (a *goBlog) checkRenderData(r *http.Request, data *renderData) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user