diff options
| author | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
|---|---|---|
| committer | Mitchell Riedstra <mitch@riedstra.dev> | 2023-01-07 13:31:23 -0500 |
| commit | ca33a035c779ae14fb6330c8801c75f49dd1bb79 (patch) | |
| tree | deaabaf15d6d91079a68f247e46070399e4343ee /page/renderJson.go | |
| parent | 97dd660925434be537cd9a49a1d0c893b223e357 (diff) | |
| download | go-website-0.0.22.tar.gz go-website-0.0.22.tar.xz | |
Add an internal caching option. It performs quite well.v0.0.22
Also refactor and clean up most linter warnings.
Diffstat (limited to 'page/renderJson.go')
| -rw-r--r-- | page/renderJson.go | 77 |
1 files changed, 30 insertions, 47 deletions
diff --git a/page/renderJson.go b/page/renderJson.go index 9359b69..4c03e12 100644 --- a/page/renderJson.go +++ b/page/renderJson.go @@ -6,32 +6,28 @@ import ( "errors" "io/fs" "net/http" - "path/filepath" ) -// RenderJson is analogous to Render, though it renders the page, +// 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, +// +// --- +// 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) { - u := getURLPath(r) - - u = filepath.Join(".", u) - // Sepcifically use the specified path for the page p := NewPage(path) @@ -40,22 +36,18 @@ func RenderJson(w http.ResponseWriter, r *http.Request, } out := map[string]interface{}{} - buf := &bytes.Buffer{} + err := p.Render(buf) if err != nil { if errors.Is(err, fs.ErrNotExist) { - renderJsonErr(w, r, "Not found", http.StatusNotFound) + renderJSONErr(w, "Not found", http.StatusNotFound) return } - Logger.Printf("%s %s path: %s rendering encountered: %s", - r.RemoteAddr, - r.Method, - u, - err) - renderJsonErr(w, r, "Internal server error", + logErr(r, "rendering JSON encountered", err) + renderJSONErr(w, "Internal server error", http.StatusInternalServerError) return @@ -66,13 +58,7 @@ func RenderJson(w http.ResponseWriter, r *http.Request, } if r.URL.Query().Get("markdown") == "1" { - if p.Vars != nil { - p.Vars["RenderingMarkdownOnly"] = true - } else { - p.Vars = map[string]interface{}{ - "RenderingMarkdownOnly": true, - } - } + p.setRenderingMarkdownOnly() // Tossing the error, since it would've been revealed above md, _ := p.GetMarkdown() out["Markdown"] = md @@ -80,7 +66,7 @@ func RenderJson(w http.ResponseWriter, r *http.Request, // Make a "set" of keys keys := map[string]struct{}{} - for _, k := range p.JsonVars { + for _, k := range p.JSONVars { keys[k] = struct{}{} } @@ -100,26 +86,23 @@ func RenderJson(w http.ResponseWriter, r *http.Request, err = enc.Encode(out) if err != nil { - Logger.Printf("%s %s %d %s: while writing buf: %s", - r.RemoteAddr, - r.Method, - statusCode, - u, - err) + logErr(r, "while writing to buf", err) return } - Logger.Printf("%s %s %d %s", r.RemoteAddr, r.Method, statusCode, u) + logReq(r, statusCode) } -func renderJsonErr(w http.ResponseWriter, r *http.Request, msg string, - statusCode int) { +func renderJSONErr(w http.ResponseWriter, msg string, statusCode int) { enc := json.NewEncoder(w) w.WriteHeader(statusCode) + _ = enc.Encode(&struct { - Status string + StatusCode int + Msg string }{ - Status: msg, + StatusCode: statusCode, + Msg: msg, }) } |
