GoBlog/render.go

97 lines
2.5 KiB
Go
Raw Normal View History

2020-07-31 13:48:01 +00:00
package main
import (
2022-02-11 23:48:59 +00:00
"io"
"net/http"
2020-10-06 17:07:48 +00:00
"go.goblog.app/app/pkgs/contenttype"
2020-07-31 13:48:01 +00:00
)
2020-10-12 16:47:23 +00:00
type renderData struct {
BlogString string
Canonical string
2021-05-09 12:42:53 +00:00
TorAddress string
Blog *configBlog
User *configUser
2022-03-16 07:28:03 +00:00
Data any
CommentsEnabled bool
WebmentionReceivingEnabled bool
2021-05-09 12:42:53 +00:00
TorUsed bool
2021-07-23 10:59:24 +00:00
EasterEgg bool
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
}
func (a *goBlog) render(w http.ResponseWriter, r *http.Request, f func(*htmlBuilder, *renderData), data *renderData) {
a.renderWithStatusCode(w, r, http.StatusOK, f, data)
}
func (a *goBlog) renderWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, f func(*htmlBuilder, *renderData), data *renderData) {
// Check render data
a.checkRenderData(r, data)
// Set content type
w.Header().Set(contentType, contenttype.HTMLUTF8)
// Write status code
w.WriteHeader(statusCode)
// Render
2022-02-11 23:48:59 +00:00
pipeReader, pipeWriter := io.Pipe()
go func() {
minifyWriter := a.min.Writer(contenttype.HTML, pipeWriter)
2022-02-12 11:37:13 +00:00
f(newHtmlBuilder(minifyWriter), data)
_ = minifyWriter.Close()
2022-02-11 23:48:59 +00:00
_ = pipeWriter.Close()
}()
2022-02-12 11:37:13 +00:00
_, readErr := io.Copy(w, pipeReader)
_ = pipeReader.CloseWithError(readErr)
}
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
if data.User == nil {
data.User = a.cfg.User
}
2021-06-23 17:20:50 +00:00
// Blog
if data.Blog == nil && data.BlogString == "" {
data.BlogString, data.Blog = a.getBlog(r)
} else if data.Blog == nil {
data.Blog = a.cfg.Blogs[data.BlogString]
} 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
if a.cfg.Server.Tor && a.torAddress != "" {
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
}
// Check if comments enabled
data.CommentsEnabled = data.Blog.Comments != nil && data.Blog.Comments.Enabled
// Check if able to receive webmentions
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
}
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{}
}
}