aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitchell Riedstra <mitch@riedstra.dev>2026-01-20 01:23:17 -0500
committerMitchell Riedstra <mitch@riedstra.dev>2026-01-20 01:23:17 -0500
commit9c94661b162527c888ea7407380b1229903cb3a1 (patch)
tree2dc3da9d774bc952b6c38a3de01a7be2296ff193
parenta6ea6d1248b38e82c3aa4d6d2c6570306a42751d (diff)
downloadgo-website-wip.tar.gz
go-website-wip.tar.xz
Switch to mapcache by default. Pull out some unused rendering functionswip
-rw-r--r--cmd/server/handlers.go36
-rw-r--r--cmd/server/main.go4
-rw-r--r--page/page.go12
-rw-r--r--page/renderJson.go108
-rw-r--r--page/renderMarkdown.go61
5 files changed, 2 insertions, 219 deletions
diff --git a/cmd/server/handlers.go b/cmd/server/handlers.go
index 010705e..0b9f348 100644
--- a/cmd/server/handlers.go
+++ b/cmd/server/handlers.go
@@ -29,8 +29,6 @@ func (a *App) addCacheableRoutes(r *http.ServeMux) *http.ServeMux {
strings.TrimSuffix(strings.TrimPrefix(a.FeedPrefix, "/"), "/"))
routes := map[string]http.Handler{
- "/_json/": http.StripPrefix("/_json/", a.PageJSONHandler()),
- "/_md/": http.StripPrefix("/_md/", a.PageMarkdownHandler()),
a.FeedPrefix: http.StripPrefix(a.FeedPrefix, a.FeedHandler()),
"/": a.PageHandler(),
}
@@ -91,40 +89,6 @@ func (a *App) PageHandler() http.Handler {
})
}
-func (a *App) PageJSONHandler() http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- u := page.GetURLPath(r)
-
- // Skip template directory
- if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) {
- page.Render4xx(w, r, map[string]interface{}{}, http.StatusNotFound)
-
- return
- }
-
- u = filepath.Join(".", u)
-
- page.RenderJSON(w, r, u, map[string]interface{}{}, http.StatusOK)
- })
-}
-
-func (a *App) PageMarkdownHandler() http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- u := page.GetURLPath(r)
-
- // Skip template directory
- if strings.HasPrefix(u[1:], filepath.Clean(page.TemplateDirectory)) {
- page.Render4xx(w, r, map[string]interface{}{}, http.StatusNotFound)
-
- return
- }
-
- u = filepath.Join(".", u)
-
- page.RenderMarkdown(w, r, u, map[string]interface{}{}, http.StatusOK)
- })
-}
-
func (a *App) RebuildIndexHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u := page.GetPagePath(r)
diff --git a/cmd/server/main.go b/cmd/server/main.go
index 10bbbf0..a776980 100644
--- a/cmd/server/main.go
+++ b/cmd/server/main.go
@@ -40,11 +40,11 @@ func main() { //nolint:funlen
verbose = false
defaultIndexPath = "/reIndex"
indexPath = "/reIndex"
- redisAddr = "127.0.0.1:6379"
+ redisAddr = ""
redisKey = "go-website"
pageTimeout = 15
genhash = false
- mapcache = false
+ mapcache = true
)
fl := flag.NewFlagSet("Website", flag.ExitOnError)
diff --git a/page/page.go b/page/page.go
index 03d1f1e..b0fcfc7 100644
--- a/page/page.go
+++ b/page/page.go
@@ -284,15 +284,3 @@ func (p *Page) GetMarkdown() (string, error) {
func (p Page) String() string {
return fmt.Sprintf("Page: %s", p.path)
}
-
-// setRenderingMarkdownOnly simply sets the special vars field,
-// "RenderingMarkdownOnly" to be true.
-func (p *Page) setRenderingMarkdownOnly() {
- if p.Vars != nil {
- p.Vars["RenderingMarkdownOnly"] = true
- } else {
- p.Vars = map[string]interface{}{
- "RenderingMarkdownOnly": true,
- }
- }
-}
diff --git a/page/renderJson.go b/page/renderJson.go
deleted file mode 100644
index 4c03e12..0000000
--- a/page/renderJson.go
+++ /dev/null
@@ -1,108 +0,0 @@
-package page
-
-import (
- "bytes"
- "encoding/json"
- "errors"
- "io/fs"
- "net/http"
-)
-
-// RenderJSON is analogous to Render, though it renders the page,
-// as a JSON key "content", optionally from the YAML it'll include
-// additional keys in the json if specified under the 'pageJson' key.
-// E.g.
-//
-// ---
-// title: Some page
-// description: Some page desc
-// jsonvars:
-// - includeMeInJSON
-// vars:
-// notIncluded: some value
-// includeMeInJSON:
-// some: arbitrary
-// data: to
-// send: down
-// |--
-// My page content, wee!
-func RenderJSON(w http.ResponseWriter, r *http.Request, //nolint:funlen
- path string, vars map[string]interface{}, statusCode int) {
- // Sepcifically use the specified path for the page
- p := NewPage(path)
-
- if vars != nil {
- p.Vars = vars
- }
-
- out := map[string]interface{}{}
- buf := &bytes.Buffer{}
-
- err := p.Render(buf)
- if err != nil {
- if errors.Is(err, fs.ErrNotExist) {
- renderJSONErr(w, "Not found", http.StatusNotFound)
-
- return
- }
-
- logErr(r, "rendering JSON encountered", err)
- renderJSONErr(w, "Internal server error",
- http.StatusInternalServerError)
-
- return
- }
-
- if r.URL.Query().Get("content") == "1" {
- out["Content"] = buf.String()
- }
-
- if r.URL.Query().Get("markdown") == "1" {
- p.setRenderingMarkdownOnly()
- // Tossing the error, since it would've been revealed above
- md, _ := p.GetMarkdown()
- out["Markdown"] = md
- }
-
- // Make a "set" of keys
- keys := map[string]struct{}{}
- for _, k := range p.JSONVars {
- keys[k] = struct{}{}
- }
-
- // And chuck the keys specified into the output
- for k, v := range p.Vars {
- _, ok := keys[k]
- if ok {
- out[k] = v
- }
- }
-
- w.WriteHeader(statusCode)
-
- enc := json.NewEncoder(w)
- enc.SetIndent("", " ")
- enc.SetEscapeHTML(false)
-
- err = enc.Encode(out)
- if err != nil {
- logErr(r, "while writing to buf", err)
-
- return
- }
-
- logReq(r, statusCode)
-}
-
-func renderJSONErr(w http.ResponseWriter, msg string, statusCode int) {
- enc := json.NewEncoder(w)
- w.WriteHeader(statusCode)
-
- _ = enc.Encode(&struct {
- StatusCode int
- Msg string
- }{
- StatusCode: statusCode,
- Msg: msg,
- })
-}
diff --git a/page/renderMarkdown.go b/page/renderMarkdown.go
deleted file mode 100644
index 8356e55..0000000
--- a/page/renderMarkdown.go
+++ /dev/null
@@ -1,61 +0,0 @@
-package page
-
-import (
- "bytes"
- "errors"
- "io/fs"
- "net/http"
-)
-
-// RenderMarkdown is analogous to Render, except it spits out rendered markdown
-// as text/plain. It also sets .Vars.RenderingMarkdownOnly so templates can
-// vary on whether or not they're plain markdown. For instance, not including
-// some HTML tags.
-func RenderMarkdown(w http.ResponseWriter, r *http.Request,
- path string, vars map[string]interface{}, statusCode int) {
- // Sepcifically use the specified path for the page
- p := NewPage(path)
-
- if vars != nil {
- p.Vars = vars
- }
-
- if p.Vars != nil {
- p.Vars["RenderingMarkdownOnly"] = true
- } else {
- p.Vars = map[string]interface{}{
- "RenderingMarkdownOnly": true,
- }
- }
-
- buf := &bytes.Buffer{}
-
- err := p.Render(buf)
- if err != nil {
- if errors.Is(err, fs.ErrNotExist) {
- plainResp(w, http.StatusNotFound, "Not found")
-
- return
- }
-
- logErr(r, "while rendering", err)
- plainResp(w, http.StatusInternalServerError, "Internal server error")
-
- return
- }
-
- // Error was handled above
- md, _ := p.GetMarkdown()
-
- w.Header().Set("Content-type", "text/plain")
- w.WriteHeader(statusCode)
-
- _, err = w.Write([]byte(md))
- if err != nil {
- logErr(r, "while writing buf", err)
-
- return
- }
-
- logReq(r, statusCode)
-}