2023-01-23 19:30:47 +00:00
|
|
|
package demo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
"go.goblog.app/app/pkgs/bufferpool"
|
|
|
|
"go.goblog.app/app/pkgs/htmlbuilder"
|
|
|
|
"go.goblog.app/app/pkgs/plugintypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type plugin struct {
|
|
|
|
app plugintypes.App
|
|
|
|
config map[string]any
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPlugin() (
|
|
|
|
plugintypes.SetApp, plugintypes.SetConfig,
|
|
|
|
plugintypes.UI,
|
2023-02-13 19:22:42 +00:00
|
|
|
plugintypes.UI2,
|
2023-01-23 19:30:47 +00:00
|
|
|
plugintypes.Exec,
|
|
|
|
plugintypes.Middleware,
|
|
|
|
) {
|
|
|
|
p := &plugin{}
|
2023-02-13 19:22:42 +00:00
|
|
|
return p, p, p, p, p, p
|
2023-01-23 19:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetApp
|
|
|
|
func (p *plugin) SetApp(app plugintypes.App) {
|
|
|
|
p.app = app
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig
|
|
|
|
func (p *plugin) SetConfig(config map[string]any) {
|
|
|
|
p.config = config
|
|
|
|
}
|
|
|
|
|
|
|
|
// UI
|
|
|
|
func (*plugin) Render(_ plugintypes.RenderContext, rendered io.Reader, modified io.Writer) {
|
|
|
|
doc, err := goquery.NewDocumentFromReader(rendered)
|
|
|
|
if err != nil {
|
2023-02-13 19:22:42 +00:00
|
|
|
fmt.Println("demo plugin: " + err.Error())
|
2023-01-23 19:30:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
buf := bufferpool.Get()
|
|
|
|
defer bufferpool.Put(buf)
|
|
|
|
hb := htmlbuilder.NewHtmlBuilder(buf)
|
|
|
|
hb.WriteElementOpen("p")
|
|
|
|
hb.WriteEscaped("End of post content")
|
|
|
|
hb.WriteElementClose("p")
|
|
|
|
doc.Find("main.h-entry article div.e-content").AppendHtml(buf.String())
|
|
|
|
_ = goquery.Render(modified, doc.Selection)
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:22:42 +00:00
|
|
|
// UI
|
|
|
|
func (p *plugin) RenderWithDocument(rc plugintypes.RenderContext, doc *goquery.Document) {
|
|
|
|
buf := bufferpool.Get()
|
|
|
|
defer bufferpool.Put(buf)
|
|
|
|
hb := htmlbuilder.NewHtmlBuilder(buf)
|
|
|
|
hb.WriteElementOpen("p")
|
|
|
|
hb.WriteEscaped("Second end of post content")
|
|
|
|
hb.WriteElementClose("p")
|
|
|
|
doc.Find("main.h-entry article div.e-content").AppendHtml(buf.String())
|
|
|
|
}
|
|
|
|
|
2023-01-23 19:30:47 +00:00
|
|
|
// Exec
|
|
|
|
func (p *plugin) Exec() {
|
|
|
|
fmt.Println("Hello World from the demo plugin!")
|
|
|
|
|
|
|
|
row, _ := p.app.GetDatabase().QueryRow("select count (*) from posts")
|
|
|
|
var count int
|
|
|
|
if err := row.Scan(&count); err != nil {
|
|
|
|
fmt.Println(fmt.Errorf("failed to count posts: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Number of posts in database: %d", count)
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
func (p *plugin) Prio() int {
|
|
|
|
if prioAny, ok := p.config["prio"]; ok {
|
|
|
|
if prio, ok := prioAny.(int); ok {
|
|
|
|
return prio
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
func (p *plugin) Handler(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-Demo", fmt.Sprintf("This is from the demo middleware with prio %d", p.Prio()))
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|