2020-07-31 13:48:01 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-02-11 23:48:59 +00:00
|
|
|
"io"
|
2020-07-31 17:46:12 +00:00
|
|
|
"net/http"
|
2020-10-06 17:07:48 +00:00
|
|
|
|
2023-02-13 19:22:42 +00:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2021-06-28 20:17:18 +00:00
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
2022-08-12 10:48:16 +00:00
|
|
|
"go.goblog.app/app/pkgs/htmlbuilder"
|
2023-01-23 19:30:47 +00:00
|
|
|
"go.goblog.app/app/pkgs/plugintypes"
|
2020-07-31 13:48:01 +00:00
|
|
|
)
|
|
|
|
|
2020-10-12 16:47:23 +00:00
|
|
|
type renderData struct {
|
2021-04-23 17:36:57 +00:00
|
|
|
BlogString string
|
|
|
|
Canonical string
|
2021-05-09 12:42:53 +00:00
|
|
|
TorAddress string
|
2021-04-23 17:36:57 +00:00
|
|
|
Blog *configBlog
|
2021-06-18 12:32:03 +00:00
|
|
|
User *configUser
|
2022-03-16 07:28:03 +00:00
|
|
|
Data any
|
2021-04-23 17:36:57 +00:00
|
|
|
WebmentionReceivingEnabled bool
|
2021-05-09 12:42:53 +00:00
|
|
|
TorUsed bool
|
2021-07-23 10:59:24 +00:00
|
|
|
EasterEgg bool
|
2023-03-10 14:14:50 +00:00
|
|
|
// For plugins
|
|
|
|
prc *pluginRenderContext
|
2021-07-25 08:53:12 +00:00
|
|
|
// Not directly accessible
|
|
|
|
app *goBlog
|
|
|
|
req *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *renderData) LoggedIn() bool {
|
|
|
|
return d.app.isLoggedIn(d.req)
|
2020-10-12 16:47:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 10:48:16 +00:00
|
|
|
func (a *goBlog) render(w http.ResponseWriter, r *http.Request, f func(*htmlbuilder.HtmlBuilder, *renderData), data *renderData) {
|
2022-01-30 15:40:53 +00:00
|
|
|
a.renderWithStatusCode(w, r, http.StatusOK, f, data)
|
2022-01-20 17:22:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-12 10:48:16 +00:00
|
|
|
func (a *goBlog) renderWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, f func(*htmlbuilder.HtmlBuilder, *renderData), data *renderData) {
|
2022-01-20 17:22:10 +00:00
|
|
|
// Check render data
|
|
|
|
a.checkRenderData(r, data)
|
|
|
|
// Set content type
|
|
|
|
w.Header().Set(contentType, contenttype.HTMLUTF8)
|
|
|
|
// Write status code
|
|
|
|
w.WriteHeader(statusCode)
|
2023-03-10 14:14:50 +00:00
|
|
|
// Render (with UI2 plugins)
|
2023-01-23 19:30:47 +00:00
|
|
|
renderPipeReader, renderPipeWriter := io.Pipe()
|
2022-02-11 23:48:59 +00:00
|
|
|
go func() {
|
2023-03-10 14:14:50 +00:00
|
|
|
hb, finish := a.wrapForPlugins(
|
|
|
|
renderPipeWriter,
|
|
|
|
a.getPlugins(pluginUi2Type),
|
|
|
|
func(plugin any, doc *goquery.Document) {
|
|
|
|
plugin.(plugintypes.UI2).RenderWithDocument(data.prc, doc)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
f(hb, data)
|
|
|
|
finish()
|
2023-02-02 21:59:12 +00:00
|
|
|
_ = renderPipeWriter.Close()
|
2022-02-11 23:48:59 +00:00
|
|
|
}()
|
2023-03-10 14:14:50 +00:00
|
|
|
// Run io based UI plugins
|
2023-01-23 19:30:47 +00:00
|
|
|
pluginPipeReader, pluginPipeWriter := io.Pipe()
|
|
|
|
go func() {
|
2023-03-10 14:14:50 +00:00
|
|
|
a.chainUiPlugins(a.getPlugins(pluginUiType), data.prc, renderPipeReader, pluginPipeWriter)
|
2023-02-02 21:59:12 +00:00
|
|
|
_ = pluginPipeWriter.Close()
|
2023-01-23 19:30:47 +00:00
|
|
|
}()
|
|
|
|
// Return minified HTML
|
|
|
|
_ = pluginPipeReader.CloseWithError(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)
|
2023-02-02 21:59:12 +00:00
|
|
|
_ = reader.Close()
|
2022-01-20 17:22:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 17:20:50 +00:00
|
|
|
func (a *goBlog) checkRenderData(r *http.Request, data *renderData) {
|
2021-07-25 08:53:12 +00:00
|
|
|
if data.app == nil {
|
|
|
|
data.app = a
|
|
|
|
}
|
|
|
|
if data.req == nil {
|
|
|
|
data.req = r
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// User
|
2021-06-18 12:32:03 +00:00
|
|
|
if data.User == nil {
|
|
|
|
data.User = a.cfg.User
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// Blog
|
2021-12-20 13:00:11 +00:00
|
|
|
if data.Blog == nil && data.BlogString == "" {
|
|
|
|
data.BlogString, data.Blog = a.getBlog(r)
|
|
|
|
} else if data.Blog == nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
data.Blog = a.cfg.Blogs[data.BlogString]
|
2021-12-20 13:00:11 +00:00
|
|
|
} else if data.BlogString == "" {
|
|
|
|
for name, blog := range a.cfg.Blogs {
|
|
|
|
if blog == data.Blog {
|
|
|
|
data.BlogString = name
|
2021-01-17 11:53:07 +00:00
|
|
|
break
|
|
|
|
}
|
2020-10-12 16:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// Tor
|
2021-06-06 12:39:42 +00:00
|
|
|
if a.cfg.Server.Tor && a.torAddress != "" {
|
2021-08-02 18:43:24 +00:00
|
|
|
data.TorAddress = a.torAddress + r.RequestURI
|
2021-05-09 12:42:53 +00:00
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
if torUsed, ok := r.Context().Value(torUsedKey).(bool); ok && torUsed {
|
|
|
|
data.TorUsed = true
|
2020-12-12 22:44:03 +00:00
|
|
|
}
|
2021-04-23 17:36:57 +00:00
|
|
|
// Check if able to receive webmentions
|
2021-06-06 12:39:42 +00:00
|
|
|
data.WebmentionReceivingEnabled = a.cfg.Webmention == nil || !a.cfg.Webmention.DisableReceiving
|
2021-07-23 10:59:24 +00:00
|
|
|
// Easter egg
|
|
|
|
if ee := a.cfg.EasterEgg; ee != nil && ee.Enabled {
|
|
|
|
data.EasterEgg = true
|
|
|
|
}
|
2023-03-10 14:14:50 +00:00
|
|
|
// Plugins
|
|
|
|
if data.prc == nil {
|
|
|
|
data.prc = &pluginRenderContext{
|
2024-02-08 19:54:16 +00:00
|
|
|
blog: data.BlogString,
|
|
|
|
path: r.URL.Path,
|
|
|
|
url: a.getFullAddress(r.URL.Path),
|
|
|
|
loggedIn: a.isLoggedIn(r),
|
2023-03-10 14:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-23 17:20:50 +00:00
|
|
|
// Data
|
|
|
|
if data.Data == nil {
|
2022-03-16 07:28:03 +00:00
|
|
|
data.Data = map[string]any{}
|
2020-07-31 17:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-23 19:30:47 +00:00
|
|
|
|
|
|
|
// Plugins
|
|
|
|
|
|
|
|
type pluginRenderContext struct {
|
2024-02-08 19:54:16 +00:00
|
|
|
blog string
|
|
|
|
path string
|
|
|
|
url string
|
|
|
|
loggedIn bool
|
2023-01-23 19:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *pluginRenderContext) GetBlog() string {
|
|
|
|
return d.blog
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *pluginRenderContext) GetPath() string {
|
|
|
|
return d.path
|
|
|
|
}
|
2023-03-10 14:14:50 +00:00
|
|
|
|
|
|
|
func (d *pluginRenderContext) GetURL() string {
|
|
|
|
return d.url
|
|
|
|
}
|
2024-02-08 19:54:16 +00:00
|
|
|
|
|
|
|
func (d *pluginRenderContext) IsLoggedIn() bool {
|
|
|
|
return d.loggedIn
|
|
|
|
}
|